@labdigital/commercetools-mock 2.31.2 → 2.33.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/dist/index.cjs +55 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/repositories/customer/actions.ts +82 -0
- package/src/repositories/order/actions.ts +9 -0
- package/src/services/customer.test.ts +125 -0
- package/src/services/order.test.ts +16 -0
package/package.json
CHANGED
|
@@ -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>,
|
|
@@ -15,6 +15,7 @@ import type {
|
|
|
15
15
|
OrderSetDeliveryCustomFieldAction,
|
|
16
16
|
OrderSetLocaleAction,
|
|
17
17
|
OrderSetOrderNumberAction,
|
|
18
|
+
OrderSetPurchaseOrderNumberAction,
|
|
18
19
|
OrderSetShippingAddressAction,
|
|
19
20
|
OrderSetStoreAction,
|
|
20
21
|
OrderTransitionStateAction,
|
|
@@ -227,6 +228,14 @@ export class OrderUpdateHandler
|
|
|
227
228
|
resource.orderNumber = orderNumber;
|
|
228
229
|
}
|
|
229
230
|
|
|
231
|
+
setPurchaseOrderNumber(
|
|
232
|
+
context: RepositoryContext,
|
|
233
|
+
resource: Writable<Order>,
|
|
234
|
+
{ purchaseOrderNumber }: OrderSetPurchaseOrderNumberAction,
|
|
235
|
+
) {
|
|
236
|
+
resource.purchaseOrderNumber = purchaseOrderNumber;
|
|
237
|
+
}
|
|
238
|
+
|
|
230
239
|
setShippingAddress(
|
|
231
240
|
context: RepositoryContext,
|
|
232
241
|
resource: Writable<Order>,
|
|
@@ -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
|
|
|
@@ -371,6 +371,22 @@ describe("Order Update Actions", () => {
|
|
|
371
371
|
expect(response.body.orderNumber).toBe("5000123");
|
|
372
372
|
});
|
|
373
373
|
|
|
374
|
+
test("setPurchaseOrderNumber", async () => {
|
|
375
|
+
assert(order, "order not created");
|
|
376
|
+
|
|
377
|
+
const response = await supertest(ctMock.app)
|
|
378
|
+
.post(`/dummy/orders/${order.id}`)
|
|
379
|
+
.send({
|
|
380
|
+
version: 1,
|
|
381
|
+
actions: [
|
|
382
|
+
{ action: "setPurchaseOrderNumber", purchaseOrderNumber: "abc123" },
|
|
383
|
+
],
|
|
384
|
+
});
|
|
385
|
+
expect(response.status).toBe(200);
|
|
386
|
+
expect(response.body.version).toBe(2);
|
|
387
|
+
expect(response.body.purchaseOrderNumber).toBe("abc123");
|
|
388
|
+
});
|
|
389
|
+
|
|
374
390
|
test("changeOrderState", async () => {
|
|
375
391
|
assert(order, "order not created");
|
|
376
392
|
|