@labdigital/commercetools-mock 2.31.2 → 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.2",
3
+ "version": "2.32.0",
4
4
  "license": "MIT",
5
5
  "author": "Michael van Tellingen",
6
6
  "type": "module",
@@ -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>,
@@ -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