@labdigital/commercetools-mock 2.51.0 → 2.53.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 (42) hide show
  1. package/dist/index.d.ts +34 -23
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +140 -13
  4. package/dist/index.js.map +1 -1
  5. package/package.json +1 -1
  6. package/src/lib/product-review-statistics.test.ts +349 -0
  7. package/src/lib/productSearchFilter.test.ts +77 -0
  8. package/src/lib/review-statistics.ts +58 -0
  9. package/src/product-projection-search.ts +17 -2
  10. package/src/product-search-availability.test.ts +242 -0
  11. package/src/product-search.ts +22 -4
  12. package/src/repositories/as-associate.test.ts +126 -0
  13. package/src/repositories/attribute-group.test.ts +221 -0
  14. package/src/repositories/business-unit.test.ts +425 -0
  15. package/src/repositories/business-unit.ts +57 -1
  16. package/src/repositories/channel.test.ts +374 -0
  17. package/src/repositories/customer-group.test.ts +262 -0
  18. package/src/repositories/extension.test.ts +306 -0
  19. package/src/repositories/index.test.ts +17 -0
  20. package/src/repositories/product/index.ts +22 -1
  21. package/src/repositories/product-projection.ts +8 -2
  22. package/src/repositories/review.test.ts +636 -0
  23. package/src/repositories/review.ts +145 -4
  24. package/src/repositories/subscription.test.ts +207 -0
  25. package/src/repositories/zone.test.ts +278 -0
  26. package/src/services/as-associate-cart.test.ts +58 -0
  27. package/src/services/as-associate.test.ts +34 -0
  28. package/src/services/attribute-group.test.ts +114 -0
  29. package/src/services/channel.test.ts +90 -0
  30. package/src/services/customer-group.test.ts +85 -0
  31. package/src/services/discount-code.test.ts +120 -0
  32. package/src/services/extension.test.ts +130 -0
  33. package/src/services/my-business-unit.test.ts +113 -0
  34. package/src/services/my-business-unit.ts +6 -0
  35. package/src/services/my-customer.test.ts +24 -0
  36. package/src/services/order.test.ts +18 -0
  37. package/src/services/product-discount.test.ts +146 -0
  38. package/src/services/project.test.ts +17 -0
  39. package/src/services/reviews.test.ts +230 -0
  40. package/src/services/subscription.test.ts +151 -0
  41. package/src/services/type.test.ts +127 -0
  42. package/src/services/zone.test.ts +117 -0
@@ -0,0 +1,425 @@
1
+ import type {
2
+ BusinessUnitAddShippingAddressIdAction,
3
+ BusinessUnitChangeNameAction,
4
+ BusinessUnitChangeStatusAction,
5
+ BusinessUnitRemoveAddressAction,
6
+ BusinessUnitSetContactEmailAction,
7
+ BusinessUnitSetDefaultShippingAddressAction,
8
+ CompanyDraft,
9
+ DivisionDraft,
10
+ } from "@commercetools/platform-sdk";
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 { BusinessUnitRepository } from "./business-unit";
16
+ import { CustomerRepository } from "./customer";
17
+
18
+ describe("BusinessUnit Repository", () => {
19
+ const storage = new InMemoryStorage();
20
+ const config: Config = { storage, strict: false };
21
+ const repository = new BusinessUnitRepository(config);
22
+
23
+ // Add required dependencies for testing
24
+ storage.add("dummy", "store", {
25
+ ...getBaseResourceProperties(),
26
+ id: "store-123",
27
+ key: "test-store",
28
+ name: { "en-US": "Test Store" },
29
+ languages: ["en-US"],
30
+ countries: [{ code: "US" }],
31
+ distributionChannels: [],
32
+ supplyChannels: [],
33
+ productSelections: [],
34
+ });
35
+
36
+ // Create a proper customer using the customer repository
37
+ const customerRepository = new CustomerRepository(config);
38
+ const customer = customerRepository.create(
39
+ { projectKey: "dummy" },
40
+ {
41
+ email: "associate@example.com",
42
+ password: "password123",
43
+ firstName: "John",
44
+ lastName: "Associate",
45
+ },
46
+ );
47
+
48
+ test("create company business unit", () => {
49
+ const draft: CompanyDraft = {
50
+ key: "test-company",
51
+ unitType: "Company",
52
+ status: "Active",
53
+ name: "Test Company Inc.",
54
+ contactEmail: "contact@testcompany.com",
55
+ storeMode: "Explicit",
56
+ associateMode: "Explicit",
57
+ approvalRuleMode: "ExplicitApproval",
58
+ stores: [
59
+ {
60
+ typeId: "store",
61
+ key: "test-store",
62
+ },
63
+ ],
64
+ addresses: [
65
+ {
66
+ country: "US",
67
+ city: "New York",
68
+ streetName: "5th Avenue",
69
+ streetNumber: "123",
70
+ postalCode: "10001",
71
+ },
72
+ ],
73
+ defaultBillingAddress: 0,
74
+ defaultShippingAddress: 0,
75
+ billingAddresses: [0],
76
+ shippingAddresses: [0],
77
+ associates: [
78
+ {
79
+ customer: {
80
+ typeId: "customer",
81
+ id: customer.id,
82
+ },
83
+ associateRoleAssignments: [
84
+ {
85
+ associateRole: {
86
+ typeId: "associate-role",
87
+ key: "admin",
88
+ },
89
+ inheritance: "Enabled",
90
+ },
91
+ ],
92
+ },
93
+ ],
94
+ };
95
+
96
+ const ctx = { projectKey: "dummy" };
97
+ const result = repository.create(ctx, draft);
98
+
99
+ expect(result.id).toBeDefined();
100
+ expect(result.version).toBe(1);
101
+ expect(result.key).toBe(draft.key);
102
+ expect(result.unitType).toBe("Company");
103
+ expect(result.name).toBe(draft.name);
104
+ expect(result.contactEmail).toBe(draft.contactEmail);
105
+ expect(result.status).toBe(draft.status);
106
+ expect(result.storeMode).toBe(draft.storeMode);
107
+ expect(result.associateMode).toBe(draft.associateMode);
108
+ expect(result.approvalRuleMode).toBe(draft.approvalRuleMode);
109
+ expect(result.addresses).toHaveLength(1);
110
+ expect(result.addresses[0].country).toBe("US");
111
+ expect(result.stores).toHaveLength(1);
112
+ expect(result.associates).toHaveLength(1);
113
+
114
+ // Test that the business unit is stored
115
+ const items = repository.query(ctx);
116
+ expect(items.count).toBe(1);
117
+ expect(items.results[0].id).toBe(result.id);
118
+ });
119
+
120
+ test("create division business unit", () => {
121
+ // First create a company to be the parent
122
+ const companyDraft: CompanyDraft = {
123
+ key: "parent-company",
124
+ unitType: "Company",
125
+ status: "Active",
126
+ name: "Parent Company",
127
+ };
128
+
129
+ const company = repository.create({ projectKey: "dummy" }, companyDraft);
130
+
131
+ const draft: DivisionDraft = {
132
+ key: "test-division",
133
+ unitType: "Division",
134
+ status: "Active",
135
+ name: "Test Division",
136
+ contactEmail: "division@testcompany.com",
137
+ parentUnit: {
138
+ typeId: "business-unit",
139
+ key: "parent-company",
140
+ },
141
+ };
142
+
143
+ const ctx = { projectKey: "dummy" };
144
+ const result = repository.create(ctx, draft);
145
+
146
+ expect(result.id).toBeDefined();
147
+ expect(result.key).toBe(draft.key);
148
+ expect(result.unitType).toBe("Division");
149
+ expect(result.name).toBe(draft.name);
150
+ expect(result.contactEmail).toBe(draft.contactEmail);
151
+
152
+ // Check division-specific properties
153
+ if (result.unitType === "Division") {
154
+ expect(result.parentUnit?.key).toBe("parent-company");
155
+ }
156
+ });
157
+
158
+ test("create business unit with minimal data", () => {
159
+ const draft: CompanyDraft = {
160
+ key: "minimal-company",
161
+ unitType: "Company",
162
+ name: "Minimal Company",
163
+ };
164
+
165
+ const ctx = { projectKey: "dummy" };
166
+ const result = repository.create(ctx, draft);
167
+
168
+ expect(result.id).toBeDefined();
169
+ expect(result.key).toBe(draft.key);
170
+ expect(result.name).toBe(draft.name);
171
+ expect(result.unitType).toBe("Company");
172
+ expect(result.addresses).toEqual([]);
173
+ expect(result.associates).toEqual([]);
174
+ expect(result.stores).toBeUndefined();
175
+ });
176
+
177
+ test("update business unit - changeName", () => {
178
+ const draft: CompanyDraft = {
179
+ key: "update-test-company",
180
+ unitType: "Company",
181
+ name: "Original Company Name",
182
+ };
183
+
184
+ const ctx = { projectKey: "dummy" };
185
+ const businessUnit = repository.create(ctx, draft);
186
+
187
+ const result = repository.processUpdateActions(
188
+ ctx,
189
+ businessUnit,
190
+ businessUnit.version,
191
+ [
192
+ {
193
+ action: "changeName",
194
+ name: "Updated Company Name",
195
+ } as BusinessUnitChangeNameAction,
196
+ ],
197
+ );
198
+
199
+ expect(result.name).toBe("Updated Company Name");
200
+ expect(result.version).toBe(businessUnit.version + 1);
201
+ });
202
+
203
+ test("update business unit - setContactEmail", () => {
204
+ const draft: CompanyDraft = {
205
+ key: "email-test-company",
206
+ unitType: "Company",
207
+ name: "Email Test Company",
208
+ };
209
+
210
+ const ctx = { projectKey: "dummy" };
211
+ const businessUnit = repository.create(ctx, draft);
212
+
213
+ const result = repository.processUpdateActions(
214
+ ctx,
215
+ businessUnit,
216
+ businessUnit.version,
217
+ [
218
+ {
219
+ action: "setContactEmail",
220
+ contactEmail: "newemail@company.com",
221
+ } as BusinessUnitSetContactEmailAction,
222
+ ],
223
+ );
224
+
225
+ expect(result.contactEmail).toBe("newemail@company.com");
226
+ expect(result.version).toBe(businessUnit.version + 1);
227
+ });
228
+
229
+ test("update business unit - changeStatus", () => {
230
+ const draft: CompanyDraft = {
231
+ key: "status-test-company",
232
+ unitType: "Company",
233
+ name: "Status Test Company",
234
+ status: "Active",
235
+ };
236
+
237
+ const ctx = { projectKey: "dummy" };
238
+ const businessUnit = repository.create(ctx, draft);
239
+
240
+ const result = repository.processUpdateActions(
241
+ ctx,
242
+ businessUnit,
243
+ businessUnit.version,
244
+ [
245
+ {
246
+ action: "changeStatus",
247
+ status: "Inactive",
248
+ } as BusinessUnitChangeStatusAction,
249
+ ],
250
+ );
251
+
252
+ expect(result.status).toBe("Inactive");
253
+ expect(result.version).toBe(businessUnit.version + 1);
254
+ });
255
+
256
+ test("update business unit - setDefaultShippingAddress", () => {
257
+ const draft: CompanyDraft = {
258
+ key: "default-shipping-company",
259
+ unitType: "Company",
260
+ name: "Default Shipping Company",
261
+ addresses: [
262
+ {
263
+ country: "US",
264
+ city: "New York",
265
+ streetName: "5th Avenue",
266
+ streetNumber: "123",
267
+ postalCode: "10001",
268
+ },
269
+ {
270
+ country: "US",
271
+ city: "Boston",
272
+ streetName: "Boylston",
273
+ streetNumber: "456",
274
+ postalCode: "02116",
275
+ },
276
+ ],
277
+ shippingAddresses: [0, 1],
278
+ };
279
+
280
+ const ctx = { projectKey: "dummy" };
281
+ const businessUnit = repository.create(ctx, draft);
282
+ const addressId = businessUnit.addresses[1].id;
283
+
284
+ const result = repository.processUpdateActions(
285
+ ctx,
286
+ businessUnit,
287
+ businessUnit.version,
288
+ [
289
+ {
290
+ action: "setDefaultShippingAddress",
291
+ addressId,
292
+ } as BusinessUnitSetDefaultShippingAddressAction,
293
+ ],
294
+ );
295
+
296
+ expect(result.defaultShippingAddressId).toBe(addressId);
297
+ expect(result.version).toBe(businessUnit.version + 1);
298
+ });
299
+
300
+ test("update business unit - addShippingAddressId", () => {
301
+ const draft: CompanyDraft = {
302
+ key: "add-shipping-address-company",
303
+ unitType: "Company",
304
+ name: "Add Shipping Address Company",
305
+ addresses: [
306
+ {
307
+ country: "US",
308
+ city: "New York",
309
+ streetName: "5th Avenue",
310
+ streetNumber: "123",
311
+ postalCode: "10001",
312
+ },
313
+ {
314
+ country: "US",
315
+ city: "Boston",
316
+ streetName: "Boylston",
317
+ streetNumber: "456",
318
+ postalCode: "02116",
319
+ },
320
+ ],
321
+ };
322
+
323
+ const ctx = { projectKey: "dummy" };
324
+ const businessUnit = repository.create(ctx, draft);
325
+ const addressId = businessUnit.addresses[1].id;
326
+
327
+ const result = repository.processUpdateActions(
328
+ ctx,
329
+ businessUnit,
330
+ businessUnit.version,
331
+ [
332
+ {
333
+ action: "addShippingAddressId",
334
+ addressId,
335
+ } as BusinessUnitAddShippingAddressIdAction,
336
+ ],
337
+ );
338
+
339
+ expect(result.shippingAddressIds).toContain(addressId);
340
+ expect(result.version).toBe(businessUnit.version + 1);
341
+ });
342
+
343
+ test("update business unit - removeAddress", () => {
344
+ const draft: CompanyDraft = {
345
+ key: "remove-address-company",
346
+ unitType: "Company",
347
+ name: "Remove Address Company",
348
+ addresses: [
349
+ {
350
+ country: "US",
351
+ city: "New York",
352
+ streetName: "5th Avenue",
353
+ streetNumber: "123",
354
+ postalCode: "10001",
355
+ },
356
+ {
357
+ country: "US",
358
+ city: "Boston",
359
+ streetName: "Boylston",
360
+ streetNumber: "456",
361
+ postalCode: "02116",
362
+ },
363
+ ],
364
+ billingAddresses: [0, 1],
365
+ shippingAddresses: [0, 1],
366
+ defaultBillingAddress: 0,
367
+ defaultShippingAddress: 1,
368
+ };
369
+
370
+ const ctx = { projectKey: "dummy" };
371
+ const businessUnit = repository.create(ctx, draft);
372
+ const addressIdToRemove = businessUnit.addresses[0].id;
373
+ const remainingAddressId = businessUnit.addresses[1].id;
374
+
375
+ const result = repository.processUpdateActions(
376
+ ctx,
377
+ businessUnit,
378
+ businessUnit.version,
379
+ [
380
+ {
381
+ action: "removeAddress",
382
+ addressId: addressIdToRemove,
383
+ } as BusinessUnitRemoveAddressAction,
384
+ ],
385
+ );
386
+
387
+ expect(result.addresses).toHaveLength(1);
388
+ expect(result.addresses[0].id).toBe(remainingAddressId);
389
+ expect(result.billingAddressIds).toEqual([remainingAddressId]);
390
+ expect(result.shippingAddressIds).toEqual([remainingAddressId]);
391
+ expect(result.defaultBillingAddressId).toBeUndefined();
392
+ expect(result.defaultShippingAddressId).toBe(remainingAddressId);
393
+ expect(result.version).toBe(businessUnit.version + 1);
394
+ });
395
+
396
+ test("get and delete business unit", () => {
397
+ const draft: CompanyDraft = {
398
+ key: "delete-test",
399
+ unitType: "Company",
400
+ name: "Delete Test Company",
401
+ };
402
+
403
+ const ctx = { projectKey: "dummy" };
404
+ const businessUnit = repository.create(ctx, draft);
405
+
406
+ // Test get
407
+ const retrieved = repository.get(ctx, businessUnit.id);
408
+ expect(retrieved).toBeDefined();
409
+ expect(retrieved?.id).toBe(businessUnit.id);
410
+
411
+ // Test getByKey
412
+ const retrievedByKey = repository.getByKey(ctx, businessUnit.key!);
413
+ expect(retrievedByKey).toBeDefined();
414
+ expect(retrievedByKey?.key).toBe(businessUnit.key);
415
+
416
+ // Test delete
417
+ const deleted = repository.delete(ctx, businessUnit.id);
418
+ expect(deleted).toBeDefined();
419
+ expect(deleted?.id).toBe(businessUnit.id);
420
+
421
+ // Verify it's deleted
422
+ const notFound = repository.get(ctx, businessUnit.id);
423
+ expect(notFound).toBeNull();
424
+ });
425
+ });
@@ -1,8 +1,10 @@
1
1
  import type {
2
+ BusinessUnitAddShippingAddressIdAction,
2
3
  BusinessUnitChangeApprovalRuleModeAction,
3
4
  BusinessUnitChangeAssociateModeAction,
4
5
  BusinessUnitChangeStatusAction,
5
6
  BusinessUnitSetCustomTypeAction,
7
+ BusinessUnitSetDefaultShippingAddressAction,
6
8
  BusinessUnitUpdateAction,
7
9
  CompanyDraft,
8
10
  DivisionDraft,
@@ -17,6 +19,7 @@ import type {
17
19
  BusinessUnitChangeNameAction,
18
20
  BusinessUnitChangeParentUnitAction,
19
21
  BusinessUnitDraft,
22
+ BusinessUnitRemoveAddressAction,
20
23
  BusinessUnitSetAssociatesAction,
21
24
  BusinessUnitSetContactEmailAction,
22
25
  BusinessUnitSetStoreModeAction,
@@ -101,6 +104,7 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business
101
104
  if (this._isDivisionDraft(draft)) {
102
105
  const division = {
103
106
  ...resource,
107
+ unitType: "Division" as const,
104
108
  parentUnit: getBusinessUnitKeyReference(
105
109
  draft.parentUnit,
106
110
  context.projectKey,
@@ -112,7 +116,10 @@ export class BusinessUnitRepository extends AbstractResourceRepository<"business
112
116
  return division;
113
117
  }
114
118
  if (this._isCompanyDraft(draft)) {
115
- const company = resource as Company;
119
+ const company = {
120
+ ...resource,
121
+ unitType: "Company" as const,
122
+ } as Company;
116
123
 
117
124
  this.saveNew(context, company);
118
125
  return company;
@@ -285,4 +292,53 @@ class BusinessUnitUpdateHandler
285
292
  ) {
286
293
  resource.storeMode = storeMode;
287
294
  }
295
+
296
+ setDefaultShippingAddress(
297
+ context: RepositoryContext,
298
+ resource: Writable<BusinessUnit>,
299
+ { addressId }: BusinessUnitSetDefaultShippingAddressAction,
300
+ ) {
301
+ resource.defaultShippingAddressId = addressId;
302
+ }
303
+
304
+ addShippingAddressId(
305
+ context: RepositoryContext,
306
+ resource: Writable<BusinessUnit>,
307
+ { addressId }: BusinessUnitAddShippingAddressIdAction,
308
+ ) {
309
+ if (!resource.shippingAddressIds) {
310
+ resource.shippingAddressIds = [];
311
+ }
312
+ if (addressId) {
313
+ resource.shippingAddressIds.push(addressId);
314
+ }
315
+ }
316
+
317
+ removeAddress(
318
+ context: RepositoryContext,
319
+ resource: Writable<BusinessUnit>,
320
+ { addressId }: BusinessUnitRemoveAddressAction,
321
+ ) {
322
+ resource.addresses = resource.addresses.filter(
323
+ (addr) => addr.id !== addressId,
324
+ );
325
+
326
+ if (resource.shippingAddressIds) {
327
+ resource.shippingAddressIds = resource.shippingAddressIds.filter(
328
+ (id) => id !== addressId,
329
+ );
330
+ }
331
+ if (resource.billingAddressIds) {
332
+ resource.billingAddressIds = resource.billingAddressIds.filter(
333
+ (id) => id !== addressId,
334
+ );
335
+ }
336
+
337
+ if (resource.defaultShippingAddressId === addressId) {
338
+ resource.defaultShippingAddressId = undefined;
339
+ }
340
+ if (resource.defaultBillingAddressId === addressId) {
341
+ resource.defaultBillingAddressId = undefined;
342
+ }
343
+ }
288
344
  }