@labdigital/commercetools-mock 2.31.1 → 2.32.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labdigital/commercetools-mock",
3
- "version": "2.31.1",
3
+ "version": "2.32.0",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -73,7 +73,14 @@ export class CartUpdateHandler
73
73
  addLineItem(
74
74
  context: RepositoryContext,
75
75
  resource: Writable<Cart>,
76
- { productId, variantId, sku, custom, quantity = 1 }: CartAddLineItemAction,
76
+ {
77
+ productId,
78
+ variantId,
79
+ sku,
80
+ custom,
81
+ quantity = 1,
82
+ addedAt,
83
+ }: CartAddLineItemAction,
77
84
  ) {
78
85
  let product: Product | null = null;
79
86
 
@@ -157,6 +164,7 @@ export class CartUpdateHandler
157
164
  }
158
165
  resource.lineItems.push({
159
166
  id: uuidv4(),
167
+ addedAt: addedAt ? addedAt : new Date().toISOString(),
160
168
  productId: product.id,
161
169
  productKey: product.key,
162
170
  productSlug: product.masterData.current.slug,
@@ -1,5 +1,9 @@
1
1
  import type {
2
+ BaseAddress,
2
3
  Customer,
4
+ CustomerAddAddressAction,
5
+ CustomerAddBillingAddressIdAction,
6
+ CustomerAddShippingAddressIdAction,
3
7
  CustomerChangeAddressAction,
4
8
  CustomerChangeEmailAction,
5
9
  CustomerSetAuthenticationModeAction,
@@ -19,6 +23,7 @@ import type {
19
23
  InvalidJsonInputError,
20
24
  } from "@commercetools/platform-sdk";
21
25
  import { CommercetoolsError } from "~src/exceptions";
26
+ import { generateRandomString } from "~src/helpers";
22
27
  import { hashPassword } from "~src/lib/password";
23
28
  import type { Writable } from "~src/types";
24
29
  import {
@@ -32,6 +37,83 @@ export class CustomerUpdateHandler
32
37
  extends AbstractUpdateHandler
33
38
  implements Partial<UpdateHandlerInterface<Customer, CustomerUpdateAction>>
34
39
  {
40
+ addAddress(
41
+ _context: RepositoryContext,
42
+ resource: Writable<Customer>,
43
+ { address }: CustomerAddAddressAction,
44
+ ) {
45
+ resource.addresses.push({
46
+ ...address,
47
+ id: address.id ?? generateRandomString(5),
48
+ } as BaseAddress);
49
+ }
50
+
51
+ addBillingAddressId(
52
+ _context: RepositoryContext,
53
+ resource: Writable<Customer>,
54
+ { addressId, addressKey }: CustomerAddBillingAddressIdAction,
55
+ ) {
56
+ const address = resource.addresses.find((a) => {
57
+ if (a.id != undefined && addressId != undefined && a.id === addressId) {
58
+ return true;
59
+ }
60
+
61
+ return (
62
+ a.key != undefined && addressKey != undefined && a.key === addressKey
63
+ );
64
+ });
65
+
66
+ if (!address) {
67
+ throw new CommercetoolsError<InvalidInputError>(
68
+ {
69
+ code: "InvalidInput",
70
+ message: `Address with id '${addressId}' or key '${addressKey}' not found.`,
71
+ },
72
+ 400,
73
+ );
74
+ }
75
+
76
+ const billingAddressId = String(address.id);
77
+ if (resource.billingAddressIds?.length) {
78
+ resource.billingAddressIds.push(billingAddressId);
79
+ } else if (address) {
80
+ resource.billingAddressIds = [billingAddressId];
81
+ }
82
+ }
83
+
84
+ addShippingAddressId(
85
+ _context: RepositoryContext,
86
+ resource: Writable<Customer>,
87
+ { addressId, addressKey }: CustomerAddShippingAddressIdAction,
88
+ ) {
89
+ const address = resource.addresses.find((a) => {
90
+ if (a.id != undefined && addressId != undefined && a.id === addressId) {
91
+ return true;
92
+ }
93
+
94
+ return (
95
+ a.key != undefined && addressKey != undefined && a.key === addressKey
96
+ );
97
+ });
98
+
99
+ if (!address) {
100
+ throw new CommercetoolsError<InvalidInputError>(
101
+ {
102
+ code: "InvalidInput",
103
+ message: `Address with id '${addressId}' or key '${addressKey}' not found.`,
104
+ },
105
+ 400,
106
+ );
107
+ }
108
+
109
+ const shippingAddressId = String(address.id);
110
+ if (resource.shippingAddressIds?.length) {
111
+ resource.shippingAddressIds.push(shippingAddressId);
112
+ } else if (address) {
113
+ resource.shippingAddressIds = [shippingAddressId];
114
+ }
115
+ }
116
+
35
117
  changeAddress(
36
118
  context: RepositoryContext,
37
119
  resource: Writable<Customer>,
@@ -36,7 +36,13 @@ export class ShoppingListUpdateHandler
36
36
  addLineItem(
37
37
  context: RepositoryContext,
38
38
  resource: Writable<ShoppingList>,
39
- { productId, variantId, sku, quantity = 1 }: ShoppingListAddLineItemAction,
39
+ {
40
+ productId,
41
+ variantId,
42
+ sku,
43
+ quantity = 1,
44
+ addedAt,
45
+ }: ShoppingListAddLineItemAction,
40
46
  ) {
41
47
  let product: Product | null = null;
42
48
 
@@ -90,7 +96,7 @@ export class ShoppingListUpdateHandler
90
96
  } else {
91
97
  // add line item
92
98
  resource.lineItems.push({
93
- addedAt: new Date().toISOString(),
99
+ addedAt: addedAt ? addedAt : new Date().toISOString(),
94
100
  id: uuidv4(),
95
101
  productId: product.id,
96
102
  productSlug: product.masterData.current.slug,
@@ -320,6 +320,131 @@ describe("Customer Update Actions", () => {
320
320
  expect(response.body.vatId).toBe("ABCD");
321
321
  });
322
322
 
323
+ test("addAddress", async () => {
324
+ assert(customer, "customer not created");
325
+
326
+ customer = {
327
+ ...customer,
328
+ addresses: [
329
+ {
330
+ ...getBaseResourceProperties(),
331
+ id: "address-uuid",
332
+ firstName: "John",
333
+ lastName: "Doe",
334
+ streetName: "Main Street",
335
+ streetNumber: "1",
336
+ postalCode: "12345",
337
+ country: "DE",
338
+ },
339
+ ],
340
+ defaultBillingAddressId: "address-uuid",
341
+ };
342
+ ctMock.project("dummy").add("customer", customer);
343
+
344
+ const response = await supertest(ctMock.app)
345
+ .post(`/dummy/customers/${customer.id}`)
346
+ .send({
347
+ version: 1,
348
+ actions: [
349
+ {
350
+ action: "addAddress",
351
+ address: {
352
+ firstName: "Foo",
353
+ lastName: "Bar",
354
+ streetName: "Baz Street",
355
+ streetNumber: "99",
356
+ postalCode: "12ab",
357
+ country: "NL",
358
+ },
359
+ },
360
+ ],
361
+ });
362
+ expect(response.status).toBe(200);
363
+ expect(response.body.version).toBe(2);
364
+ expect(response.body.addresses).toMatchObject([
365
+ {
366
+ id: "address-uuid",
367
+ firstName: "John",
368
+ lastName: "Doe",
369
+ streetName: "Main Street",
370
+ streetNumber: "1",
371
+ postalCode: "12345",
372
+ country: "DE",
373
+ },
374
+ {
375
+ id: expect.any(String),
376
+ firstName: "Foo",
377
+ lastName: "Bar",
378
+ streetName: "Baz Street",
379
+ streetNumber: "99",
380
+ postalCode: "12ab",
381
+ country: "NL",
382
+ },
383
+ ]);
384
+ });
385
+
386
+ test("addBillingAddressId", async () => {
387
+ assert(customer, "customer not created");
388
+
389
+ customer = {
390
+ ...customer,
391
+ addresses: [
392
+ {
393
+ key: "address-key",
394
+ firstName: "John",
395
+ lastName: "Doe",
396
+ streetName: "Main Street",
397
+ streetNumber: "1",
398
+ postalCode: "12345",
399
+ country: "DE",
400
+ },
401
+ ],
402
+ };
403
+ ctMock.project("dummy").add("customer", customer);
404
+
405
+ const response = await supertest(ctMock.app)
406
+ .post(`/dummy/customers/${customer.id}`)
407
+ .send({
408
+ version: 1,
409
+ actions: [{ action: "addBillingAddressId", addressKey: "address-key" }],
410
+ });
411
+ expect(response.status).toBe(200);
412
+ expect(response.body.version).toBe(2);
413
+ expect(response.body.billingAddressIds).toHaveLength(1);
414
+ });
415
+
416
+ test("addShippingAddressId", async () => {
417
+ assert(customer, "customer not created");
418
+
419
+ customer = {
420
+ ...customer,
421
+ addresses: [
422
+ {
423
+ key: "address-key",
424
+ firstName: "John",
425
+ lastName: "Doe",
426
+ streetName: "Main Street",
427
+ streetNumber: "1",
428
+ postalCode: "12345",
429
+ country: "DE",
430
+ },
431
+ ],
432
+ };
433
+ ctMock.project("dummy").add("customer", customer);
434
+
435
+ const response = await supertest(ctMock.app)
436
+ .post(`/dummy/customers/${customer.id}`)
437
+ .send({
438
+ version: 1,
439
+ actions: [
440
+ { action: "addShippingAddressId", addressKey: "address-key" },
441
+ ],
442
+ });
443
+ expect(response.status).toBe(200);
444
+ expect(response.body.version).toBe(2);
445
+ expect(response.body.shippingAddressIds).toHaveLength(1);
446
+ });
447
+
323
448
  test("changeAddress", async () => {
324
449
  assert(customer, "customer not created");
325
450