@labdigital/commercetools-mock 2.59.1 → 2.61.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/dist/{index.d.ts → index.d.mts} +154 -275
  2. package/dist/{index.js → index.mjs} +413 -401
  3. package/dist/index.mjs.map +1 -0
  4. package/package.json +30 -18
  5. package/src/config.ts +1 -1
  6. package/src/ctMock.test.ts +1 -1
  7. package/src/ctMock.ts +22 -21
  8. package/src/index.test.ts +1 -1
  9. package/src/index.ts +3 -3
  10. package/src/lib/haversine.test.ts +2 -2
  11. package/src/lib/masking.ts +1 -1
  12. package/src/lib/parser.ts +2 -2
  13. package/src/lib/password.ts +1 -1
  14. package/src/lib/predicateParser.test.ts +1 -1
  15. package/src/lib/predicateParser.ts +4 -4
  16. package/src/lib/product-review-statistics.test.ts +4 -8
  17. package/src/lib/productSearchFilter.test.ts +3 -3
  18. package/src/lib/productSearchFilter.ts +6 -6
  19. package/src/lib/projectionSearchFilter.test.ts +3 -3
  20. package/src/lib/projectionSearchFilter.ts +4 -4
  21. package/src/lib/review-statistics.ts +1 -1
  22. package/src/lib/searchQueryTypeChecker.test.ts +3 -3
  23. package/src/lib/searchQueryTypeChecker.ts +8 -4
  24. package/src/lib/tax.test.ts +119 -0
  25. package/src/lib/tax.ts +186 -0
  26. package/src/oauth/server.test.ts +7 -7
  27. package/src/oauth/server.ts +7 -7
  28. package/src/priceSelector.test.ts +1 -1
  29. package/src/priceSelector.ts +2 -2
  30. package/src/product-projection-search.ts +10 -12
  31. package/src/product-search-availability.test.ts +1 -1
  32. package/src/product-search.ts +6 -7
  33. package/src/projectAPI.test.ts +1 -1
  34. package/src/projectAPI.ts +7 -7
  35. package/src/repositories/abstract.ts +8 -10
  36. package/src/repositories/as-associate.test.ts +4 -4
  37. package/src/repositories/as-associate.ts +3 -3
  38. package/src/repositories/associate-role.ts +7 -9
  39. package/src/repositories/attribute-group.test.ts +3 -3
  40. package/src/repositories/attribute-group.ts +5 -5
  41. package/src/repositories/business-unit.test.ts +6 -6
  42. package/src/repositories/business-unit.ts +21 -23
  43. package/src/repositories/cart/actions.ts +79 -15
  44. package/src/repositories/cart/helpers.ts +5 -84
  45. package/src/repositories/cart/index.test.ts +53 -10
  46. package/src/repositories/cart/index.ts +17 -21
  47. package/src/repositories/cart-discount/actions.ts +5 -6
  48. package/src/repositories/cart-discount/index.ts +5 -5
  49. package/src/repositories/category/actions.ts +4 -4
  50. package/src/repositories/category/index.test.ts +3 -3
  51. package/src/repositories/category/index.ts +8 -8
  52. package/src/repositories/channel.test.ts +4 -4
  53. package/src/repositories/channel.ts +6 -6
  54. package/src/repositories/custom-object.ts +11 -8
  55. package/src/repositories/customer/actions.ts +9 -9
  56. package/src/repositories/customer/index.test.ts +3 -3
  57. package/src/repositories/customer/index.ts +12 -9
  58. package/src/repositories/customer-group.test.ts +4 -4
  59. package/src/repositories/customer-group.ts +6 -6
  60. package/src/repositories/discount-code/actions.ts +4 -4
  61. package/src/repositories/discount-code/index.ts +5 -5
  62. package/src/repositories/discount-group/actions.ts +3 -4
  63. package/src/repositories/discount-group/index.ts +4 -4
  64. package/src/repositories/errors.ts +1 -1
  65. package/src/repositories/extension.test.ts +3 -3
  66. package/src/repositories/extension.ts +6 -6
  67. package/src/repositories/helpers.ts +9 -12
  68. package/src/repositories/index.test.ts +3 -3
  69. package/src/repositories/index.ts +43 -43
  70. package/src/repositories/inventory-entry/actions.ts +15 -3
  71. package/src/repositories/inventory-entry/index.ts +5 -5
  72. package/src/repositories/my-customer.ts +5 -5
  73. package/src/repositories/my-order.ts +2 -2
  74. package/src/repositories/my-quote-request.ts +1 -1
  75. package/src/repositories/order/actions.ts +8 -9
  76. package/src/repositories/order/index.test.ts +191 -9
  77. package/src/repositories/order/index.ts +63 -21
  78. package/src/repositories/order-edit.ts +4 -4
  79. package/src/repositories/payment/actions.ts +6 -6
  80. package/src/repositories/payment/helpers.ts +3 -3
  81. package/src/repositories/payment/index.ts +7 -7
  82. package/src/repositories/product/actions.ts +7 -7
  83. package/src/repositories/product/helpers.ts +4 -4
  84. package/src/repositories/product/index.ts +13 -13
  85. package/src/repositories/product-discount.ts +9 -6
  86. package/src/repositories/product-projection.ts +7 -7
  87. package/src/repositories/product-selection.ts +9 -6
  88. package/src/repositories/product-tailoring.ts +6 -3
  89. package/src/repositories/product-type.ts +8 -5
  90. package/src/repositories/project.ts +5 -5
  91. package/src/repositories/quote/actions.ts +6 -6
  92. package/src/repositories/quote/index.ts +5 -5
  93. package/src/repositories/quote-request/actions.ts +6 -6
  94. package/src/repositories/quote-request/index.test.ts +3 -3
  95. package/src/repositories/quote-request/index.ts +5 -5
  96. package/src/repositories/quote-staged/actions.ts +6 -6
  97. package/src/repositories/quote-staged/index.ts +5 -5
  98. package/src/repositories/recurrence-policy/actions.ts +3 -3
  99. package/src/repositories/recurrence-policy/index.ts +4 -8
  100. package/src/repositories/recurring-order/actions.ts +3 -3
  101. package/src/repositories/recurring-order/index.ts +5 -5
  102. package/src/repositories/review.test.ts +5 -5
  103. package/src/repositories/review.ts +10 -9
  104. package/src/repositories/shipping-method/actions.ts +7 -9
  105. package/src/repositories/shipping-method/helpers.ts +1 -1
  106. package/src/repositories/shipping-method/index.ts +8 -8
  107. package/src/repositories/shopping-list/actions.ts +5 -5
  108. package/src/repositories/shopping-list/index.ts +7 -7
  109. package/src/repositories/standalone-price.ts +9 -6
  110. package/src/repositories/state.ts +9 -6
  111. package/src/repositories/store.ts +10 -7
  112. package/src/repositories/subscription.test.ts +3 -3
  113. package/src/repositories/subscription.ts +9 -6
  114. package/src/repositories/tax-category/actions.ts +4 -4
  115. package/src/repositories/tax-category/index.ts +6 -6
  116. package/src/repositories/type/actions.ts +4 -4
  117. package/src/repositories/type/index.ts +5 -5
  118. package/src/repositories/zone.test.ts +3 -3
  119. package/src/repositories/zone.ts +8 -5
  120. package/src/server.ts +4 -3
  121. package/src/services/abstract.ts +5 -5
  122. package/src/services/as-associate-cart.test.ts +1 -1
  123. package/src/services/as-associate-cart.ts +2 -2
  124. package/src/services/as-associate-order.test.ts +1 -1
  125. package/src/services/as-associate-order.ts +2 -2
  126. package/src/services/as-associate-quote-request.ts +2 -3
  127. package/src/services/as-associate.test.ts +1 -1
  128. package/src/services/as-associate.ts +4 -4
  129. package/src/services/associate-roles.test.ts +1 -1
  130. package/src/services/associate-roles.ts +2 -2
  131. package/src/services/attribute-group.test.ts +1 -1
  132. package/src/services/attribute-group.ts +2 -2
  133. package/src/services/business-units.test.ts +5 -5
  134. package/src/services/business-units.ts +2 -2
  135. package/src/services/cart-discount.ts +2 -2
  136. package/src/services/cart.test.ts +213 -2
  137. package/src/services/cart.ts +4 -5
  138. package/src/services/category.test.ts +1 -1
  139. package/src/services/category.ts +2 -2
  140. package/src/services/channel.test.ts +1 -1
  141. package/src/services/channel.ts +2 -2
  142. package/src/services/custom-object.test.ts +2 -2
  143. package/src/services/custom-object.ts +3 -3
  144. package/src/services/customer-group.test.ts +1 -1
  145. package/src/services/customer-group.ts +2 -2
  146. package/src/services/customer.test.ts +3 -3
  147. package/src/services/customer.ts +4 -5
  148. package/src/services/discount-code.test.ts +1 -1
  149. package/src/services/discount-code.ts +2 -2
  150. package/src/services/discount-group.test.ts +1 -1
  151. package/src/services/discount-group.ts +2 -2
  152. package/src/services/extension.test.ts +1 -1
  153. package/src/services/extension.ts +2 -2
  154. package/src/services/index.ts +44 -44
  155. package/src/services/inventory-entry.test.ts +16 -1
  156. package/src/services/inventory-entry.ts +2 -2
  157. package/src/services/my-business-unit.test.ts +1 -1
  158. package/src/services/my-business-unit.ts +2 -2
  159. package/src/services/my-cart.test.ts +1 -1
  160. package/src/services/my-cart.ts +2 -2
  161. package/src/services/my-customer.test.ts +2 -2
  162. package/src/services/my-customer.ts +6 -6
  163. package/src/services/my-order.ts +2 -2
  164. package/src/services/my-payment.test.ts +1 -1
  165. package/src/services/my-payment.ts +2 -2
  166. package/src/services/my-shopping-list.ts +2 -2
  167. package/src/services/order.test.ts +4 -11
  168. package/src/services/order.ts +4 -4
  169. package/src/services/payment.test.ts +1 -1
  170. package/src/services/payment.ts +2 -2
  171. package/src/services/product-discount.test.ts +1 -1
  172. package/src/services/product-discount.ts +2 -2
  173. package/src/services/product-projection.test.ts +2 -2
  174. package/src/services/product-projection.ts +4 -4
  175. package/src/services/product-selection.test.ts +1 -1
  176. package/src/services/product-selection.ts +2 -2
  177. package/src/services/product-type.test.ts +1 -1
  178. package/src/services/product-type.ts +2 -2
  179. package/src/services/product.test.ts +1 -1
  180. package/src/services/product.ts +3 -3
  181. package/src/services/project.test.ts +1 -1
  182. package/src/services/project.ts +4 -4
  183. package/src/services/quote-request.test.ts +2 -2
  184. package/src/services/quote-request.ts +2 -2
  185. package/src/services/quote-staged.ts +2 -2
  186. package/src/services/quote.ts +2 -2
  187. package/src/services/recurrence-policy.test.ts +1 -1
  188. package/src/services/recurrence-policy.ts +2 -2
  189. package/src/services/recurring-order.test.ts +1 -1
  190. package/src/services/recurring-order.ts +2 -2
  191. package/src/services/reviews.test.ts +1 -1
  192. package/src/services/reviews.ts +2 -2
  193. package/src/services/shipping-method.test.ts +2 -2
  194. package/src/services/shipping-method.ts +4 -4
  195. package/src/services/shopping-list.test.ts +1 -1
  196. package/src/services/shopping-list.ts +2 -2
  197. package/src/services/standalone-price.test.ts +1 -1
  198. package/src/services/standalone-price.ts +2 -2
  199. package/src/services/state.test.ts +1 -1
  200. package/src/services/state.ts +2 -2
  201. package/src/services/store.test.ts +1 -1
  202. package/src/services/store.ts +2 -2
  203. package/src/services/subscription.test.ts +1 -1
  204. package/src/services/subscription.ts +2 -2
  205. package/src/services/tax-category.test.ts +1 -1
  206. package/src/services/tax-category.ts +2 -2
  207. package/src/services/type.test.ts +1 -1
  208. package/src/services/type.ts +2 -2
  209. package/src/services/zone.test.ts +1 -1
  210. package/src/services/zone.ts +2 -2
  211. package/src/shipping.test.ts +1 -1
  212. package/src/shipping.ts +8 -8
  213. package/src/storage/abstract.ts +1 -1
  214. package/src/storage/in-memory.ts +15 -17
  215. package/src/storage/index.ts +2 -2
  216. package/src/testing/business-unit.ts +1 -2
  217. package/src/testing/customer.ts +1 -1
  218. package/src/testing/type.ts +1 -1
  219. package/src/types.ts +1 -1
  220. package/src/validate.ts +1 -1
  221. package/dist/index.d.ts.map +0 -1
  222. package/dist/index.js.map +0 -1
@@ -1,8 +1,3 @@
1
- import type {
2
- CartSetAnonymousIdAction,
3
- CartSetCustomerIdAction,
4
- CartUpdateAction,
5
- } from "@commercetools/platform-sdk";
6
1
  import type {
7
2
  Address,
8
3
  AddressDraft,
@@ -18,22 +13,26 @@ import type {
18
13
  CartRemoveDiscountCodeAction,
19
14
  CartRemoveLineItemAction,
20
15
  CartRemoveShippingMethodAction,
16
+ CartSetAnonymousIdAction,
21
17
  CartSetBillingAddressAction,
22
18
  CartSetBillingAddressCustomTypeAction,
23
19
  CartSetCountryAction,
20
+ CartSetCustomerEmailAction,
21
+ CartSetCustomerIdAction,
24
22
  CartSetCustomFieldAction,
25
23
  CartSetCustomShippingMethodAction,
26
24
  CartSetCustomTypeAction,
27
- CartSetCustomerEmailAction,
28
25
  CartSetDirectDiscountsAction,
29
26
  CartSetLineItemCustomFieldAction,
30
27
  CartSetLineItemCustomTypeAction,
28
+ CartSetLineItemPriceAction,
31
29
  CartSetLineItemShippingDetailsAction,
32
30
  CartSetLocaleAction,
33
31
  CartSetShippingAddressAction,
34
32
  CartSetShippingAddressCustomFieldAction,
35
33
  CartSetShippingAddressCustomTypeAction,
36
34
  CartSetShippingMethodAction,
35
+ CartUpdateAction,
37
36
  CustomFields,
38
37
  GeneralError,
39
38
  ItemShippingDetails,
@@ -41,7 +40,6 @@ import type {
41
40
  Product,
42
41
  ProductPagedQueryResponse,
43
42
  ProductVariant,
44
- TaxCategoryReference,
45
43
  } from "@commercetools/platform-sdk";
46
44
  import type {
47
45
  CustomLineItem,
@@ -49,23 +47,23 @@ import type {
49
47
  } from "@commercetools/platform-sdk/dist/declarations/src/generated/models/cart";
50
48
  import type { ShippingMethodResourceIdentifier } from "@commercetools/platform-sdk/dist/declarations/src/generated/models/shipping-method";
51
49
  import { v4 as uuidv4 } from "uuid";
52
- import { CommercetoolsError } from "~src/exceptions";
53
- import type { Writable } from "~src/types";
54
- import type { CartRepository } from ".";
55
- import type { UpdateHandlerInterface } from "../abstract";
56
- import { AbstractUpdateHandler, type RepositoryContext } from "../abstract";
50
+ import { CommercetoolsError } from "#src/exceptions.ts";
51
+ import type { Writable } from "#src/types.ts";
52
+ import type { UpdateHandlerInterface } from "../abstract.ts";
53
+ import { AbstractUpdateHandler, type RepositoryContext } from "../abstract.ts";
57
54
  import {
58
55
  createAddress,
59
56
  createCentPrecisionMoney,
60
57
  createCustomFields,
61
58
  createTypedMoney,
62
- } from "../helpers";
59
+ } from "../helpers.ts";
63
60
  import {
64
61
  calculateCartTotalPrice,
65
62
  calculateLineItemTotalPrice,
66
63
  createCustomLineItemFromDraft,
67
64
  selectPrice,
68
- } from "./helpers";
65
+ } from "./helpers.ts";
66
+ import type { CartRepository } from "./index.ts";
69
67
 
70
68
  export class CartUpdateHandler
71
69
  extends AbstractUpdateHandler
@@ -740,6 +738,72 @@ export class CartUpdateHandler
740
738
  }
741
739
  }
742
740
 
741
+ setLineItemPrice(
742
+ context: RepositoryContext,
743
+ resource: Writable<Cart>,
744
+ { lineItemId, lineItemKey, externalPrice }: CartSetLineItemPriceAction,
745
+ ) {
746
+ const lineItem = resource.lineItems.find(
747
+ (x) =>
748
+ (lineItemId && x.id === lineItemId) ||
749
+ (lineItemKey && x.key === lineItemKey),
750
+ );
751
+
752
+ if (!lineItem) {
753
+ throw new CommercetoolsError<GeneralError>({
754
+ code: "General",
755
+ message: lineItemKey
756
+ ? `A line item with key '${lineItemKey}' not found.`
757
+ : `A line item with ID '${lineItemId}' not found.`,
758
+ });
759
+ }
760
+
761
+ if (!externalPrice && lineItem.priceMode !== "ExternalPrice") {
762
+ return;
763
+ }
764
+
765
+ if (
766
+ externalPrice &&
767
+ externalPrice.currencyCode !== resource.totalPrice.currencyCode
768
+ ) {
769
+ throw new CommercetoolsError<GeneralError>({
770
+ code: "General",
771
+ message: `Currency mismatch. Expected '${resource.totalPrice.currencyCode}' but got '${externalPrice.currencyCode}'.`,
772
+ });
773
+ }
774
+
775
+ if (externalPrice) {
776
+ lineItem.priceMode = "ExternalPrice";
777
+ const priceValue = createTypedMoney(externalPrice);
778
+
779
+ lineItem.price = lineItem.price ?? { id: uuidv4() };
780
+ lineItem.price.value = priceValue;
781
+ } else {
782
+ lineItem.priceMode = "Platform";
783
+
784
+ const price = selectPrice({
785
+ prices: lineItem.variant.prices,
786
+ currency: resource.totalPrice.currencyCode,
787
+ country: resource.country,
788
+ });
789
+
790
+ if (!price) {
791
+ throw new Error(
792
+ `No valid price found for ${lineItem.productId} for country ${resource.country} and currency ${resource.totalPrice.currencyCode}`,
793
+ );
794
+ }
795
+
796
+ lineItem.price = price;
797
+ }
798
+
799
+ const lineItemTotal = calculateLineItemTotalPrice(lineItem);
800
+ lineItem.totalPrice = createCentPrecisionMoney({
801
+ ...lineItem.price!.value,
802
+ centAmount: lineItemTotal,
803
+ });
804
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
805
+ }
806
+
743
807
  setLineItemShippingDetails(
744
808
  context: RepositoryContext,
745
809
  resource: Writable<Cart>,
@@ -790,7 +854,7 @@ export class CartUpdateHandler
790
854
  return;
791
855
  }
792
856
 
793
- let custom: CustomFields | undefined = undefined;
857
+ let custom: CustomFields | undefined;
794
858
  if ((address as Address & AddressDraft).custom) {
795
859
  custom = createCustomFields(
796
860
  (address as Address & AddressDraft).custom,
@@ -1,22 +1,21 @@
1
1
  import type {
2
2
  Cart,
3
- CentPrecisionMoney,
4
3
  CustomLineItem,
5
4
  CustomLineItemDraft,
6
5
  LineItem,
7
6
  Price,
8
7
  TaxCategory,
9
8
  TaxCategoryReference,
10
- TaxedPrice,
11
9
  } from "@commercetools/platform-sdk";
12
10
  import { v4 as uuidv4 } from "uuid";
13
- import type { AbstractStorage } from "~src/storage/abstract";
11
+ import { calculateTaxedPrice } from "#src/lib/tax.ts";
12
+ import type { AbstractStorage } from "#src/storage/abstract.ts";
14
13
  import {
15
14
  createCentPrecisionMoney,
16
15
  createCustomFields,
17
16
  createTypedMoney,
18
17
  getReferenceFromResourceIdentifier,
19
- } from "../helpers";
18
+ } from "../helpers.ts";
20
19
 
21
20
  export const selectPrice = ({
22
21
  prices,
@@ -56,84 +55,6 @@ export const calculateCartTotalPrice = (cart: Cart): number => {
56
55
  return lineItemsTotal + customLineItemsTotal;
57
56
  };
58
57
 
59
- export const calculateTaxedPrice = (
60
- amount: number,
61
- taxCategory: TaxCategory | undefined,
62
- currency: string,
63
- country: string | undefined,
64
- ): TaxedPrice | undefined => {
65
- if (!taxCategory || !taxCategory.rates.length) {
66
- return undefined;
67
- }
68
-
69
- // Find the appropriate tax rate for the country
70
- const taxRate =
71
- taxCategory.rates.find(
72
- (rate) => !rate.country || rate.country === country,
73
- ) || taxCategory.rates[0]; // Fallback to first rate if no country-specific rate found
74
-
75
- if (!taxRate) {
76
- return undefined;
77
- }
78
-
79
- let netAmount: number;
80
- let grossAmount: number;
81
- let taxAmount: number;
82
-
83
- if (taxRate.includedInPrice) {
84
- // Amount is gross, calculate net
85
- grossAmount = amount;
86
- taxAmount = Math.round(
87
- (grossAmount * taxRate.amount) / (1 + taxRate.amount),
88
- );
89
- netAmount = grossAmount - taxAmount;
90
- } else {
91
- // Amount is net, calculate gross
92
- netAmount = amount;
93
- taxAmount = Math.round(netAmount * taxRate.amount);
94
- grossAmount = netAmount + taxAmount;
95
- }
96
-
97
- return {
98
- totalNet: {
99
- type: "centPrecision",
100
- currencyCode: currency,
101
- centAmount: netAmount,
102
- fractionDigits: 2,
103
- },
104
- totalGross: {
105
- type: "centPrecision",
106
- currencyCode: currency,
107
- centAmount: grossAmount,
108
- fractionDigits: 2,
109
- },
110
- taxPortions:
111
- taxAmount > 0
112
- ? [
113
- {
114
- rate: taxRate.amount,
115
- amount: {
116
- type: "centPrecision",
117
- currencyCode: currency,
118
- centAmount: taxAmount,
119
- fractionDigits: 2,
120
- },
121
- name: taxRate.name,
122
- },
123
- ]
124
- : [],
125
- totalTax:
126
- taxAmount > 0
127
- ? {
128
- type: "centPrecision",
129
- currencyCode: currency,
130
- centAmount: taxAmount,
131
- fractionDigits: 2,
132
- }
133
- : undefined,
134
- };
135
- };
136
-
137
58
  export const createCustomLineItemFromDraft = (
138
59
  projectKey: string,
139
60
  draft: CustomLineItemDraft,
@@ -150,13 +71,13 @@ export const createCustomLineItemFromDraft = (
150
71
  )
151
72
  : undefined;
152
73
 
153
- let taxCategory: TaxCategory | undefined = undefined;
74
+ let taxCategory: TaxCategory | undefined;
154
75
  if (taxCategoryRef) {
155
76
  try {
156
77
  taxCategory =
157
78
  storage.get(projectKey, "tax-category", taxCategoryRef.id, {}) ||
158
79
  undefined;
159
- } catch (error) {
80
+ } catch (_error) {
160
81
  // Tax category not found, continue without tax calculation
161
82
  }
162
83
  }
@@ -1,14 +1,9 @@
1
- import type {
2
- Cart,
3
- CartDraft,
4
- CustomLineItemDraft,
5
- LineItem,
6
- } from "@commercetools/platform-sdk";
1
+ import type { CartDraft, LineItem } from "@commercetools/platform-sdk";
7
2
  import { beforeEach, describe, expect, test } from "vitest";
8
- import type { Config } from "~src/config";
9
- import { getBaseResourceProperties } from "~src/helpers";
10
- import { InMemoryStorage } from "~src/storage";
11
- import { CartRepository } from "./index";
3
+ import type { Config } from "#src/config.ts";
4
+ import { getBaseResourceProperties } from "#src/helpers.ts";
5
+ import { InMemoryStorage } from "#src/storage/index.ts";
6
+ import { CartRepository } from "./index.ts";
12
7
 
13
8
  describe("Cart repository", () => {
14
9
  const storage = new InMemoryStorage();
@@ -389,6 +384,54 @@ describe("Cart repository", () => {
389
384
  expect(customLineItem.taxRate?.includedInPrice).toBe(false);
390
385
  expect(customLineItem.taxRate?.country).toBe("NL");
391
386
  });
387
+
388
+ test("should calculate taxed price for the cart", () => {
389
+ storage.add("dummy", "tax-category", {
390
+ ...getBaseResourceProperties(),
391
+ id: "cart-tax-category",
392
+ key: "cart-vat-tax",
393
+ name: "Cart VAT Tax",
394
+ rates: [
395
+ {
396
+ id: "cart-rate-1",
397
+ name: "Standard VAT",
398
+ amount: 0.21,
399
+ includedInPrice: false,
400
+ country: "NL",
401
+ },
402
+ ],
403
+ });
404
+
405
+ const cart: CartDraft = {
406
+ currency: "EUR",
407
+ country: "NL",
408
+ customLineItems: [
409
+ {
410
+ name: { en: "Gift Wrap" },
411
+ slug: "gift-wrap",
412
+ money: {
413
+ currencyCode: "EUR",
414
+ centAmount: 1000,
415
+ },
416
+ quantity: 1,
417
+ taxCategory: {
418
+ typeId: "tax-category" as const,
419
+ id: "cart-tax-category",
420
+ },
421
+ },
422
+ ],
423
+ };
424
+
425
+ const ctx = { projectKey: "dummy", storeKey: "dummyStore" };
426
+ const result = repository.create(ctx, cart);
427
+
428
+ expect(result.taxedPrice).toBeDefined();
429
+ expect(result.taxedPrice?.totalNet.centAmount).toBe(1000);
430
+ expect(result.taxedPrice?.totalGross.centAmount).toBe(1210);
431
+ expect(result.taxedPrice?.totalTax?.centAmount).toBe(210);
432
+ expect(result.taxedPrice?.taxPortions).toHaveLength(1);
433
+ expect(result.taxedPrice?.taxPortions?.[0].rate).toBe(0.21);
434
+ });
392
435
  });
393
436
 
394
437
  describe("createShippingInfo", () => {
@@ -1,44 +1,36 @@
1
1
  import type {
2
2
  BusinessUnit,
3
- CentPrecisionMoney,
4
- InvalidOperationError,
5
- MissingTaxRateForCountryError,
6
- ShippingMethodDoesNotMatchCartError,
7
- } from "@commercetools/platform-sdk";
8
- import type {
9
3
  Cart,
10
4
  CartDraft,
11
- CustomLineItem,
12
- CustomLineItemDraft,
13
5
  GeneralError,
6
+ InvalidOperationError,
14
7
  LineItem,
15
8
  LineItemDraft,
16
9
  Product,
17
10
  ProductPagedQueryResponse,
18
- TaxPortion,
19
- TaxedItemPrice,
11
+ ShippingMethodDoesNotMatchCartError,
20
12
  } from "@commercetools/platform-sdk";
21
- import { Decimal } from "decimal.js/decimal";
22
13
  import { v4 as uuidv4 } from "uuid";
23
- import type { Config } from "~src/config";
24
- import { CommercetoolsError } from "~src/exceptions";
25
- import { getBaseResourceProperties } from "~src/helpers";
14
+ import type { Config } from "#src/config.ts";
15
+ import { CommercetoolsError } from "#src/exceptions.ts";
16
+ import { getBaseResourceProperties } from "#src/helpers.ts";
17
+ import { calculateTaxTotals } from "#src/lib/tax.ts";
26
18
  import {
27
19
  createShippingInfoFromMethod,
28
20
  getShippingMethodsMatchingCart,
29
- } from "~src/shipping";
30
- import type { Writable } from "~src/types";
21
+ } from "#src/shipping.ts";
22
+ import type { Writable } from "#src/types.ts";
31
23
  import {
32
24
  AbstractResourceRepository,
33
25
  type RepositoryContext,
34
- } from "../abstract";
35
- import { createAddress, createCustomFields } from "../helpers";
36
- import { CartUpdateHandler } from "./actions";
26
+ } from "../abstract.ts";
27
+ import { createAddress, createCustomFields } from "../helpers.ts";
28
+ import { CartUpdateHandler } from "./actions.ts";
37
29
  import {
38
30
  calculateCartTotalPrice,
39
31
  createCustomLineItemFromDraft,
40
32
  selectPrice,
41
- } from "./helpers";
33
+ } from "./helpers.ts";
42
34
 
43
35
  export class CartRepository extends AbstractResourceRepository<"cart"> {
44
36
  constructor(config: Config) {
@@ -62,7 +54,7 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
62
54
  });
63
55
  }
64
56
 
65
- let storedBusinessUnit: BusinessUnit | undefined = undefined;
57
+ let storedBusinessUnit: BusinessUnit | undefined;
66
58
  if (draft.businessUnit?.id || draft.businessUnit?.key) {
67
59
  storedBusinessUnit =
68
60
  this._storage.getByResourceIdentifier<"business-unit">(
@@ -163,6 +155,10 @@ export class CartRepository extends AbstractResourceRepository<"cart"> {
163
155
  );
164
156
  }
165
157
 
158
+ const { taxedPrice, taxedShippingPrice } = calculateTaxTotals(resource);
159
+ resource.taxedPrice = taxedPrice;
160
+ resource.taxedShippingPrice = taxedShippingPrice;
161
+
166
162
  return this.saveNew(context, resource);
167
163
  }
168
164
 
@@ -14,12 +14,11 @@ import type {
14
14
  CartDiscountUpdateAction,
15
15
  InvalidOperationError,
16
16
  } from "@commercetools/platform-sdk";
17
- import type { Writable } from "~src/types";
18
- import type { UpdateHandlerInterface } from "../abstract";
19
- import { AbstractUpdateHandler, type RepositoryContext } from "../abstract";
20
-
21
- import { CommercetoolsError } from "~src/exceptions";
22
- import { getStoreKeyReference } from "~src/repositories/helpers";
17
+ import { CommercetoolsError } from "#src/exceptions.ts";
18
+ import { getStoreKeyReference } from "#src/repositories/helpers.ts";
19
+ import type { Writable } from "#src/types.ts";
20
+ import type { UpdateHandlerInterface } from "../abstract.ts";
21
+ import { AbstractUpdateHandler, type RepositoryContext } from "../abstract.ts";
23
22
 
24
23
  export class CartDiscountUpdateHandler
25
24
  extends AbstractUpdateHandler
@@ -7,18 +7,18 @@ import type {
7
7
  CartDiscountValueGiftLineItem,
8
8
  CartDiscountValueRelative,
9
9
  } from "@commercetools/platform-sdk";
10
- import type { Config } from "~src/config";
11
- import { getBaseResourceProperties } from "~src/helpers";
10
+ import type { Config } from "#src/config.ts";
11
+ import { getBaseResourceProperties } from "#src/helpers.ts";
12
12
  import {
13
13
  AbstractResourceRepository,
14
14
  type RepositoryContext,
15
- } from "../abstract";
15
+ } from "../abstract.ts";
16
16
  import {
17
17
  createCustomFields,
18
18
  createTypedMoney,
19
19
  getStoreKeyReference,
20
- } from "../helpers";
21
- import { CartDiscountUpdateHandler } from "./actions";
20
+ } from "../helpers.ts";
21
+ import { CartDiscountUpdateHandler } from "./actions.ts";
22
22
 
23
23
  export class CartDiscountRepository extends AbstractResourceRepository<"cart-discount"> {
24
24
  constructor(config: Config) {
@@ -20,10 +20,10 @@ import type {
20
20
  CategoryUpdateAction,
21
21
  } from "@commercetools/platform-sdk";
22
22
  import { v4 as uuidv4 } from "uuid";
23
- import type { Writable } from "~src/types";
24
- import type { RepositoryContext, UpdateHandlerInterface } from "../abstract";
25
- import { AbstractUpdateHandler } from "../abstract";
26
- import { createCustomFields } from "../helpers";
23
+ import type { Writable } from "#src/types.ts";
24
+ import type { RepositoryContext, UpdateHandlerInterface } from "../abstract.ts";
25
+ import { AbstractUpdateHandler } from "../abstract.ts";
26
+ import { createCustomFields } from "../helpers.ts";
27
27
 
28
28
  export class CategoryUpdateHandler
29
29
  extends AbstractUpdateHandler
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, test } from "vitest";
2
- import type { Config } from "~src/config";
3
- import { InMemoryStorage } from "~src/storage";
4
- import { CategoryRepository } from "./index";
2
+ import type { Config } from "#src/config.ts";
3
+ import { InMemoryStorage } from "#src/storage/index.ts";
4
+ import { CategoryRepository } from "./index.ts";
5
5
 
6
6
  describe("Order repository", () => {
7
7
  const storage = new InMemoryStorage();
@@ -4,17 +4,17 @@ import type {
4
4
  CategoryReference,
5
5
  } from "@commercetools/platform-sdk";
6
6
  import { v4 as uuidv4 } from "uuid";
7
- import type { Config } from "~src/config";
8
- import { getBaseResourceProperties } from "~src/helpers";
9
- import { parseExpandClause } from "~src/lib/expandParser";
10
- import type { Writable } from "~src/types";
11
- import type { GetParams } from "../abstract";
7
+ import type { Config } from "#src/config.ts";
8
+ import { getBaseResourceProperties } from "#src/helpers.ts";
9
+ import { parseExpandClause } from "#src/lib/expandParser.ts";
10
+ import type { Writable } from "#src/types.ts";
11
+ import type { GetParams } from "../abstract.ts";
12
12
  import {
13
13
  AbstractResourceRepository,
14
14
  type RepositoryContext,
15
- } from "../abstract";
16
- import { createCustomFields } from "../helpers";
17
- import { CategoryUpdateHandler } from "./actions";
15
+ } from "../abstract.ts";
16
+ import { createCustomFields } from "../helpers.ts";
17
+ import { CategoryUpdateHandler } from "./actions.ts";
18
18
 
19
19
  export class CategoryRepository extends AbstractResourceRepository<"category"> {
20
20
  constructor(config: Config) {
@@ -9,10 +9,10 @@ import type {
9
9
  ChannelSetGeoLocationAction,
10
10
  } from "@commercetools/platform-sdk";
11
11
  import { describe, expect, test } from "vitest";
12
- import type { Config } from "~src/config";
13
- import { getBaseResourceProperties } from "~src/helpers";
14
- import { InMemoryStorage } from "~src/storage";
15
- import { ChannelRepository } from "./channel";
12
+ import type { Config } from "#src/config.ts";
13
+ import { getBaseResourceProperties } from "#src/helpers.ts";
14
+ import { InMemoryStorage } from "#src/storage/index.ts";
15
+ import { ChannelRepository } from "./channel.ts";
16
16
 
17
17
  describe("Channel Repository", () => {
18
18
  const storage = new InMemoryStorage();
@@ -10,16 +10,16 @@ import type {
10
10
  ChannelSetGeoLocationAction,
11
11
  ChannelUpdateAction,
12
12
  } from "@commercetools/platform-sdk";
13
- import type { Config } from "~src/config";
14
- import { getBaseResourceProperties } from "../helpers";
15
- import type { Writable } from "../types";
16
- import type { UpdateHandlerInterface } from "./abstract";
13
+ import type { Config } from "#src/config.ts";
14
+ import { getBaseResourceProperties } from "../helpers.ts";
15
+ import type { Writable } from "../types.ts";
16
+ import type { UpdateHandlerInterface } from "./abstract.ts";
17
17
  import {
18
18
  AbstractResourceRepository,
19
19
  AbstractUpdateHandler,
20
20
  type RepositoryContext,
21
- } from "./abstract";
22
- import { createAddress, createCustomFields } from "./helpers";
21
+ } from "./abstract.ts";
22
+ import { createAddress, createCustomFields } from "./helpers.ts";
23
23
 
24
24
  export class ChannelRepository extends AbstractResourceRepository<"channel"> {
25
25
  constructor(config: Config) {
@@ -3,13 +3,16 @@ import type {
3
3
  CustomObjectDraft,
4
4
  InvalidOperationError,
5
5
  } from "@commercetools/platform-sdk";
6
- import type { Config } from "~src/config";
7
- import { CommercetoolsError } from "~src/exceptions";
8
- import { cloneObject, getBaseResourceProperties } from "../helpers";
9
- import type { Writable } from "../types";
10
- import type { QueryParams } from "./abstract";
11
- import { AbstractResourceRepository, type RepositoryContext } from "./abstract";
12
- import { checkConcurrentModification } from "./errors";
6
+ import type { Config } from "#src/config.ts";
7
+ import { CommercetoolsError } from "#src/exceptions.ts";
8
+ import { cloneObject, getBaseResourceProperties } from "../helpers.ts";
9
+ import type { Writable } from "../types.ts";
10
+ import type { QueryParams } from "./abstract.ts";
11
+ import {
12
+ AbstractResourceRepository,
13
+ type RepositoryContext,
14
+ } from "./abstract.ts";
15
+ import { checkConcurrentModification } from "./errors.ts";
13
16
 
14
17
  export class CustomObjectRepository extends AbstractResourceRepository<"key-value-document"> {
15
18
  constructor(config: Config) {
@@ -88,7 +91,7 @@ export class CustomObjectRepository extends AbstractResourceRepository<"key-valu
88
91
  where: whereClause,
89
92
  });
90
93
 
91
- // @ts-ignore
94
+ // @ts-expect-error
92
95
  result.results = result.results.map((r) =>
93
96
  this.postProcessResource(context, r as CustomObject, {
94
97
  expand: params.expand,
@@ -17,10 +17,10 @@ import type {
17
17
  CustomerSetAddressCustomTypeAction,
18
18
  CustomerSetAuthenticationModeAction,
19
19
  CustomerSetCompanyNameAction,
20
- CustomerSetCustomFieldAction,
21
- CustomerSetCustomTypeAction,
22
20
  CustomerSetCustomerGroupAction,
23
21
  CustomerSetCustomerNumberAction,
22
+ CustomerSetCustomFieldAction,
23
+ CustomerSetCustomTypeAction,
24
24
  CustomerSetDateOfBirthAction,
25
25
  CustomerSetDefaultBillingAddressAction,
26
26
  CustomerSetDefaultShippingAddressAction,
@@ -39,13 +39,13 @@ import type {
39
39
  InvalidJsonInputError,
40
40
  InvalidOperationError,
41
41
  } from "@commercetools/platform-sdk";
42
- import { CommercetoolsError } from "~src/exceptions";
43
- import { generateRandomString } from "~src/helpers";
44
- import { hashPassword } from "~src/lib/password";
45
- import type { Writable } from "~src/types";
46
- import type { UpdateHandlerInterface } from "../abstract";
47
- import { AbstractUpdateHandler, type RepositoryContext } from "../abstract";
48
- import { createAddress, createCustomFields } from "../helpers";
42
+ import { CommercetoolsError } from "#src/exceptions.ts";
43
+ import { generateRandomString } from "#src/helpers.ts";
44
+ import { hashPassword } from "#src/lib/password.ts";
45
+ import type { Writable } from "#src/types.ts";
46
+ import type { UpdateHandlerInterface } from "../abstract.ts";
47
+ import { AbstractUpdateHandler, type RepositoryContext } from "../abstract.ts";
48
+ import { createAddress, createCustomFields } from "../helpers.ts";
49
49
 
50
50
  export class CustomerUpdateHandler
51
51
  extends AbstractUpdateHandler
@@ -1,8 +1,8 @@
1
1
  import type { Store } from "@commercetools/platform-sdk";
2
2
  import { describe, expect, test } from "vitest";
3
- import type { Config } from "~src/config";
4
- import { InMemoryStorage } from "~src/storage";
5
- import { CustomerRepository } from "./index";
3
+ import type { Config } from "#src/config.ts";
4
+ import { InMemoryStorage } from "#src/storage/index.ts";
5
+ import { CustomerRepository } from "./index.ts";
6
6
 
7
7
  describe("Customer repository", () => {
8
8
  const storage = new InMemoryStorage();