@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
@@ -8,7 +8,7 @@ import type {
8
8
  ChannelSetCustomTypeAction,
9
9
  ChannelSetGeoLocationAction,
10
10
  } from "@commercetools/platform-sdk";
11
- import { describe, expect, test } from "vitest";
11
+ import { beforeAll, describe, expect, test } from "vitest";
12
12
  import type { Config } from "#src/config.ts";
13
13
  import { getBaseResourceProperties } from "#src/helpers.ts";
14
14
  import { InMemoryStorage } from "#src/storage/index.ts";
@@ -19,25 +19,27 @@ describe("Channel Repository", () => {
19
19
  const config: Config = { storage, strict: false };
20
20
  const repository = new ChannelRepository(config);
21
21
 
22
- // Add a custom type for testing
23
- storage.add("dummy", "type", {
24
- ...getBaseResourceProperties(),
25
- id: "custom-type-id",
26
- key: "custom-type-key",
27
- name: { "en-US": "Custom Type" },
28
- resourceTypeIds: ["channel"],
29
- fieldDefinitions: [
30
- {
31
- name: "description",
32
- label: { "en-US": "Description" },
33
- required: false,
34
- type: { name: "String" },
35
- inputHint: "SingleLine",
36
- },
37
- ],
22
+ beforeAll(async () => {
23
+ // Add a custom type for testing
24
+ await storage.add("dummy", "type", {
25
+ ...getBaseResourceProperties(),
26
+ id: "custom-type-id",
27
+ key: "custom-type-key",
28
+ name: { "en-US": "Custom Type" },
29
+ resourceTypeIds: ["channel"],
30
+ fieldDefinitions: [
31
+ {
32
+ name: "description",
33
+ label: { "en-US": "Description" },
34
+ required: false,
35
+ type: { name: "String" },
36
+ inputHint: "SingleLine",
37
+ },
38
+ ],
39
+ });
38
40
  });
39
41
 
40
- test("create channel", () => {
42
+ test("create channel", async () => {
41
43
  const draft: ChannelDraft = {
42
44
  key: "distribution-center-1",
43
45
  name: { "en-US": "Distribution Center 1" },
@@ -46,7 +48,7 @@ describe("Channel Repository", () => {
46
48
  };
47
49
 
48
50
  const ctx = { projectKey: "dummy" };
49
- const result = repository.create(ctx, draft);
51
+ const result = await repository.create(ctx, draft);
50
52
 
51
53
  expect(result.id).toBeDefined();
52
54
  expect(result.version).toBe(1);
@@ -59,12 +61,12 @@ describe("Channel Repository", () => {
59
61
  expect(result.custom).toBeUndefined();
60
62
 
61
63
  // Test that the channel is stored
62
- const items = repository.query(ctx);
64
+ const items = await repository.query(ctx);
63
65
  expect(items.count).toBe(1);
64
66
  expect(items.results[0].id).toBe(result.id);
65
67
  });
66
68
 
67
- test("create channel with all optional fields", () => {
69
+ test("create channel with all optional fields", async () => {
68
70
  const draft: ChannelDraft = {
69
71
  key: "store-berlin",
70
72
  name: { "en-US": "Berlin Store", "de-DE": "Berlin Geschäft" },
@@ -93,7 +95,7 @@ describe("Channel Repository", () => {
93
95
  };
94
96
 
95
97
  const ctx = { projectKey: "dummy" };
96
- const result = repository.create(ctx, draft);
98
+ const result = await repository.create(ctx, draft);
97
99
 
98
100
  expect(result.id).toBeDefined();
99
101
  expect(result.key).toBe(draft.key);
@@ -104,16 +106,16 @@ describe("Channel Repository", () => {
104
106
  expect(result.custom?.fields.description).toBe("Custom description");
105
107
  });
106
108
 
107
- test("update channel - changeName", () => {
109
+ test("update channel - changeName", async () => {
108
110
  const draft: ChannelDraft = {
109
111
  key: "test-channel",
110
112
  name: { "en-US": "Test Channel" },
111
113
  };
112
114
 
113
115
  const ctx = { projectKey: "dummy" };
114
- const channel = repository.create(ctx, draft);
116
+ const channel = await repository.create(ctx, draft);
115
117
 
116
- const result = repository.processUpdateActions(
118
+ const result = await repository.processUpdateActions(
117
119
  ctx,
118
120
  channel,
119
121
  channel.version,
@@ -129,16 +131,16 @@ describe("Channel Repository", () => {
129
131
  expect(result.version).toBe(channel.version + 1);
130
132
  });
131
133
 
132
- test("update channel - changeKey", () => {
134
+ test("update channel - changeKey", async () => {
133
135
  const draft: ChannelDraft = {
134
136
  key: "test-channel-2",
135
137
  name: { "en-US": "Test Channel 2" },
136
138
  };
137
139
 
138
140
  const ctx = { projectKey: "dummy" };
139
- const channel = repository.create(ctx, draft);
141
+ const channel = await repository.create(ctx, draft);
140
142
 
141
- const result = repository.processUpdateActions(
143
+ const result = await repository.processUpdateActions(
142
144
  ctx,
143
145
  channel,
144
146
  channel.version,
@@ -154,16 +156,16 @@ describe("Channel Repository", () => {
154
156
  expect(result.version).toBe(channel.version + 1);
155
157
  });
156
158
 
157
- test("update channel - changeDescription", () => {
159
+ test("update channel - changeDescription", async () => {
158
160
  const draft: ChannelDraft = {
159
161
  key: "test-channel-3",
160
162
  name: { "en-US": "Test Channel 3" },
161
163
  };
162
164
 
163
165
  const ctx = { projectKey: "dummy" };
164
- const channel = repository.create(ctx, draft);
166
+ const channel = await repository.create(ctx, draft);
165
167
 
166
- const result = repository.processUpdateActions(
168
+ const result = await repository.processUpdateActions(
167
169
  ctx,
168
170
  channel,
169
171
  channel.version,
@@ -179,16 +181,16 @@ describe("Channel Repository", () => {
179
181
  expect(result.version).toBe(channel.version + 1);
180
182
  });
181
183
 
182
- test("update channel - setAddress", () => {
184
+ test("update channel - setAddress", async () => {
183
185
  const draft: ChannelDraft = {
184
186
  key: "test-channel-4",
185
187
  name: { "en-US": "Test Channel 4" },
186
188
  };
187
189
 
188
190
  const ctx = { projectKey: "dummy" };
189
- const channel = repository.create(ctx, draft);
191
+ const channel = await repository.create(ctx, draft);
190
192
 
191
- const result = repository.processUpdateActions(
193
+ const result = await repository.processUpdateActions(
192
194
  ctx,
193
195
  channel,
194
196
  channel.version,
@@ -211,16 +213,16 @@ describe("Channel Repository", () => {
211
213
  expect(result.version).toBe(channel.version + 1);
212
214
  });
213
215
 
214
- test("update channel - setGeoLocation", () => {
216
+ test("update channel - setGeoLocation", async () => {
215
217
  const draft: ChannelDraft = {
216
218
  key: "test-channel-5",
217
219
  name: { "en-US": "Test Channel 5" },
218
220
  };
219
221
 
220
222
  const ctx = { projectKey: "dummy" };
221
- const channel = repository.create(ctx, draft);
223
+ const channel = await repository.create(ctx, draft);
222
224
 
223
- const result = repository.processUpdateActions(
225
+ const result = await repository.processUpdateActions(
224
226
  ctx,
225
227
  channel,
226
228
  channel.version,
@@ -242,17 +244,17 @@ describe("Channel Repository", () => {
242
244
  expect(result.version).toBe(channel.version + 1);
243
245
  });
244
246
 
245
- test("update channel - setCustomType", () => {
247
+ test("update channel - setCustomType", async () => {
246
248
  const draft: ChannelDraft = {
247
249
  key: "test-channel-6",
248
250
  name: { "en-US": "Test Channel 6" },
249
251
  };
250
252
 
251
253
  const ctx = { projectKey: "dummy" };
252
- const channel = repository.create(ctx, draft);
254
+ const channel = await repository.create(ctx, draft);
253
255
 
254
256
  // Set custom type
255
- const result = repository.processUpdateActions(
257
+ const result = await repository.processUpdateActions(
256
258
  ctx,
257
259
  channel,
258
260
  channel.version,
@@ -275,7 +277,7 @@ describe("Channel Repository", () => {
275
277
  expect(result.version).toBe(channel.version + 1);
276
278
 
277
279
  // Remove custom type
278
- const result2 = repository.processUpdateActions(
280
+ const result2 = await repository.processUpdateActions(
279
281
  ctx,
280
282
  result,
281
283
  result.version,
@@ -290,7 +292,7 @@ describe("Channel Repository", () => {
290
292
  expect(result2.version).toBe(result.version + 1);
291
293
  });
292
294
 
293
- test("update channel - setCustomField", () => {
295
+ test("update channel - setCustomField", async () => {
294
296
  const draft: ChannelDraft = {
295
297
  key: "test-channel-7",
296
298
  name: { "en-US": "Test Channel 7" },
@@ -306,10 +308,10 @@ describe("Channel Repository", () => {
306
308
  };
307
309
 
308
310
  const ctx = { projectKey: "dummy" };
309
- const channel = repository.create(ctx, draft);
311
+ const channel = await repository.create(ctx, draft);
310
312
 
311
313
  // Update custom field
312
- const result = repository.processUpdateActions(
314
+ const result = await repository.processUpdateActions(
313
315
  ctx,
314
316
  channel,
315
317
  channel.version,
@@ -326,7 +328,7 @@ describe("Channel Repository", () => {
326
328
  expect(result.version).toBe(channel.version + 1);
327
329
 
328
330
  // Remove custom field
329
- const result2 = repository.processUpdateActions(
331
+ const result2 = await repository.processUpdateActions(
330
332
  ctx,
331
333
  result,
332
334
  result.version,
@@ -343,32 +345,32 @@ describe("Channel Repository", () => {
343
345
  expect(result2.version).toBe(result.version + 1);
344
346
  });
345
347
 
346
- test("get and delete channel", () => {
348
+ test("get and delete channel", async () => {
347
349
  const draft: ChannelDraft = {
348
350
  key: "delete-test",
349
351
  name: { "en-US": "Delete Test Channel" },
350
352
  };
351
353
 
352
354
  const ctx = { projectKey: "dummy" };
353
- const channel = repository.create(ctx, draft);
355
+ const channel = await repository.create(ctx, draft);
354
356
 
355
357
  // Test get
356
- const retrieved = repository.get(ctx, channel.id);
358
+ const retrieved = await repository.get(ctx, channel.id);
357
359
  expect(retrieved).toBeDefined();
358
360
  expect(retrieved?.id).toBe(channel.id);
359
361
 
360
362
  // Test getByKey
361
- const retrievedByKey = repository.getByKey(ctx, channel.key!);
363
+ const retrievedByKey = await repository.getByKey(ctx, channel.key!);
362
364
  expect(retrievedByKey).toBeDefined();
363
365
  expect(retrievedByKey?.key).toBe(channel.key);
364
366
 
365
367
  // Test delete
366
- const deleted = repository.delete(ctx, channel.id);
368
+ const deleted = await repository.delete(ctx, channel.id);
367
369
  expect(deleted).toBeDefined();
368
370
  expect(deleted?.id).toBe(channel.id);
369
371
 
370
372
  // Verify it's deleted
371
- const notFound = repository.get(ctx, channel.id);
373
+ const notFound = await repository.get(ctx, channel.id);
372
374
  expect(notFound).toBeNull();
373
375
  });
374
376
  });
@@ -11,6 +11,7 @@ import type {
11
11
  ChannelUpdateAction,
12
12
  } from "@commercetools/platform-sdk";
13
13
  import type { Config } from "#src/config.ts";
14
+ import { ChannelDraftSchema } from "#src/schemas/generated/channel.ts";
14
15
  import { getBaseResourceProperties } from "../helpers.ts";
15
16
  import type { Writable } from "../types.ts";
16
17
  import type { UpdateHandlerInterface } from "./abstract.ts";
@@ -25,24 +26,28 @@ export class ChannelRepository extends AbstractResourceRepository<"channel"> {
25
26
  constructor(config: Config) {
26
27
  super("channel", config);
27
28
  this.actions = new ChannelUpdateHandler(this._storage);
29
+ this.draftSchema = ChannelDraftSchema;
28
30
  }
29
31
 
30
- create(context: RepositoryContext, draft: ChannelDraft): Channel {
32
+ async create(
33
+ context: RepositoryContext,
34
+ draft: ChannelDraft,
35
+ ): Promise<Channel> {
31
36
  const resource: Channel = {
32
- ...getBaseResourceProperties(),
37
+ ...getBaseResourceProperties(context.clientId),
33
38
  key: draft.key,
34
39
  name: draft.name,
35
40
  description: draft.description,
36
41
  roles: draft.roles || [],
37
42
  geoLocation: draft.geoLocation,
38
43
  address: createAddress(draft.address, context.projectKey, this._storage),
39
- custom: createCustomFields(
44
+ custom: await createCustomFields(
40
45
  draft.custom,
41
46
  context.projectKey,
42
47
  this._storage,
43
48
  ),
44
49
  };
45
- return this.saveNew(context, resource);
50
+ return await this.saveNew(context, resource);
46
51
  }
47
52
  }
48
53
 
@@ -91,30 +96,15 @@ class ChannelUpdateHandler
91
96
  resource: Writable<Channel>,
92
97
  { name, value }: ChannelSetCustomFieldAction,
93
98
  ) {
94
- if (!resource.custom) {
95
- return;
96
- }
97
- if (value === null) {
98
- delete resource.custom.fields[name];
99
- } else {
100
- resource.custom.fields[name] = value;
101
- }
99
+ this._setCustomFieldValues(resource, { name, value });
102
100
  }
103
101
 
104
- setCustomType(
102
+ async setCustomType(
105
103
  context: RepositoryContext,
106
104
  resource: Writable<Channel>,
107
105
  { type, fields }: ChannelSetCustomTypeAction,
108
106
  ) {
109
- if (type) {
110
- resource.custom = createCustomFields(
111
- { type, fields },
112
- context.projectKey,
113
- this._storage,
114
- );
115
- } else {
116
- resource.custom = undefined;
117
- }
107
+ await this._setCustomType(context, resource, { type, fields });
118
108
  }
119
109
 
120
110
  setGeoLocation(
@@ -5,7 +5,8 @@ import type {
5
5
  } from "@commercetools/platform-sdk";
6
6
  import type { Config } from "#src/config.ts";
7
7
  import { CommercetoolsError } from "#src/exceptions.ts";
8
- import { cloneObject, getBaseResourceProperties } from "../helpers.ts";
8
+ import { CustomObjectDraftSchema } from "#src/schemas/generated/custom-object.ts";
9
+ import { getBaseResourceProperties } from "../helpers.ts";
9
10
  import type { Writable } from "../types.ts";
10
11
  import type { QueryParams } from "./abstract.ts";
11
12
  import {
@@ -17,17 +18,18 @@ import { checkConcurrentModification } from "./errors.ts";
17
18
  export class CustomObjectRepository extends AbstractResourceRepository<"key-value-document"> {
18
19
  constructor(config: Config) {
19
20
  super("key-value-document", config);
21
+ this.draftSchema = CustomObjectDraftSchema;
20
22
  }
21
23
 
22
- create(
24
+ async create(
23
25
  context: RepositoryContext,
24
26
  draft: Writable<CustomObjectDraft>,
25
- ): CustomObject {
26
- const current = this.getWithContainerAndKey(
27
+ ): Promise<CustomObject> {
28
+ const current = (await this.getWithContainerAndKey(
27
29
  context,
28
30
  draft.container,
29
31
  draft.key,
30
- ) as Writable<CustomObject | undefined>;
32
+ )) as Writable<CustomObject | undefined>;
31
33
 
32
34
  if (current) {
33
35
  // Only check version if it is passed in the draft
@@ -38,11 +40,11 @@ export class CustomObjectRepository extends AbstractResourceRepository<"key-valu
38
40
  }
39
41
 
40
42
  if (draft.value !== current.value) {
41
- const updated = cloneObject(current) as Writable<CustomObject>;
42
- updated.value = draft.value;
43
- updated.version += 1;
44
- this.saveUpdate(context, draft.version, updated);
45
- return updated;
43
+ // current is already a clone from storage retrieval, safe to mutate
44
+ current.value = draft.value;
45
+ current.version += 1;
46
+ await this.saveUpdate(context, draft.version, current);
47
+ return current;
46
48
  }
47
49
  return current;
48
50
  }
@@ -56,7 +58,7 @@ export class CustomObjectRepository extends AbstractResourceRepository<"key-valu
56
58
  400,
57
59
  );
58
60
  }
59
- const baseProperties = getBaseResourceProperties();
61
+ const baseProperties = getBaseResourceProperties(context.clientId);
60
62
  const resource: CustomObject = {
61
63
  ...baseProperties,
62
64
  container: draft.container,
@@ -64,38 +66,45 @@ export class CustomObjectRepository extends AbstractResourceRepository<"key-valu
64
66
  value: draft.value,
65
67
  };
66
68
 
67
- this.saveNew(context, resource);
69
+ await this.saveNew(context, resource);
68
70
  return resource;
69
71
  }
70
72
 
71
- getWithContainerAndKey(
73
+ async getWithContainerAndKey(
72
74
  context: RepositoryContext,
73
75
  container: string,
74
76
  key: string,
75
77
  ) {
76
- const items = this._storage.all(context.projectKey, this.getTypeId());
77
- return items.find(
78
- (item) => item.container === container && item.key === key,
78
+ return this._storage.getByContainerAndKey(
79
+ context.projectKey,
80
+ container,
81
+ key,
79
82
  );
80
83
  }
81
84
 
82
- queryWithContainer(
85
+ async queryWithContainer(
83
86
  context: RepositoryContext,
84
87
  container: string,
85
88
  params: QueryParams = {},
86
89
  ) {
87
90
  const whereClause = params.where || [];
88
91
  whereClause.push(`container="${container}"`);
89
- const result = this._storage.query(context.projectKey, this.getTypeId(), {
90
- ...params,
91
- where: whereClause,
92
- });
92
+ const result = await this._storage.query(
93
+ context.projectKey,
94
+ this.getTypeId(),
95
+ {
96
+ ...params,
97
+ where: whereClause,
98
+ },
99
+ );
93
100
 
94
101
  // @ts-expect-error
95
- result.results = result.results.map((r) =>
96
- this.postProcessResource(context, r as CustomObject, {
97
- expand: params.expand,
98
- }),
102
+ result.results = await Promise.all(
103
+ result.results.map((r) =>
104
+ this.postProcessResource(context, r as CustomObject, {
105
+ expand: params.expand,
106
+ }),
107
+ ),
99
108
  );
100
109
  return result;
101
110
  }
@@ -45,7 +45,7 @@ import { hashPassword } from "#src/lib/password.ts";
45
45
  import type { Writable } from "#src/types.ts";
46
46
  import type { UpdateHandlerInterface } from "../abstract.ts";
47
47
  import { AbstractUpdateHandler, type RepositoryContext } from "../abstract.ts";
48
- import { createAddress, createCustomFields } from "../helpers.ts";
48
+ import { createAddress } from "../helpers.ts";
49
49
 
50
50
  export class CustomerUpdateHandler
51
51
  extends AbstractUpdateHandler
@@ -94,7 +94,6 @@ export class CustomerUpdateHandler
94
94
  if (!resource.shippingAddressIds.includes(address.id)) {
95
95
  resource.shippingAddressIds.push(address.id);
96
96
  }
97
- return resource;
98
97
  }
99
98
 
100
99
  addStore(
@@ -102,7 +101,13 @@ export class CustomerUpdateHandler
102
101
  resource: Writable<Customer>,
103
102
  action: CustomerAddStoreAction,
104
103
  ) {
105
- throw new Error("Method not implemented.");
104
+ throw new CommercetoolsError<InvalidOperationError>(
105
+ {
106
+ code: "InvalidOperation",
107
+ message: "The action 'addStore' is not implemented yet",
108
+ },
109
+ 400,
110
+ );
106
111
  }
107
112
 
108
113
  changeAddress(
@@ -214,7 +219,13 @@ export class CustomerUpdateHandler
214
219
  resource: Writable<Customer>,
215
220
  action: CustomerRemoveStoreAction,
216
221
  ) {
217
- throw new Error("Method not implemented.");
222
+ throw new CommercetoolsError<InvalidOperationError>(
223
+ {
224
+ code: "InvalidOperation",
225
+ message: "The action 'removeStore' is not implemented yet",
226
+ },
227
+ 400,
228
+ );
218
229
  }
219
230
 
220
231
  setAddressCustomField(
@@ -222,7 +233,13 @@ export class CustomerUpdateHandler
222
233
  resource: Writable<Customer>,
223
234
  action: CustomerSetAddressCustomFieldAction,
224
235
  ) {
225
- throw new Error("Method not implemented.");
236
+ throw new CommercetoolsError<InvalidOperationError>(
237
+ {
238
+ code: "InvalidOperation",
239
+ message: "The action 'setAddressCustomField' is not implemented yet",
240
+ },
241
+ 400,
242
+ );
226
243
  }
227
244
 
228
245
  setAddressCustomType(
@@ -230,7 +247,13 @@ export class CustomerUpdateHandler
230
247
  resource: Writable<Customer>,
231
248
  action: CustomerSetAddressCustomTypeAction,
232
249
  ) {
233
- throw new Error("Method not implemented.");
250
+ throw new CommercetoolsError<InvalidOperationError>(
251
+ {
252
+ code: "InvalidOperation",
253
+ message: "The action 'setAddressCustomType' is not implemented yet",
254
+ },
255
+ 400,
256
+ );
234
257
  }
235
258
 
236
259
  setAuthenticationMode(
@@ -274,7 +297,7 @@ export class CustomerUpdateHandler
274
297
  resource.companyName = companyName;
275
298
  }
276
299
 
277
- setCustomerGroup(
300
+ async setCustomerGroup(
278
301
  context: RepositoryContext,
279
302
  resource: Writable<Customer>,
280
303
  action: CustomerSetCustomerGroupAction,
@@ -289,7 +312,7 @@ export class CustomerUpdateHandler
289
312
  );
290
313
  }
291
314
 
292
- const group = this._storage.getByResourceIdentifier<"customer-group">(
315
+ const group = await this._storage.getByResourceIdentifier<"customer-group">(
293
316
  context.projectKey,
294
317
  action.customerGroup,
295
318
  );
@@ -306,8 +329,12 @@ export class CustomerUpdateHandler
306
329
  { customerNumber }: CustomerSetCustomerNumberAction,
307
330
  ) {
308
331
  if (resource.customerNumber) {
309
- throw new Error(
310
- "A Customer number already exists and cannot be set again.",
332
+ throw new CommercetoolsError<InvalidOperationError>(
333
+ {
334
+ code: "InvalidOperation",
335
+ message: "A Customer number already exists and cannot be set again.",
336
+ },
337
+ 400,
311
338
  );
312
339
  }
313
340
  resource.customerNumber = customerNumber;
@@ -318,26 +345,15 @@ export class CustomerUpdateHandler
318
345
  resource: Writable<Customer>,
319
346
  { name, value }: CustomerSetCustomFieldAction,
320
347
  ) {
321
- if (!resource.custom) {
322
- throw new Error("Resource has no custom field");
323
- }
324
- resource.custom.fields[name] = value;
348
+ this._setCustomFieldValues(resource, { name, value });
325
349
  }
326
350
 
327
- setCustomType(
351
+ async setCustomType(
328
352
  context: RepositoryContext,
329
353
  resource: Writable<Customer>,
330
354
  { type, fields }: CustomerSetCustomTypeAction,
331
355
  ) {
332
- if (type) {
333
- resource.custom = createCustomFields(
334
- { type, fields },
335
- context.projectKey,
336
- this._storage,
337
- );
338
- } else {
339
- resource.custom = undefined;
340
- }
356
+ await this._setCustomType(context, resource, { type, fields });
341
357
  }
342
358
 
343
359
  setDateOfBirth(
@@ -453,7 +469,13 @@ export class CustomerUpdateHandler
453
469
  resource: Writable<Customer>,
454
470
  action: CustomerSetStoresAction,
455
471
  ) {
456
- throw new Error("Method not implemented.");
472
+ throw new CommercetoolsError<InvalidOperationError>(
473
+ {
474
+ code: "InvalidOperation",
475
+ message: "The action 'setStores' is not implemented yet",
476
+ },
477
+ 400,
478
+ );
457
479
  }
458
480
 
459
481
  setTitle(