@labdigital/commercetools-mock 2.66.0 → 3.0.0-beta.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 (281) hide show
  1. package/README.md +31 -8
  2. package/dist/abstract-BKFcva6S.mjs +1044 -0
  3. package/dist/abstract-BKFcva6S.mjs.map +1 -0
  4. package/dist/config-BcNSzPZz.d.mts +1718 -0
  5. package/dist/index.d.mts +50 -1633
  6. package/dist/index.mjs +3769 -2653
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/storage/sqlite.d.mts +59 -0
  9. package/dist/storage/sqlite.mjs +234 -0
  10. package/dist/storage/sqlite.mjs.map +1 -0
  11. package/package.json +26 -29
  12. package/src/ctMock.ts +125 -136
  13. package/src/helpers.ts +14 -6
  14. package/src/index.ts +5 -0
  15. package/src/lib/masking.ts +4 -5
  16. package/src/lib/product-review-statistics.test.ts +257 -294
  17. package/src/lib/review-statistics.ts +17 -4
  18. package/src/oauth/helpers.ts +7 -4
  19. package/src/oauth/server.test.ts +102 -62
  20. package/src/oauth/server.ts +215 -213
  21. package/src/oauth/store.ts +20 -6
  22. package/src/orderSearch.ts +3 -3
  23. package/src/product-projection-search.ts +38 -20
  24. package/src/product-search-availability.test.ts +31 -52
  25. package/src/product-search.ts +19 -10
  26. package/src/projectAPI.ts +6 -22
  27. package/src/repositories/abstract.ts +182 -48
  28. package/src/repositories/as-associate.test.ts +19 -19
  29. package/src/repositories/associate-role.ts +12 -23
  30. package/src/repositories/attribute-group.test.ts +23 -23
  31. package/src/repositories/attribute-group.ts +6 -4
  32. package/src/repositories/business-unit.test.ts +63 -57
  33. package/src/repositories/business-unit.ts +107 -55
  34. package/src/repositories/cart/actions.ts +96 -65
  35. package/src/repositories/cart/helpers.ts +15 -11
  36. package/src/repositories/cart/index.test.ts +136 -30
  37. package/src/repositories/cart/index.ts +76 -59
  38. package/src/repositories/cart-discount/actions.ts +12 -44
  39. package/src/repositories/cart-discount/index.ts +20 -8
  40. package/src/repositories/category/actions.ts +27 -27
  41. package/src/repositories/category/index.test.ts +13 -9
  42. package/src/repositories/category/index.ts +40 -23
  43. package/src/repositories/channel.test.ts +53 -51
  44. package/src/repositories/channel.ts +12 -22
  45. package/src/repositories/custom-object.ts +34 -25
  46. package/src/repositories/customer/actions.ts +47 -25
  47. package/src/repositories/customer/index.test.ts +11 -11
  48. package/src/repositories/customer/index.ts +65 -35
  49. package/src/repositories/customer-group.test.ts +44 -42
  50. package/src/repositories/customer-group.ts +12 -22
  51. package/src/repositories/discount-code/actions.ts +3 -19
  52. package/src/repositories/discount-code/index.ts +9 -4
  53. package/src/repositories/discount-group/index.ts +8 -3
  54. package/src/repositories/extension.test.ts +27 -27
  55. package/src/repositories/extension.ts +10 -5
  56. package/src/repositories/helpers.ts +126 -47
  57. package/src/repositories/inventory-entry/actions.ts +3 -24
  58. package/src/repositories/inventory-entry/index.ts +19 -11
  59. package/src/repositories/my-customer.ts +13 -12
  60. package/src/repositories/my-order.ts +5 -2
  61. package/src/repositories/order/actions.ts +84 -56
  62. package/src/repositories/order/index.test.ts +36 -31
  63. package/src/repositories/order/index.ts +83 -49
  64. package/src/repositories/order-edit.ts +8 -3
  65. package/src/repositories/payment/actions.ts +64 -44
  66. package/src/repositories/payment/helpers.ts +3 -3
  67. package/src/repositories/payment/index.ts +28 -12
  68. package/src/repositories/product/actions.ts +133 -98
  69. package/src/repositories/product/helpers.ts +29 -16
  70. package/src/repositories/product/index.ts +42 -25
  71. package/src/repositories/product-discount.ts +6 -4
  72. package/src/repositories/product-projection.ts +41 -21
  73. package/src/repositories/product-selection.ts +8 -15
  74. package/src/repositories/product-tailoring.ts +22 -3
  75. package/src/repositories/product-type.ts +45 -4
  76. package/src/repositories/project.ts +57 -13
  77. package/src/repositories/quote/actions.ts +5 -28
  78. package/src/repositories/quote/index.ts +29 -6
  79. package/src/repositories/quote-request/actions.ts +5 -28
  80. package/src/repositories/quote-request/index.test.ts +3 -3
  81. package/src/repositories/quote-request/index.ts +31 -11
  82. package/src/repositories/quote-staged/actions.ts +5 -28
  83. package/src/repositories/quote-staged/index.ts +22 -8
  84. package/src/repositories/recurrence-policy/index.ts +6 -4
  85. package/src/repositories/recurring-order/actions.ts +7 -32
  86. package/src/repositories/recurring-order/index.ts +8 -6
  87. package/src/repositories/review.test.ts +147 -142
  88. package/src/repositories/review.ts +31 -37
  89. package/src/repositories/shipping-method/actions.ts +11 -28
  90. package/src/repositories/shipping-method/index.ts +26 -15
  91. package/src/repositories/shopping-list/actions.ts +21 -31
  92. package/src/repositories/shopping-list/index.ts +44 -22
  93. package/src/repositories/standalone-price.ts +6 -4
  94. package/src/repositories/state.ts +15 -9
  95. package/src/repositories/store.ts +21 -32
  96. package/src/repositories/subscription.test.ts +22 -22
  97. package/src/repositories/subscription.ts +8 -3
  98. package/src/repositories/tax-category/index.ts +8 -3
  99. package/src/repositories/type/actions.ts +21 -3
  100. package/src/repositories/type/index.ts +5 -3
  101. package/src/repositories/zone.test.ts +112 -77
  102. package/src/repositories/zone.ts +5 -3
  103. package/src/schemas/generated/associate-role.ts +13 -0
  104. package/src/schemas/generated/attribute-group.ts +12 -0
  105. package/src/schemas/generated/business-unit.ts +38 -0
  106. package/src/schemas/generated/cart-discount.ts +33 -0
  107. package/src/schemas/generated/cart.ts +61 -0
  108. package/src/schemas/generated/category.ts +25 -0
  109. package/src/schemas/generated/channel.ts +21 -0
  110. package/src/schemas/generated/common.ts +1372 -0
  111. package/src/schemas/generated/custom-object.ts +11 -0
  112. package/src/schemas/generated/customer-group.ts +11 -0
  113. package/src/schemas/generated/customer.ts +47 -0
  114. package/src/schemas/generated/discount-code.ts +25 -0
  115. package/src/schemas/generated/discount-group.ts +13 -0
  116. package/src/schemas/generated/extension.ts +15 -0
  117. package/src/schemas/generated/index.ts +42 -0
  118. package/src/schemas/generated/inventory-entry.ts +20 -0
  119. package/src/schemas/generated/my-quote-request.ts +10 -0
  120. package/src/schemas/generated/order-edit.ts +18 -0
  121. package/src/schemas/generated/order-from-cart.ts +25 -0
  122. package/src/schemas/generated/payment.ts +30 -0
  123. package/src/schemas/generated/product-discount.ts +20 -0
  124. package/src/schemas/generated/product-selection.ts +18 -0
  125. package/src/schemas/generated/product-tailoring.ts +26 -0
  126. package/src/schemas/generated/product-type.ts +12 -0
  127. package/src/schemas/generated/product.ts +37 -0
  128. package/src/schemas/generated/quote-request.ts +19 -0
  129. package/src/schemas/generated/quote.ts +18 -0
  130. package/src/schemas/generated/recurrence-policy.ts +15 -0
  131. package/src/schemas/generated/recurring-order.ts +19 -0
  132. package/src/schemas/generated/review.ts +24 -0
  133. package/src/schemas/generated/shipping-method.ts +24 -0
  134. package/src/schemas/generated/shopping-list.ts +28 -0
  135. package/src/schemas/generated/staged-quote.ts +18 -0
  136. package/src/schemas/generated/standalone-price.ts +32 -0
  137. package/src/schemas/generated/state.ts +20 -0
  138. package/src/schemas/generated/store.ts +23 -0
  139. package/src/schemas/generated/subscription.ts +20 -0
  140. package/src/schemas/generated/tax-category.ts +12 -0
  141. package/src/schemas/generated/type.ts +17 -0
  142. package/src/schemas/generated/zone.ts +12 -0
  143. package/src/schemas/update-request.ts +3 -5
  144. package/src/server.ts +32 -4
  145. package/src/services/abstract.ts +207 -101
  146. package/src/services/as-associate-cart.test.ts +28 -36
  147. package/src/services/as-associate-cart.ts +15 -12
  148. package/src/services/as-associate-order.test.ts +33 -40
  149. package/src/services/as-associate-order.ts +15 -12
  150. package/src/services/as-associate-quote-request.ts +15 -12
  151. package/src/services/as-associate-shopping-list.test.ts +25 -35
  152. package/src/services/as-associate-shopping-list.ts +15 -12
  153. package/src/services/as-associate.test.ts +21 -15
  154. package/src/services/as-associate.ts +23 -22
  155. package/src/services/associate-roles.test.ts +16 -22
  156. package/src/services/associate-roles.ts +2 -2
  157. package/src/services/attribute-group.test.ts +40 -44
  158. package/src/services/attribute-group.ts +2 -2
  159. package/src/services/business-units.test.ts +227 -163
  160. package/src/services/business-units.ts +2 -2
  161. package/src/services/cart-discount.test.ts +253 -187
  162. package/src/services/cart-discount.ts +2 -2
  163. package/src/services/cart.test.ts +833 -832
  164. package/src/services/cart.ts +31 -12
  165. package/src/services/category.test.ts +208 -130
  166. package/src/services/category.ts +2 -2
  167. package/src/services/channel.test.ts +39 -44
  168. package/src/services/channel.ts +2 -2
  169. package/src/services/custom-object.test.ts +103 -79
  170. package/src/services/custom-object.ts +106 -38
  171. package/src/services/customer-group.test.ts +39 -44
  172. package/src/services/customer-group.ts +2 -2
  173. package/src/services/customer.test.ts +357 -292
  174. package/src/services/customer.ts +70 -23
  175. package/src/services/discount-code.test.ts +57 -68
  176. package/src/services/discount-code.ts +2 -2
  177. package/src/services/discount-group.test.ts +111 -134
  178. package/src/services/discount-group.ts +2 -2
  179. package/src/services/draft-validation.test.ts +255 -0
  180. package/src/services/extension.test.ts +39 -44
  181. package/src/services/extension.ts +2 -2
  182. package/src/services/inventory-entry.test.ts +106 -87
  183. package/src/services/inventory-entry.ts +2 -2
  184. package/src/services/my-business-unit.test.ts +82 -112
  185. package/src/services/my-business-unit.ts +25 -19
  186. package/src/services/my-cart.test.ts +46 -41
  187. package/src/services/my-cart.ts +32 -28
  188. package/src/services/my-customer.test.ts +153 -88
  189. package/src/services/my-customer.ts +130 -61
  190. package/src/services/my-order.ts +15 -12
  191. package/src/services/my-payment.test.ts +30 -24
  192. package/src/services/my-payment.ts +2 -2
  193. package/src/services/my-shopping-list.ts +2 -2
  194. package/src/services/order.test.ts +332 -276
  195. package/src/services/order.ts +45 -27
  196. package/src/services/payment.test.ts +31 -29
  197. package/src/services/payment.ts +2 -2
  198. package/src/services/product-discount.test.ts +39 -46
  199. package/src/services/product-discount.ts +2 -2
  200. package/src/services/product-projection.test.ts +176 -166
  201. package/src/services/product-projection.ts +31 -15
  202. package/src/services/product-selection.test.ts +17 -9
  203. package/src/services/product-selection.ts +2 -2
  204. package/src/services/product-type.test.ts +80 -21
  205. package/src/services/product-type.ts +2 -2
  206. package/src/services/product.test.ts +569 -534
  207. package/src/services/product.ts +14 -7
  208. package/src/services/project.test.ts +22 -12
  209. package/src/services/project.ts +28 -13
  210. package/src/services/quote-request.test.ts +36 -39
  211. package/src/services/quote-request.ts +2 -2
  212. package/src/services/quote-staged.ts +2 -2
  213. package/src/services/quote.ts +2 -2
  214. package/src/services/recurrence-policy.test.ts +114 -139
  215. package/src/services/recurrence-policy.ts +2 -2
  216. package/src/services/recurring-order.test.ts +149 -194
  217. package/src/services/recurring-order.ts +2 -2
  218. package/src/services/reviews.test.ts +127 -106
  219. package/src/services/reviews.ts +2 -2
  220. package/src/services/shipping-method.test.ts +96 -125
  221. package/src/services/shipping-method.ts +24 -12
  222. package/src/services/shopping-list.test.ts +183 -141
  223. package/src/services/shopping-list.ts +2 -2
  224. package/src/services/standalone-price.test.ts +60 -46
  225. package/src/services/standalone-price.ts +2 -2
  226. package/src/services/state.test.ts +20 -25
  227. package/src/services/state.ts +2 -2
  228. package/src/services/store.test.ts +26 -45
  229. package/src/services/store.ts +2 -2
  230. package/src/services/subscription.test.ts +39 -44
  231. package/src/services/subscription.ts +2 -2
  232. package/src/services/tax-category.test.ts +33 -36
  233. package/src/services/tax-category.ts +2 -2
  234. package/src/services/type.test.ts +45 -44
  235. package/src/services/type.ts +2 -2
  236. package/src/services/zone.test.ts +40 -44
  237. package/src/services/zone.ts +2 -2
  238. package/src/shipping.ts +41 -11
  239. package/src/storage/abstract.ts +248 -17
  240. package/src/storage/in-memory.ts +147 -290
  241. package/src/storage/sqlite.ts +429 -0
  242. package/src/storage/storage-map.ts +75 -0
  243. package/src/storage/storage.test-helpers.ts +97 -0
  244. package/src/storage/storage.test.ts +802 -0
  245. package/src/testing/associate-role.ts +28 -0
  246. package/src/testing/attribute-group.ts +27 -0
  247. package/src/testing/business-unit.ts +9 -8
  248. package/src/testing/cart-discount.ts +34 -0
  249. package/src/testing/cart.ts +20 -0
  250. package/src/testing/category.ts +25 -0
  251. package/src/testing/channel.ts +23 -0
  252. package/src/testing/custom-object.ts +27 -0
  253. package/src/testing/customer-group.ts +26 -0
  254. package/src/testing/customer.ts +36 -33
  255. package/src/testing/discount-code.ts +29 -0
  256. package/src/testing/discount-group.ts +27 -0
  257. package/src/testing/extension.ts +32 -0
  258. package/src/testing/index.ts +33 -0
  259. package/src/testing/inventory-entry.ts +26 -0
  260. package/src/testing/order.ts +27 -0
  261. package/src/testing/payment.ts +23 -0
  262. package/src/testing/product-discount.ts +33 -0
  263. package/src/testing/product-selection.ts +28 -0
  264. package/src/testing/product-type.ts +27 -0
  265. package/src/testing/product.ts +38 -0
  266. package/src/testing/quote-request.ts +29 -0
  267. package/src/testing/recurrence-policy.ts +33 -0
  268. package/src/testing/recurring-order.ts +32 -0
  269. package/src/testing/review.ts +24 -0
  270. package/src/testing/shipping-method.ts +31 -0
  271. package/src/testing/shopping-list.ts +25 -0
  272. package/src/testing/standalone-price.ts +31 -0
  273. package/src/testing/state.ts +21 -0
  274. package/src/testing/store.ts +26 -0
  275. package/src/testing/subscription.ts +38 -0
  276. package/src/testing/tax-category.ts +27 -0
  277. package/src/testing/type.ts +9 -6
  278. package/src/testing/zone.ts +22 -0
  279. package/src/validate.test.ts +122 -0
  280. package/src/validate.ts +78 -7
  281. package/src/.env +0 -0
@@ -22,10 +22,7 @@ import type {
22
22
  import type { Writable } from "#src/types.ts";
23
23
  import type { RepositoryContext, UpdateHandlerInterface } from "../abstract.ts";
24
24
  import { AbstractUpdateHandler } from "../abstract.ts";
25
- import {
26
- createCustomFields,
27
- getReferenceFromResourceIdentifier,
28
- } from "../helpers.ts";
25
+ import { getReferenceFromResourceIdentifier } from "../helpers.ts";
29
26
  import { transformShippingRate } from "./helpers.ts";
30
27
 
31
28
  export class ShippingMethodUpdateHandler
@@ -60,16 +57,17 @@ export class ShippingMethodUpdateHandler
60
57
  });
61
58
  }
62
59
 
63
- addZone(
60
+ async addZone(
64
61
  context: RepositoryContext,
65
62
  resource: Writable<ShippingMethod>,
66
63
  { zone }: ShippingMethodAddZoneAction,
67
64
  ) {
68
- const zoneReference = getReferenceFromResourceIdentifier<ZoneReference>(
69
- zone,
70
- context.projectKey,
71
- this._storage,
72
- );
65
+ const zoneReference =
66
+ await getReferenceFromResourceIdentifier<ZoneReference>(
67
+ zone,
68
+ context.projectKey,
69
+ this._storage,
70
+ );
73
71
 
74
72
  if (resource.zoneRates === undefined) {
75
73
  resource.zoneRates = [];
@@ -136,30 +134,15 @@ export class ShippingMethodUpdateHandler
136
134
  resource: Writable<ShippingMethod>,
137
135
  { name, value }: ShippingMethodSetCustomFieldAction,
138
136
  ) {
139
- if (!resource.custom) {
140
- return;
141
- }
142
- if (value === null) {
143
- delete resource.custom.fields[name];
144
- } else {
145
- resource.custom.fields[name] = value;
146
- }
137
+ this._setCustomFieldValues(resource, { name, value });
147
138
  }
148
139
 
149
- setCustomType(
140
+ async setCustomType(
150
141
  context: RepositoryContext,
151
142
  resource: Writable<ShippingMethod>,
152
143
  { type, fields }: ShippingMethodSetCustomTypeAction,
153
144
  ) {
154
- if (type) {
155
- resource.custom = createCustomFields(
156
- { type, fields },
157
- context.projectKey,
158
- this._storage,
159
- );
160
- } else {
161
- resource.custom = undefined;
162
- }
145
+ await this._setCustomType(context, resource, { type, fields });
163
146
  }
164
147
 
165
148
  setDescription(
@@ -6,6 +6,7 @@ import type {
6
6
  ZoneReference,
7
7
  } from "@commercetools/platform-sdk";
8
8
  import type { Config } from "#src/config.ts";
9
+ import { ShippingMethodDraftSchema } from "#src/schemas/generated/shipping-method.ts";
9
10
  import { getBaseResourceProperties } from "../../helpers.ts";
10
11
  import { getShippingMethodsMatchingCart } from "../../shipping.ts";
11
12
  import type { GetParams, RepositoryContext } from "../abstract.ts";
@@ -21,31 +22,36 @@ export class ShippingMethodRepository extends AbstractResourceRepository<"shippi
21
22
  constructor(config: Config) {
22
23
  super("shipping-method", config);
23
24
  this.actions = new ShippingMethodUpdateHandler(config.storage);
25
+ this.draftSchema = ShippingMethodDraftSchema;
24
26
  }
25
27
 
26
- create(
28
+ async create(
27
29
  context: RepositoryContext,
28
30
  draft: ShippingMethodDraft,
29
- ): ShippingMethod {
31
+ ): Promise<ShippingMethod> {
32
+ const zoneRates = draft.zoneRates
33
+ ? await Promise.all(
34
+ draft.zoneRates.map((z) => this._transformZoneRateDraft(context, z)),
35
+ )
36
+ : [];
37
+
30
38
  const resource: ShippingMethod = {
31
- ...getBaseResourceProperties(),
39
+ ...getBaseResourceProperties(context.clientId),
32
40
  ...draft,
33
41
  active: draft.active ?? true,
34
- taxCategory: getReferenceFromResourceIdentifier(
42
+ taxCategory: await getReferenceFromResourceIdentifier(
35
43
  draft.taxCategory,
36
44
  context.projectKey,
37
45
  this._storage,
38
46
  ),
39
- zoneRates: draft.zoneRates?.map((z) =>
40
- this._transformZoneRateDraft(context, z),
41
- ),
42
- custom: createCustomFields(
47
+ zoneRates,
48
+ custom: await createCustomFields(
43
49
  draft.custom,
44
50
  context.projectKey,
45
51
  this._storage,
46
52
  ),
47
53
  };
48
- return this.saveNew(context, resource);
54
+ return await this.saveNew(context, resource);
49
55
  }
50
56
 
51
57
  /*
@@ -54,26 +60,31 @@ export class ShippingMethodRepository extends AbstractResourceRepository<"shippi
54
60
  * the flag isMatching set to true. This ShippingRate is used when the
55
61
  * ShippingMethod is added to the Cart.
56
62
  */
57
- public matchingCart(
63
+ public async matchingCart(
58
64
  context: RepositoryContext,
59
65
  cartId: string,
60
66
  params: GetParams = {},
61
67
  ) {
62
- const cart = this._storage.get(context.projectKey, "cart", cartId);
68
+ const cart = await this._storage.get(context.projectKey, "cart", cartId);
63
69
  if (!cart) {
64
70
  return undefined;
65
71
  }
66
72
 
67
- return getShippingMethodsMatchingCart(context, this._storage, cart, params);
73
+ return await getShippingMethodsMatchingCart(
74
+ context,
75
+ this._storage,
76
+ cart,
77
+ params,
78
+ );
68
79
  }
69
80
 
70
- private _transformZoneRateDraft(
81
+ private async _transformZoneRateDraft(
71
82
  context: RepositoryContext,
72
83
  draft: ZoneRateDraft,
73
- ): ZoneRate {
84
+ ): Promise<ZoneRate> {
74
85
  return {
75
86
  ...draft,
76
- zone: getReferenceFromResourceIdentifier<ZoneReference>(
87
+ zone: await getReferenceFromResourceIdentifier<ZoneReference>(
77
88
  draft.zone,
78
89
  context.projectKey,
79
90
  this._storage,
@@ -1,7 +1,7 @@
1
1
  import type {
2
2
  GeneralError,
3
+ InvalidOperationError,
3
4
  Product,
4
- ProductPagedQueryResponse,
5
5
  ShoppingList,
6
6
  ShoppingListAddLineItemAction,
7
7
  ShoppingListChangeLineItemQuantityAction,
@@ -30,7 +30,7 @@ export class ShoppingListUpdateHandler
30
30
  implements
31
31
  Partial<UpdateHandlerInterface<ShoppingList, ShoppingListUpdateAction>>
32
32
  {
33
- addLineItem(
33
+ async addLineItem(
34
34
  context: RepositoryContext,
35
35
  resource: Writable<ShoppingList>,
36
36
  {
@@ -46,14 +46,19 @@ export class ShoppingListUpdateHandler
46
46
 
47
47
  if (productId) {
48
48
  // Fetch product and variant by ID
49
- product = this._storage.get(context.projectKey, "product", productId, {});
49
+ product = await this._storage.get(
50
+ context.projectKey,
51
+ "product",
52
+ productId,
53
+ {},
54
+ );
50
55
  } else if (sku) {
51
56
  // Fetch product and variant by SKU
52
- const items = this._storage.query(context.projectKey, "product", {
57
+ const items = await this._storage.query(context.projectKey, "product", {
53
58
  where: [
54
59
  `masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
55
60
  ],
56
- }) as ProductPagedQueryResponse;
61
+ });
57
62
 
58
63
  if (items.count === 1) {
59
64
  product = items.results[0];
@@ -208,7 +213,10 @@ export class ShoppingListUpdateHandler
208
213
  { customer }: ShoppingListSetCustomerAction,
209
214
  ) {
210
215
  if (customer?.key) {
211
- throw new Error("set customer on shoppinglist by key not implemented");
216
+ throw new CommercetoolsError<InvalidOperationError>({
217
+ code: "InvalidOperation",
218
+ message: "set customer on shoppinglist by key not implemented",
219
+ });
212
220
  }
213
221
  if (customer?.id) {
214
222
  resource.customer = { typeId: "customer", id: customer.id };
@@ -220,36 +228,15 @@ export class ShoppingListUpdateHandler
220
228
  resource: ShoppingList,
221
229
  { name, value }: ShoppingListSetCustomFieldAction,
222
230
  ) {
223
- if (!resource.custom) {
224
- throw new Error("Resource has no custom field");
225
- }
226
- resource.custom.fields[name] = value;
231
+ this._setCustomFieldValues(resource, { name, value });
227
232
  }
228
233
 
229
- setCustomType(
234
+ async setCustomType(
230
235
  context: RepositoryContext,
231
236
  resource: Writable<ShoppingList>,
232
237
  { type, fields }: ShoppingListSetCustomTypeAction,
233
238
  ) {
234
- if (!type) {
235
- resource.custom = undefined;
236
- } else {
237
- const resolvedType = this._storage.getByResourceIdentifier(
238
- context.projectKey,
239
- type,
240
- );
241
- if (!resolvedType) {
242
- throw new Error(`Type ${type} not found`);
243
- }
244
-
245
- resource.custom = {
246
- type: {
247
- typeId: "type",
248
- id: resolvedType.id,
249
- },
250
- fields: fields || {},
251
- };
252
- }
239
+ await this._setCustomType(context, resource, { type, fields });
253
240
  }
254
241
 
255
242
  setDeleteDaysAfterLastModification(
@@ -295,7 +282,10 @@ export class ShoppingListUpdateHandler
295
282
  resource.store = { typeId: "store", key: store.key };
296
283
  }
297
284
  if (store?.id) {
298
- throw new Error("set store on shoppinglist by id not implemented");
285
+ throw new CommercetoolsError<InvalidOperationError>({
286
+ code: "InvalidOperation",
287
+ message: "set store on shoppinglist by id not implemented",
288
+ });
299
289
  }
300
290
  }
301
291
  }
@@ -1,12 +1,14 @@
1
1
  import type {
2
2
  CustomerReference,
3
3
  LineItemDraft,
4
- ProductPagedQueryResponse,
4
+ ReferencedResourceNotFoundError,
5
5
  ShoppingList,
6
6
  ShoppingListDraft,
7
7
  ShoppingListLineItem,
8
8
  } from "@commercetools/platform-sdk";
9
9
  import type { Config } from "#src/config.ts";
10
+ import { CommercetoolsError } from "#src/exceptions.ts";
11
+ import { ShoppingListDraftSchema } from "#src/schemas/generated/shopping-list.ts";
10
12
  import { getBaseResourceProperties } from "../../helpers.ts";
11
13
  import type { Writable } from "../../types.ts";
12
14
  import type { RepositoryContext } from "../abstract.ts";
@@ -23,18 +25,23 @@ export class ShoppingListRepository extends AbstractResourceRepository<"shopping
23
25
  constructor(config: Config) {
24
26
  super("shopping-list", config);
25
27
  this.actions = new ShoppingListUpdateHandler(config.storage);
28
+ this.draftSchema = ShoppingListDraftSchema;
26
29
  }
27
30
 
28
- create(context: RepositoryContext, draft: ShoppingListDraft): ShoppingList {
29
- const lineItems =
31
+ async create(
32
+ context: RepositoryContext,
33
+ draft: ShoppingListDraft,
34
+ ): Promise<ShoppingList> {
35
+ const lineItems = await Promise.all(
30
36
  draft.lineItems?.map((draftLineItem) =>
31
37
  this.draftLineItemtoLineItem(context.projectKey, draftLineItem),
32
- ) ?? [];
38
+ ) ?? [],
39
+ );
33
40
 
34
41
  const resource: ShoppingList = {
35
- ...getBaseResourceProperties(),
42
+ ...getBaseResourceProperties(context.clientId),
36
43
  ...draft,
37
- custom: createCustomFields(
44
+ custom: await createCustomFields(
38
45
  draft.custom,
39
46
  context.projectKey,
40
47
  this._storage,
@@ -42,30 +49,34 @@ export class ShoppingListRepository extends AbstractResourceRepository<"shopping
42
49
  textLineItems: [],
43
50
  lineItems,
44
51
  customer: draft.customer
45
- ? getReferenceFromResourceIdentifier<CustomerReference>(
52
+ ? await getReferenceFromResourceIdentifier<CustomerReference>(
46
53
  draft.customer,
47
54
  context.projectKey,
48
55
  this._storage,
49
56
  )
50
57
  : undefined,
51
58
  store: draft.store
52
- ? getStoreKeyReference(draft.store, context.projectKey, this._storage)
59
+ ? await getStoreKeyReference(
60
+ draft.store,
61
+ context.projectKey,
62
+ this._storage,
63
+ )
53
64
  : undefined,
54
65
  businessUnit: draft.businessUnit
55
- ? getBusinessUnitKeyReference(
66
+ ? await getBusinessUnitKeyReference(
56
67
  draft.businessUnit,
57
68
  context.projectKey,
58
69
  this._storage,
59
70
  )
60
71
  : undefined,
61
72
  };
62
- return this.saveNew(context, resource);
73
+ return await this.saveNew(context, resource);
63
74
  }
64
75
 
65
- draftLineItemtoLineItem = (
76
+ draftLineItemtoLineItem = async (
66
77
  projectKey: string,
67
78
  draftLineItem: LineItemDraft,
68
- ): ShoppingListLineItem => {
79
+ ): Promise<ShoppingListLineItem> => {
69
80
  const { sku, productId, variantId } = draftLineItem;
70
81
 
71
82
  const lineItem: Writable<ShoppingListLineItem> = {
@@ -78,7 +89,7 @@ export class ShoppingListRepository extends AbstractResourceRepository<"shopping
78
89
  published: true,
79
90
  quantity: draftLineItem.quantity ?? 1,
80
91
  productType: { typeId: "product-type", id: "" },
81
- custom: createCustomFields(
92
+ custom: await createCustomFields(
82
93
  draftLineItem.custom,
83
94
  projectKey,
84
95
  this._storage,
@@ -90,14 +101,18 @@ export class ShoppingListRepository extends AbstractResourceRepository<"shopping
90
101
  }
91
102
 
92
103
  if (sku) {
93
- const items = this._storage.query(projectKey, "product", {
104
+ const items = await this._storage.query(projectKey, "product", {
94
105
  where: [
95
106
  `masterData(current(masterVariant(sku="${sku}"))) or masterData(current(variants(sku="${sku}")))`,
96
107
  ],
97
- }) as ProductPagedQueryResponse;
108
+ });
98
109
 
99
110
  if (items.count === 0) {
100
- throw new Error(`Product with sku ${sku} not found`);
111
+ throw new CommercetoolsError<ReferencedResourceNotFoundError>({
112
+ code: "ReferencedResourceNotFound",
113
+ message: `Product with sku ${sku} not found`,
114
+ typeId: "product",
115
+ });
101
116
  }
102
117
 
103
118
  const product = items.results[0];
@@ -112,12 +127,16 @@ export class ShoppingListRepository extends AbstractResourceRepository<"shopping
112
127
  }
113
128
 
114
129
  if (productId) {
115
- const items = this._storage.query(projectKey, "product", {
130
+ const items = await this._storage.query(projectKey, "product", {
116
131
  where: [`id="${productId}"`],
117
- }) as ProductPagedQueryResponse;
132
+ });
118
133
 
119
134
  if (items.count === 0) {
120
- throw new Error(`Product with id ${productId} not found`);
135
+ throw new CommercetoolsError<ReferencedResourceNotFoundError>({
136
+ code: "ReferencedResourceNotFound",
137
+ message: `Product with id ${productId} not found`,
138
+ typeId: "product",
139
+ });
121
140
  }
122
141
 
123
142
  const variantId = items.results[0].masterData.current.masterVariant.id;
@@ -125,8 +144,11 @@ export class ShoppingListRepository extends AbstractResourceRepository<"shopping
125
144
  return lineItem;
126
145
  }
127
146
 
128
- throw new Error(
129
- "must provide either sku, productId or variantId for ShoppingListLineItem",
130
- );
147
+ throw new CommercetoolsError<ReferencedResourceNotFoundError>({
148
+ code: "ReferencedResourceNotFound",
149
+ message:
150
+ "must provide either sku, productId or variantId for ShoppingListLineItem",
151
+ typeId: "product",
152
+ });
131
153
  };
132
154
  }
@@ -10,6 +10,7 @@ import type {
10
10
  StandalonePriceUpdateAction,
11
11
  } from "@commercetools/platform-sdk";
12
12
  import type { Config } from "#src/config.ts";
13
+ import { StandalonePriceDraftSchema } from "#src/schemas/generated/standalone-price.ts";
13
14
  import { getBaseResourceProperties } from "../helpers.ts";
14
15
  import type { Writable } from "../types.ts";
15
16
  import type { RepositoryContext, UpdateHandlerInterface } from "./abstract.ts";
@@ -23,14 +24,15 @@ export class StandAlonePriceRepository extends AbstractResourceRepository<"stand
23
24
  constructor(config: Config) {
24
25
  super("standalone-price", config);
25
26
  this.actions = new StandalonePriceUpdateHandler(this._storage);
27
+ this.draftSchema = StandalonePriceDraftSchema;
26
28
  }
27
29
 
28
- create(
30
+ async create(
29
31
  context: RepositoryContext,
30
32
  draft: StandalonePriceDraft,
31
- ): StandalonePrice {
33
+ ): Promise<StandalonePrice> {
32
34
  const resource: StandalonePrice = {
33
- ...getBaseResourceProperties(),
35
+ ...getBaseResourceProperties(context.clientId),
34
36
  active: draft.active ? draft.active : false,
35
37
  sku: draft.sku,
36
38
  value: createTypedMoney(draft.value),
@@ -44,7 +46,7 @@ export class StandAlonePriceRepository extends AbstractResourceRepository<"stand
44
46
  validFrom: draft.validFrom,
45
47
  validUntil: draft.validUntil,
46
48
  };
47
- return this.saveNew(context, resource);
49
+ return await this.saveNew(context, resource);
48
50
  }
49
51
 
50
52
  transformChannelReferenceDraft(
@@ -14,6 +14,7 @@ import type {
14
14
  StateUpdateAction,
15
15
  } from "@commercetools/platform-sdk";
16
16
  import type { Config } from "#src/config.ts";
17
+ import { StateDraftSchema } from "#src/schemas/generated/state.ts";
17
18
  import { getBaseResourceProperties } from "../helpers.ts";
18
19
  import type { Writable } from "../types.ts";
19
20
  import type { RepositoryContext, UpdateHandlerInterface } from "./abstract.ts";
@@ -27,24 +28,29 @@ export class StateRepository extends AbstractResourceRepository<"state"> {
27
28
  constructor(config: Config) {
28
29
  super("state", config);
29
30
  this.actions = new StateUpdateHandler(config.storage);
31
+ this.draftSchema = StateDraftSchema;
30
32
  }
31
33
 
32
- create(context: RepositoryContext, draft: StateDraft): State {
33
- const resource: State = {
34
- ...getBaseResourceProperties(),
35
- ...draft,
36
- builtIn: false,
37
- initial: draft.initial || false,
38
- transitions: (draft.transitions || []).map((t) =>
39
- getReferenceFromResourceIdentifier(
34
+ async create(context: RepositoryContext, draft: StateDraft): Promise<State> {
35
+ const transitions = await Promise.all(
36
+ (draft.transitions || []).map((t) =>
37
+ getReferenceFromResourceIdentifier<StateReference>(
40
38
  t,
41
39
  context.projectKey,
42
40
  this._storage,
43
41
  ),
44
42
  ),
43
+ );
44
+
45
+ const resource: State = {
46
+ ...getBaseResourceProperties(context.clientId),
47
+ ...draft,
48
+ builtIn: false,
49
+ initial: draft.initial || false,
50
+ transitions,
45
51
  };
46
52
 
47
- return this.saveNew(context, resource);
53
+ return await this.saveNew(context, resource);
48
54
  }
49
55
  }
50
56
 
@@ -12,6 +12,7 @@ import type {
12
12
  StoreUpdateAction,
13
13
  } from "@commercetools/platform-sdk";
14
14
  import type { Config } from "#src/config.ts";
15
+ import { StoreDraftSchema } from "#src/schemas/generated/store.ts";
15
16
  import { getBaseResourceProperties } from "../helpers.ts";
16
17
  import type { AbstractStorage } from "../storage/abstract.ts";
17
18
  import type { Writable } from "../types.ts";
@@ -29,48 +30,51 @@ export class StoreRepository extends AbstractResourceRepository<"store"> {
29
30
  constructor(config: Config) {
30
31
  super("store", config);
31
32
  this.actions = new StoreUpdateHandler(this._storage);
33
+ this.draftSchema = StoreDraftSchema;
32
34
  }
33
35
 
34
- create(context: RepositoryContext, draft: StoreDraft): Store {
36
+ async create(context: RepositoryContext, draft: StoreDraft): Promise<Store> {
35
37
  const resource: Store = {
36
- ...getBaseResourceProperties(),
38
+ ...getBaseResourceProperties(context.clientId),
37
39
  key: draft.key,
38
40
  name: draft.name,
39
41
  languages: draft.languages ?? [],
40
42
  countries: draft.countries ?? [],
41
- distributionChannels: transformChannels(
43
+ distributionChannels: await transformChannels(
42
44
  context,
43
45
  this._storage,
44
46
  draft.distributionChannels,
45
47
  ),
46
- supplyChannels: transformChannels(
48
+ supplyChannels: await transformChannels(
47
49
  context,
48
50
  this._storage,
49
51
  draft.supplyChannels,
50
52
  ),
51
53
  productSelections: [],
52
- custom: createCustomFields(
54
+ custom: await createCustomFields(
53
55
  draft.custom,
54
56
  context.projectKey,
55
57
  this._storage,
56
58
  ),
57
59
  };
58
- return this.saveNew(context, resource);
60
+ return await this.saveNew(context, resource);
59
61
  }
60
62
  }
61
63
 
62
- const transformChannels = (
64
+ const transformChannels = async (
63
65
  context: RepositoryContext,
64
66
  storage: AbstractStorage,
65
67
  channels?: ChannelResourceIdentifier[],
66
68
  ) => {
67
69
  if (!channels) return [];
68
70
 
69
- return channels.map((ref) =>
70
- getReferenceFromResourceIdentifier<ChannelReference>(
71
- ref,
72
- context.projectKey,
73
- storage,
71
+ return Promise.all(
72
+ channels.map((ref) =>
73
+ getReferenceFromResourceIdentifier<ChannelReference>(
74
+ ref,
75
+ context.projectKey,
76
+ storage,
77
+ ),
74
78
  ),
75
79
  );
76
80
  };
@@ -92,38 +96,23 @@ class StoreUpdateHandler
92
96
  resource: Writable<Store>,
93
97
  { name, value }: StoreSetCustomFieldAction,
94
98
  ) {
95
- if (!resource.custom) {
96
- return;
97
- }
98
- if (value === null) {
99
- delete resource.custom.fields[name];
100
- } else {
101
- resource.custom.fields[name] = value;
102
- }
99
+ this._setCustomFieldValues(resource, { name, value });
103
100
  }
104
101
 
105
- setCustomType(
102
+ async setCustomType(
106
103
  context: RepositoryContext,
107
104
  resource: Writable<Store>,
108
105
  { type, fields }: StoreSetCustomTypeAction,
109
106
  ) {
110
- if (type) {
111
- resource.custom = createCustomFields(
112
- { type, fields },
113
- context.projectKey,
114
- this._storage,
115
- );
116
- } else {
117
- resource.custom = undefined;
118
- }
107
+ await this._setCustomType(context, resource, { type, fields });
119
108
  }
120
109
 
121
- setDistributionChannels(
110
+ async setDistributionChannels(
122
111
  context: RepositoryContext,
123
112
  resource: Writable<Store>,
124
113
  { distributionChannels }: StoreSetDistributionChannelsAction,
125
114
  ) {
126
- resource.distributionChannels = transformChannels(
115
+ resource.distributionChannels = await transformChannels(
127
116
  context,
128
117
  this._storage,
129
118
  distributionChannels,