|
1
|
import { ICustomerModuleService } from "@medusajs/framework/types" |
|
2
|
import { Module, Modules } from "@medusajs/framework/utils" |
|
3
|
import { CustomerModuleService } from "@services" |
|
4
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
5
|
|
|
6
|
jest.setTimeout(30000) |
|
7
|
|
|
8
|
moduleIntegrationTestRunner<ICustomerModuleService>({ |
|
9
|
moduleName: Modules.CUSTOMER, |
|
10
|
testSuite: ({ service }) => { |
|
11
|
describe("Customer Module Service", () => { |
|
12
|
it(`should export the appropriate linkable configuration`, () => { |
|
13
|
const linkable = Module(Modules.CUSTOMER, { |
|
14
|
service: CustomerModuleService, |
|
15
|
}).linkable |
|
16
|
|
|
17
|
expect(Object.keys(linkable)).toEqual([ |
|
18
|
"customerAddress", |
|
19
|
"customer", |
|
20
|
"customerGroup", |
|
21
|
"customerGroupCustomer", |
|
22
|
]) |
|
23
|
|
|
24
|
Object.keys(linkable).forEach((key) => { |
|
25
|
delete linkable[key].toJSON |
|
26
|
}) |
|
27
|
|
|
28
|
expect(linkable).toEqual({ |
|
29
|
customerAddress: { |
|
30
|
id: { |
|
31
|
linkable: "customer_address_id", |
|
32
|
entity: "CustomerAddress", |
|
33
|
primaryKey: "id", |
|
34
|
serviceName: "customer", |
|
35
|
field: "customerAddress", |
|
36
|
}, |
|
37
|
}, |
|
38
|
customerGroupCustomer: { |
|
39
|
id: { |
|
40
|
linkable: "customer_group_customer_id", |
|
41
|
entity: "CustomerGroupCustomer", |
|
42
|
primaryKey: "id", |
|
43
|
serviceName: "customer", |
|
44
|
field: "customerGroupCustomer", |
|
45
|
}, |
|
46
|
}, |
|
47
|
customerGroup: { |
|
48
|
id: { |
|
49
|
linkable: "customer_group_id", |
|
50
|
entity: "CustomerGroup", |
|
51
|
primaryKey: "id", |
|
52
|
serviceName: "customer", |
|
53
|
field: "customerGroup", |
|
54
|
}, |
|
55
|
}, |
|
56
|
customer: { |
|
57
|
id: { |
|
58
|
linkable: "customer_id", |
|
59
|
entity: "Customer", |
|
60
|
primaryKey: "id", |
|
61
|
serviceName: "customer", |
|
62
|
field: "customer", |
|
63
|
}, |
|
64
|
}, |
|
65
|
}) |
|
66
|
}) |
|
67
|
|
|
68
|
describe("create", () => { |
|
69
|
it("should create a single customer", async () => { |
|
70
|
const customerData = { |
|
71
|
company_name: "Acme Corp", |
|
72
|
first_name: "John", |
|
73
|
last_name: "Doe", |
|
74
|
email: "john.doe@acmecorp.com", |
|
75
|
phone: "123456789", |
|
76
|
created_by: "admin", |
|
77
|
metadata: { membership: "gold" }, |
|
78
|
} |
|
79
|
const customer = await service.createCustomers(customerData) |
|
80
|
|
|
81
|
expect(customer).toEqual( |
|
82
|
expect.objectContaining({ |
|
83
|
id: expect.any(String), |
|
84
|
company_name: "Acme Corp", |
|
85
|
first_name: "John", |
|
86
|
last_name: "Doe", |
|
87
|
email: "john.doe@acmecorp.com", |
|
88
|
phone: "123456789", |
|
89
|
created_by: "admin", |
|
90
|
metadata: expect.objectContaining({ membership: "gold" }), |
|
91
|
}) |
|
92
|
) |
|
93
|
}) |
|
94
|
|
|
95
|
it("should create two customers with the same email but one has an account", async () => { |
|
96
|
const customerData = { |
|
97
|
company_name: "Acme Corp", |
|
98
|
first_name: "John", |
|
99
|
last_name: "Doe", |
|
100
|
email: "john.doe@acmecorp.com", |
|
101
|
phone: "123456789", |
|
102
|
created_by: "admin", |
|
103
|
metadata: { membership: "gold" }, |
|
104
|
} |
|
105
|
const customerData2 = { |
|
106
|
...customerData, |
|
107
|
has_account: true, |
|
108
|
} |
|
109
|
const [customer, customer2] = await service.createCustomers([ |
|
110
|
customerData, |
|
111
|
customerData2, |
|
112
|
]) |
|
113
|
|
|
114
|
expect(customer).toEqual( |
|
115
|
expect.objectContaining({ |
|
116
|
id: expect.any(String), |
|
117
|
company_name: "Acme Corp", |
|
118
|
first_name: "John", |
|
119
|
last_name: "Doe", |
|
120
|
email: "john.doe@acmecorp.com", |
|
121
|
phone: "123456789", |
|
122
|
created_by: "admin", |
|
123
|
metadata: expect.objectContaining({ membership: "gold" }), |
|
124
|
}) |
|
125
|
) |
|
126
|
expect(customer2).toEqual( |
|
127
|
expect.objectContaining({ |
|
128
|
id: expect.any(String), |
|
129
|
company_name: "Acme Corp", |
|
130
|
first_name: "John", |
|
131
|
last_name: "Doe", |
|
132
|
email: "john.doe@acmecorp.com", |
|
133
|
phone: "123456789", |
|
134
|
created_by: "admin", |
|
135
|
metadata: expect.objectContaining({ membership: "gold" }), |
|
136
|
has_account: true, |
|
137
|
}) |
|
138
|
) |
|
139
|
}) |
|
140
|
|
|
141
|
it("should fail to create a duplicated guest customers", async () => { |
|
142
|
const customerData = { |
|
143
|
company_name: "Acme Corp", |
|
144
|
first_name: "John", |
|
145
|
last_name: "Doe", |
|
146
|
email: "john.doe@acmecorp.com", |
|
147
|
phone: "123456789", |
|
148
|
created_by: "admin", |
|
149
|
metadata: { membership: "gold" }, |
|
150
|
} |
|
151
|
|
|
152
|
const err = await service |
|
153
|
.createCustomers([customerData, customerData]) |
|
154
|
.catch((err) => err) |
|
155
|
|
|
156
|
expect(err.message).toBe( |
|
157
|
"Customer with email: john.doe@acmecorp.com, has_account: false, already exists." |
|
158
|
) |
|
159
|
}) |
|
160
|
|
|
161
|
it("should fail to create a duplicated customers", async () => { |
|
162
|
const customerData = { |
|
163
|
company_name: "Acme Corp", |
|
164
|
first_name: "John", |
|
165
|
last_name: "Doe", |
|
166
|
email: "john.doe@acmecorp.com", |
|
167
|
phone: "123456789", |
|
168
|
created_by: "admin", |
|
169
|
metadata: { membership: "gold" }, |
|
170
|
has_account: true, |
|
171
|
} |
|
172
|
|
|
173
|
const err = await service |
|
174
|
.createCustomers([customerData, customerData]) |
|
175
|
.catch((err) => err) |
|
176
|
|
|
177
|
expect(err.message).toBe( |
|
178
|
"Customer with email: john.doe@acmecorp.com, has_account: true, already exists." |
|
179
|
) |
|
180
|
}) |
|
181
|
|
|
182
|
it("should create address", async () => { |
|
183
|
const customerData = { |
|
184
|
company_name: "Acme Corp", |
|
185
|
first_name: "John", |
|
186
|
last_name: "Doe", |
|
187
|
addresses: [ |
|
188
|
{ |
|
189
|
address_1: "Testvej 1", |
|
190
|
address_2: "Testvej 2", |
|
191
|
city: "Testby", |
|
192
|
country_code: "DK", |
|
193
|
province: "Test", |
|
194
|
postal_code: "8000", |
|
195
|
phone: "123456789", |
|
196
|
metadata: { membership: "gold" }, |
|
197
|
is_default_shipping: true, |
|
198
|
}, |
|
199
|
], |
|
200
|
} |
|
201
|
const customer = await service.createCustomers(customerData) |
|
202
|
|
|
203
|
const [address] = await service.listCustomerAddresses({ |
|
204
|
customer_id: customer.id, |
|
205
|
}) |
|
206
|
|
|
207
|
expect(address).toEqual( |
|
208
|
expect.objectContaining({ |
|
209
|
id: expect.any(String), |
|
210
|
address_1: "Testvej 1", |
|
211
|
address_2: "Testvej 2", |
|
212
|
city: "Testby", |
|
213
|
country_code: "DK", |
|
214
|
province: "Test", |
|
215
|
postal_code: "8000", |
|
216
|
phone: "123456789", |
|
217
|
metadata: expect.objectContaining({ membership: "gold" }), |
|
218
|
is_default_shipping: true, |
|
219
|
}) |
|
220
|
) |
|
221
|
}) |
|
222
|
|
|
223
|
it("should fail to create two default shipping", async () => { |
|
224
|
const customerData = { |
|
225
|
company_name: "Acme Corp", |
|
226
|
first_name: "John", |
|
227
|
last_name: "Doe", |
|
228
|
addresses: [ |
|
229
|
{ |
|
230
|
address_1: "Testvej 1", |
|
231
|
address_2: "Testvej 2", |
|
232
|
city: "Testby", |
|
233
|
country_code: "DK", |
|
234
|
province: "Test", |
|
235
|
postal_code: "8000", |
|
236
|
phone: "123456789", |
|
237
|
metadata: { membership: "gold" }, |
|
238
|
is_default_shipping: true, |
|
239
|
}, |
|
240
|
{ |
|
241
|
address_1: "Test Ave 1", |
|
242
|
address_2: "Test Ave 2", |
|
243
|
city: "Testville", |
|
244
|
country_code: "US", |
|
245
|
is_default_shipping: true, |
|
246
|
}, |
|
247
|
], |
|
248
|
} |
|
249
|
await expect(service.createCustomers(customerData)).rejects.toThrow( |
|
250
|
/Customer address with customer_id: .*? already exists./ |
|
251
|
) |
|
252
|
}) |
|
253
|
|
|
254
|
it("should create multiple customers", async () => { |
|
255
|
const customersData = [ |
|
256
|
{ |
|
257
|
company_name: "Acme Corp", |
|
258
|
first_name: "John", |
|
259
|
last_name: "Doe", |
|
260
|
email: "john.doe@acmecorp.com", |
|
261
|
phone: "123456789", |
|
262
|
created_by: "admin", |
|
263
|
metadata: { membership: "gold" }, |
|
264
|
}, |
|
265
|
{ |
|
266
|
first_name: "Jane", |
|
267
|
last_name: "Smith", |
|
268
|
email: "jane.smith@example.com", |
|
269
|
phone: "987654321", |
|
270
|
metadata: { membership: "silver" }, |
|
271
|
}, |
|
272
|
] |
|
273
|
const customer = await service.createCustomers(customersData) |
|
274
|
|
|
275
|
expect(customer).toEqual( |
|
276
|
expect.arrayContaining([ |
|
277
|
expect.objectContaining({ |
|
278
|
id: expect.any(String), |
|
279
|
company_name: "Acme Corp", |
|
280
|
first_name: "John", |
|
281
|
last_name: "Doe", |
|
282
|
email: "john.doe@acmecorp.com", |
|
283
|
phone: "123456789", |
|
284
|
created_by: "admin", |
|
285
|
metadata: expect.objectContaining({ membership: "gold" }), |
|
286
|
}), |
|
287
|
expect.objectContaining({ |
|
288
|
id: expect.any(String), |
|
289
|
first_name: "Jane", |
|
290
|
last_name: "Smith", |
|
291
|
email: "jane.smith@example.com", |
|
292
|
phone: "987654321", |
|
293
|
metadata: expect.objectContaining({ membership: "silver" }), |
|
294
|
}), |
|
295
|
]) |
|
296
|
) |
|
297
|
}) |
|
298
|
}) |
|
299
|
|
|
300
|
describe("createCustomerGroup", () => { |
|
301
|
it("should create a single customer group", async () => { |
|
302
|
const group = await service.createCustomerGroups({ |
|
303
|
name: "VIP Customers", |
|
304
|
metadata: { priority: "high" }, |
|
305
|
created_by: "admin", |
|
306
|
}) |
|
307
|
|
|
308
|
expect(group).toEqual( |
|
309
|
expect.objectContaining({ |
|
310
|
id: expect.any(String), |
|
311
|
name: "VIP Customers", |
|
312
|
metadata: expect.objectContaining({ priority: "high" }), |
|
313
|
created_by: "admin", |
|
314
|
}) |
|
315
|
) |
|
316
|
}) |
|
317
|
|
|
318
|
it("should create multiple customer groups", async () => { |
|
319
|
const groups = await service.createCustomerGroups([ |
|
320
|
{ |
|
321
|
name: "VIP Customers", |
|
322
|
metadata: { priority: "high" }, |
|
323
|
created_by: "admin", |
|
324
|
}, |
|
325
|
{ |
|
326
|
name: "Regular Customers", |
|
327
|
metadata: { discount: "10%" }, |
|
328
|
created_by: "staff", |
|
329
|
}, |
|
330
|
]) |
|
331
|
|
|
332
|
expect(groups).toEqual( |
|
333
|
expect.arrayContaining([ |
|
334
|
expect.objectContaining({ |
|
335
|
id: expect.any(String), |
|
336
|
name: "VIP Customers", |
|
337
|
metadata: expect.objectContaining({ priority: "high" }), |
|
338
|
created_by: "admin", |
|
339
|
}), |
|
340
|
expect.objectContaining({ |
|
341
|
id: expect.any(String), |
|
342
|
name: "Regular Customers", |
|
343
|
metadata: expect.objectContaining({ discount: "10%" }), |
|
344
|
created_by: "staff", |
|
345
|
}), |
|
346
|
]) |
|
347
|
) |
|
348
|
}) |
|
349
|
}) |
|
350
|
|
|
351
|
describe("list", () => { |
|
352
|
it("should list all customers when no filters are applied", async () => { |
|
353
|
await service.createCustomers([ |
|
354
|
{ |
|
355
|
first_name: "John", |
|
356
|
last_name: "Doe", |
|
357
|
email: "john.doe@example.com", |
|
358
|
}, |
|
359
|
{ |
|
360
|
first_name: "Jane", |
|
361
|
last_name: "Smith", |
|
362
|
email: "jane.smith@example.com", |
|
363
|
}, |
|
364
|
]) |
|
365
|
|
|
366
|
const customers = await service.listCustomers() |
|
367
|
|
|
368
|
expect(customers.length).toBeGreaterThanOrEqual(2) |
|
369
|
expect(customers).toEqual( |
|
370
|
expect.arrayContaining([ |
|
371
|
expect.objectContaining({ |
|
372
|
first_name: "John", |
|
373
|
last_name: "Doe", |
|
374
|
email: "john.doe@example.com", |
|
375
|
}), |
|
376
|
expect.objectContaining({ |
|
377
|
first_name: "Jane", |
|
378
|
last_name: "Smith", |
|
379
|
email: "jane.smith@example.com", |
|
380
|
}), |
|
381
|
]) |
|
382
|
) |
|
383
|
}) |
|
384
|
|
|
385
|
it("should list customers filtered by a specific email", async () => { |
|
386
|
await service.createCustomers([ |
|
387
|
{ |
|
388
|
first_name: "John", |
|
389
|
last_name: "Doe", |
|
390
|
email: "unique.email@example.com", |
|
391
|
}, |
|
392
|
{ |
|
393
|
first_name: "Jane", |
|
394
|
last_name: "Smith", |
|
395
|
email: "jane.smith@example.com", |
|
396
|
}, |
|
397
|
]) |
|
398
|
|
|
399
|
const filter = { email: "unique.email@example.com" } |
|
400
|
const customers = await service.listCustomers(filter) |
|
401
|
|
|
402
|
expect(customers.length).toBe(1) |
|
403
|
expect(customers[0]).toEqual( |
|
404
|
expect.objectContaining({ |
|
405
|
first_name: "John", |
|
406
|
last_name: "Doe", |
|
407
|
email: "unique.email@example.com", |
|
408
|
}) |
|
409
|
) |
|
410
|
}) |
|
411
|
|
|
412
|
it("should list customers by a specific customer group", async () => { |
|
413
|
const vipGroup = await service.createCustomerGroups({ name: "VIP" }) |
|
414
|
|
|
415
|
const [john] = await service.createCustomers([ |
|
416
|
{ |
|
417
|
first_name: "John", |
|
418
|
last_name: "Doe", |
|
419
|
email: "john.doe@example.com", |
|
420
|
}, |
|
421
|
{ |
|
422
|
first_name: "Jane", |
|
423
|
last_name: "Smith", |
|
424
|
email: "jane.smith@example.com", |
|
425
|
}, |
|
426
|
]) |
|
427
|
|
|
428
|
await service.addCustomerToGroup({ |
|
429
|
customer_id: john.id, |
|
430
|
customer_group_id: vipGroup.id, |
|
431
|
}) |
|
432
|
|
|
433
|
const filter = { groups: vipGroup.id } |
|
434
|
const customers = await service.listCustomers(filter) |
|
435
|
|
|
436
|
expect(customers).toEqual( |
|
437
|
expect.arrayContaining([ |
|
438
|
expect.objectContaining({ |
|
439
|
first_name: "John", |
|
440
|
last_name: "Doe", |
|
441
|
email: "john.doe@example.com", |
|
442
|
}), |
|
443
|
]) |
|
444
|
) |
|
445
|
expect(customers).not.toEqual( |
|
446
|
expect.arrayContaining([ |
|
447
|
expect.objectContaining({ |
|
448
|
first_name: "Jane", |
|
449
|
last_name: "Smith", |
|
450
|
email: "jane.smith@example.com", |
|
451
|
}), |
|
452
|
]) |
|
453
|
) |
|
454
|
}) |
|
455
|
}) |
|
456
|
|
|
457
|
describe("addCustomerToGroup", () => { |
|
458
|
it("should add a single customer to a customer group", async () => { |
|
459
|
const [customer] = await service.createCustomers([ |
|
460
|
{ |
|
461
|
first_name: "John", |
|
462
|
last_name: "Doe", |
|
463
|
email: "john.doe@example.com", |
|
464
|
}, |
|
465
|
]) |
|
466
|
const [group] = await service.createCustomerGroups([{ name: "VIP" }]) |
|
467
|
|
|
468
|
const result = await service.addCustomerToGroup({ |
|
469
|
customer_id: customer.id, |
|
470
|
customer_group_id: group.id, |
|
471
|
}) |
|
472
|
|
|
473
|
expect(result).toEqual( |
|
474
|
expect.objectContaining({ id: expect.any(String) }) |
|
475
|
) |
|
476
|
|
|
477
|
// Additional validation (optional): retrieve the customer and check if the group is assigned |
|
478
|
const updatedCustomer = await service.retrieveCustomer(customer.id, { |
|
479
|
relations: ["groups"], |
|
480
|
}) |
|
481
|
expect(updatedCustomer.groups).toContainEqual( |
|
482
|
expect.objectContaining({ id: group.id }) |
|
483
|
) |
|
484
|
}) |
|
485
|
|
|
486
|
it("should add multiple customers to customer groups", async () => { |
|
487
|
const customers = await service.createCustomers([ |
|
488
|
{ |
|
489
|
first_name: "John", |
|
490
|
last_name: "Doe", |
|
491
|
email: "john.doe@example.com", |
|
492
|
}, |
|
493
|
{ |
|
494
|
first_name: "Jane", |
|
495
|
last_name: "Smith", |
|
496
|
email: "jane.smith@example.com", |
|
497
|
}, |
|
498
|
]) |
|
499
|
const groups = await service.createCustomerGroups([ |
|
500
|
{ name: "VIP" }, |
|
501
|
{ name: "Regular" }, |
|
502
|
]) |
|
503
|
|
|
504
|
const pairs = customers.map((customer, index) => ({ |
|
505
|
customer_id: customer.id, |
|
506
|
customer_group_id: groups[index % groups.length].id, |
|
507
|
})) |
|
508
|
|
|
509
|
const results = await service.addCustomerToGroup(pairs) |
|
510
|
|
|
511
|
expect(results).toEqual( |
|
512
|
expect.arrayContaining([ |
|
513
|
expect.objectContaining({ id: expect.any(String) }), |
|
514
|
expect.objectContaining({ id: expect.any(String) }), |
|
515
|
]) |
|
516
|
) |
|
517
|
|
|
518
|
for (const customer of customers) { |
|
519
|
const updatedCustomer = await service.retrieveCustomer( |
|
520
|
customer.id, |
|
521
|
{ |
|
522
|
relations: ["groups"], |
|
523
|
} |
|
524
|
) |
|
525
|
expect(updatedCustomer.groups).toContainEqual( |
|
526
|
expect.objectContaining({ |
|
527
|
id: groups[customers.indexOf(customer) % groups.length].id, |
|
528
|
}) |
|
529
|
) |
|
530
|
} |
|
531
|
}) |
|
532
|
}) |
|
533
|
|
|
534
|
describe("update", () => { |
|
535
|
it("should update a single customer", async () => { |
|
536
|
const [customer] = await service.createCustomers([ |
|
537
|
{ |
|
538
|
first_name: "John", |
|
539
|
last_name: "Doe", |
|
540
|
email: "john.doe@example.com", |
|
541
|
}, |
|
542
|
]) |
|
543
|
|
|
544
|
const updateData = { first_name: "Jonathan" } |
|
545
|
const updatedCustomer = await service.updateCustomers( |
|
546
|
customer.id, |
|
547
|
updateData |
|
548
|
) |
|
549
|
|
|
550
|
expect(updatedCustomer).toEqual( |
|
551
|
expect.objectContaining({ id: customer.id, first_name: "Jonathan" }) |
|
552
|
) |
|
553
|
}) |
|
554
|
|
|
555
|
it("should update multiple customers by IDs", async () => { |
|
556
|
const customers = await service.createCustomers([ |
|
557
|
{ |
|
558
|
first_name: "John", |
|
559
|
last_name: "Doe", |
|
560
|
email: "john.doe@example.com", |
|
561
|
}, |
|
562
|
{ |
|
563
|
first_name: "Jane", |
|
564
|
last_name: "Smith", |
|
565
|
email: "jane.smith@example.com", |
|
566
|
}, |
|
567
|
]) |
|
568
|
|
|
569
|
const updateData = { last_name: "Updated" } |
|
570
|
const customerIds = customers.map((customer) => customer.id) |
|
571
|
const updatedCustomers = await service.updateCustomers( |
|
572
|
customerIds, |
|
573
|
updateData |
|
574
|
) |
|
575
|
|
|
576
|
updatedCustomers.forEach((updatedCustomer) => { |
|
577
|
expect(updatedCustomer).toEqual( |
|
578
|
expect.objectContaining({ last_name: "Updated" }) |
|
579
|
) |
|
580
|
}) |
|
581
|
}) |
|
582
|
|
|
583
|
it("should update customers using a selector", async () => { |
|
584
|
await service.createCustomers([ |
|
585
|
{ |
|
586
|
first_name: "John", |
|
587
|
last_name: "Doe", |
|
588
|
email: "john.doe@example.com", |
|
589
|
}, |
|
590
|
{ |
|
591
|
first_name: "Jane", |
|
592
|
last_name: "Doe", |
|
593
|
email: "jane.doe@example.com", |
|
594
|
}, |
|
595
|
]) |
|
596
|
|
|
597
|
const selector = { last_name: "Doe" } |
|
598
|
const updateData = { last_name: "Updated" } |
|
599
|
const updatedCustomers = await service.updateCustomers( |
|
600
|
selector, |
|
601
|
updateData |
|
602
|
) |
|
603
|
|
|
604
|
updatedCustomers.forEach((updatedCustomer) => { |
|
605
|
expect(updatedCustomer).toEqual( |
|
606
|
expect.objectContaining({ last_name: "Updated" }) |
|
607
|
) |
|
608
|
}) |
|
609
|
}) |
|
610
|
}) |
|
611
|
|
|
612
|
describe("delete", () => { |
|
613
|
it("should delete a single customer", async () => { |
|
614
|
const [customer] = await service.createCustomers([ |
|
615
|
{ |
|
616
|
first_name: "John", |
|
617
|
last_name: "Doe", |
|
618
|
email: "john.doe@example.com", |
|
619
|
}, |
|
620
|
]) |
|
621
|
|
|
622
|
await service.deleteCustomers(customer.id) |
|
623
|
|
|
624
|
await expect(service.retrieveCustomer(customer.id)).rejects.toThrow( |
|
625
|
`Customer with id: ${customer.id} was not found` |
|
626
|
) |
|
627
|
}) |
|
628
|
|
|
629
|
it("should delete multiple customers by IDs", async () => { |
|
630
|
const customers = await service.createCustomers([ |
|
631
|
{ |
|
632
|
first_name: "John", |
|
633
|
last_name: "Doe", |
|
634
|
email: "john.doe@example.com", |
|
635
|
}, |
|
636
|
{ |
|
637
|
first_name: "Jane", |
|
638
|
last_name: "Smith", |
|
639
|
email: "jane.smith@example.com", |
|
640
|
}, |
|
641
|
]) |
|
642
|
|
|
643
|
const customerIds = customers.map((customer) => customer.id) |
|
644
|
await service.deleteCustomers(customerIds) |
|
645
|
|
|
646
|
for (const customer of customers) { |
|
647
|
await expect(service.retrieveCustomer(customer.id)).rejects.toThrow( |
|
648
|
`Customer with id: ${customer.id} was not found` |
|
649
|
) |
|
650
|
} |
|
651
|
}) |
|
652
|
|
|
653
|
it("should delete customers using a selector", async () => { |
|
654
|
await service.createCustomers([ |
|
655
|
{ |
|
656
|
first_name: "John", |
|
657
|
last_name: "Doe", |
|
658
|
email: "john.doe@example.com", |
|
659
|
}, |
|
660
|
{ |
|
661
|
first_name: "Jane", |
|
662
|
last_name: "Doe", |
|
663
|
email: "jane.doe@example.com", |
|
664
|
}, |
|
665
|
]) |
|
666
|
|
|
667
|
const selector = { last_name: "Doe" } |
|
668
|
await service.deleteCustomers(selector) |
|
669
|
|
|
670
|
const remainingCustomers = await service.listCustomers({ |
|
671
|
last_name: "Doe", |
|
672
|
}) |
|
673
|
expect(remainingCustomers.length).toBe(0) |
|
674
|
}) |
|
675
|
|
|
676
|
it("should cascade address relationship when deleting customer", async () => { |
|
677
|
// Creating a customer and an address |
|
678
|
const customer = await service.createCustomers({ |
|
679
|
first_name: "John", |
|
680
|
last_name: "Doe", |
|
681
|
}) |
|
682
|
await service.createCustomerAddresses({ |
|
683
|
customer_id: customer.id, |
|
684
|
first_name: "John", |
|
685
|
last_name: "Doe", |
|
686
|
postal_code: "10001", |
|
687
|
country_code: "US", |
|
688
|
}) |
|
689
|
|
|
690
|
// verify that the address was added |
|
691
|
const customerWithCustomerAddress = await service.retrieveCustomer( |
|
692
|
customer.id, |
|
693
|
{ |
|
694
|
relations: ["addresses"], |
|
695
|
} |
|
696
|
) |
|
697
|
expect(customerWithCustomerAddress.addresses?.length).toBe(1) |
|
698
|
|
|
699
|
await service.deleteCustomers(customer.id) |
|
700
|
|
|
701
|
const res = await service.listCustomerAddresses({ |
|
702
|
customer_id: customer.id, |
|
703
|
}) |
|
704
|
expect(res.length).toBe(0) |
|
705
|
}) |
|
706
|
|
|
707
|
it("should cascade relationship when deleting customer", async () => { |
|
708
|
// Creating a customer and a group |
|
709
|
const customer = await service.createCustomers({ |
|
710
|
first_name: "John", |
|
711
|
last_name: "Doe", |
|
712
|
}) |
|
713
|
const group = await service.createCustomerGroups({ name: "VIP" }) |
|
714
|
|
|
715
|
// Adding the customer to the groups |
|
716
|
await service.addCustomerToGroup({ |
|
717
|
customer_id: customer.id, |
|
718
|
customer_group_id: group.id, |
|
719
|
}) |
|
720
|
|
|
721
|
await service.deleteCustomers(customer.id) |
|
722
|
|
|
723
|
const res = await service.listCustomerGroupCustomers({ |
|
724
|
customer_id: customer.id, |
|
725
|
customer_group_id: group.id, |
|
726
|
}) |
|
727
|
expect(res.length).toBe(0) |
|
728
|
}) |
|
729
|
}) |
|
730
|
|
|
731
|
describe("deleteCustomerGroup", () => { |
|
732
|
it("should delete a single customer group", async () => { |
|
733
|
const [group] = await service.createCustomerGroups([{ name: "VIP" }]) |
|
734
|
await service.deleteCustomerGroups(group.id) |
|
735
|
|
|
736
|
await expect( |
|
737
|
service.retrieveCustomerGroup(group.id) |
|
738
|
).rejects.toThrowError( |
|
739
|
`CustomerGroup with id: ${group.id} was not found` |
|
740
|
) |
|
741
|
}) |
|
742
|
|
|
743
|
it("should delete multiple customer groups by IDs", async () => { |
|
744
|
const groups = await service.createCustomerGroups([ |
|
745
|
{ name: "VIP" }, |
|
746
|
{ name: "Regular" }, |
|
747
|
]) |
|
748
|
|
|
749
|
const groupIds = groups.map((group) => group.id) |
|
750
|
await service.deleteCustomerGroups(groupIds) |
|
751
|
|
|
752
|
for (const group of groups) { |
|
753
|
await expect( |
|
754
|
service.retrieveCustomerGroup(group.id) |
|
755
|
).rejects.toThrowError( |
|
756
|
`CustomerGroup with id: ${group.id} was not found` |
|
757
|
) |
|
758
|
} |
|
759
|
}) |
|
760
|
|
|
761
|
it("should delete customer groups using a selector", async () => { |
|
762
|
await service.createCustomerGroups([ |
|
763
|
{ name: "VIP" }, |
|
764
|
{ name: "Regular" }, |
|
765
|
]) |
|
766
|
|
|
767
|
const selector = { name: "VIP" } |
|
768
|
await service.deleteCustomerGroups(selector) |
|
769
|
|
|
770
|
const remainingGroups = await service.listCustomerGroups({ |
|
771
|
name: "VIP", |
|
772
|
}) |
|
773
|
expect(remainingGroups.length).toBe(0) |
|
774
|
}) |
|
775
|
|
|
776
|
it("should cascade relationship when deleting customer group", async () => { |
|
777
|
// Creating a customer and a group |
|
778
|
const customer = await service.createCustomers({ |
|
779
|
first_name: "John", |
|
780
|
last_name: "Doe", |
|
781
|
}) |
|
782
|
const group = await service.createCustomerGroups({ name: "VIP" }) |
|
783
|
|
|
784
|
// Adding the customer to the groups |
|
785
|
await service.addCustomerToGroup({ |
|
786
|
customer_id: customer.id, |
|
787
|
customer_group_id: group.id, |
|
788
|
}) |
|
789
|
|
|
790
|
await service.deleteCustomerGroups(group.id) |
|
791
|
|
|
792
|
const res = await service.listCustomerGroupCustomers({ |
|
793
|
customer_id: customer.id, |
|
794
|
customer_group_id: group.id, |
|
795
|
}) |
|
796
|
expect(res.length).toBe(0) |
|
797
|
}) |
|
798
|
}) |
|
799
|
|
|
800
|
describe("addCustomerAddresses", () => { |
|
801
|
it("should add a single address to a customer", async () => { |
|
802
|
const customer = await service.createCustomers({ |
|
803
|
first_name: "John", |
|
804
|
last_name: "Doe", |
|
805
|
}) |
|
806
|
const address = await service.createCustomerAddresses({ |
|
807
|
customer_id: customer.id, |
|
808
|
first_name: "John", |
|
809
|
last_name: "Doe", |
|
810
|
postal_code: "10001", |
|
811
|
country_code: "US", |
|
812
|
}) |
|
813
|
const [customerWithCustomerAddress] = await service.listCustomers( |
|
814
|
{ id: customer.id }, |
|
815
|
{ relations: ["addresses"] } |
|
816
|
) |
|
817
|
|
|
818
|
expect(customerWithCustomerAddress.addresses).toEqual([ |
|
819
|
expect.objectContaining({ id: address.id }), |
|
820
|
]) |
|
821
|
}) |
|
822
|
|
|
823
|
it("should add multiple addresses to a customer", async () => { |
|
824
|
const customer = await service.createCustomers({ |
|
825
|
first_name: "John", |
|
826
|
last_name: "Doe", |
|
827
|
}) |
|
828
|
const addresses = await service.createCustomerAddresses([ |
|
829
|
{ |
|
830
|
customer_id: customer.id, |
|
831
|
first_name: "John", |
|
832
|
last_name: "Doe", |
|
833
|
postal_code: "10001", |
|
834
|
country_code: "US", |
|
835
|
}, |
|
836
|
{ |
|
837
|
customer_id: customer.id, |
|
838
|
first_name: "John", |
|
839
|
last_name: "Doe", |
|
840
|
postal_code: "10002", |
|
841
|
country_code: "US", |
|
842
|
}, |
|
843
|
]) |
|
844
|
const [customerWithCustomerAddresses] = await service.listCustomers( |
|
845
|
{ id: customer.id }, |
|
846
|
{ relations: ["addresses"] } |
|
847
|
) |
|
848
|
|
|
849
|
expect(customerWithCustomerAddresses.addresses).toEqual( |
|
850
|
expect.arrayContaining([ |
|
851
|
expect.objectContaining({ id: addresses[0].id }), |
|
852
|
expect.objectContaining({ id: addresses[1].id }), |
|
853
|
]) |
|
854
|
) |
|
855
|
}) |
|
856
|
|
|
857
|
it("should only be possible to add one default shipping address per customer", async () => { |
|
858
|
const customer = await service.createCustomers({ |
|
859
|
first_name: "John", |
|
860
|
last_name: "Doe", |
|
861
|
}) |
|
862
|
await service.createCustomerAddresses({ |
|
863
|
customer_id: customer.id, |
|
864
|
first_name: "John", |
|
865
|
last_name: "Doe", |
|
866
|
postal_code: "10001", |
|
867
|
country_code: "US", |
|
868
|
is_default_shipping: true, |
|
869
|
}) |
|
870
|
await service.createCustomerAddresses({ |
|
871
|
customer_id: customer.id, |
|
872
|
first_name: "John", |
|
873
|
last_name: "Doe", |
|
874
|
postal_code: "10001", |
|
875
|
country_code: "US", |
|
876
|
is_default_shipping: false, |
|
877
|
}) |
|
878
|
|
|
879
|
await expect( |
|
880
|
service.createCustomerAddresses({ |
|
881
|
customer_id: customer.id, |
|
882
|
first_name: "John", |
|
883
|
last_name: "Doe", |
|
884
|
postal_code: "10002", |
|
885
|
country_code: "US", |
|
886
|
is_default_shipping: true, |
|
887
|
}) |
|
888
|
).rejects.toThrow( |
|
889
|
/Customer address with customer_id: .*? already exists./ |
|
890
|
) |
|
891
|
}) |
|
892
|
|
|
893
|
it("should only be possible to add one default billing address per customer", async () => { |
|
894
|
const customer = await service.createCustomers({ |
|
895
|
first_name: "John", |
|
896
|
last_name: "Doe", |
|
897
|
}) |
|
898
|
await service.createCustomerAddresses({ |
|
899
|
customer_id: customer.id, |
|
900
|
first_name: "John", |
|
901
|
last_name: "Doe", |
|
902
|
postal_code: "10001", |
|
903
|
country_code: "US", |
|
904
|
is_default_billing: true, |
|
905
|
}) |
|
906
|
await service.createCustomerAddresses({ |
|
907
|
customer_id: customer.id, |
|
908
|
first_name: "John", |
|
909
|
last_name: "Doe", |
|
910
|
postal_code: "10001", |
|
911
|
country_code: "US", |
|
912
|
is_default_billing: false, |
|
913
|
}) |
|
914
|
|
|
915
|
await expect( |
|
916
|
service.createCustomerAddresses({ |
|
917
|
customer_id: customer.id, |
|
918
|
first_name: "John", |
|
919
|
last_name: "Doe", |
|
920
|
postal_code: "10002", |
|
921
|
country_code: "US", |
|
922
|
is_default_billing: true, |
|
923
|
}) |
|
924
|
).rejects.toThrow( |
|
925
|
/Customer address with customer_id: .*? already exists./ |
|
926
|
) |
|
927
|
}) |
|
928
|
}) |
|
929
|
|
|
930
|
describe("updateCustomerAddresses", () => { |
|
931
|
it("should update a single address", async () => { |
|
932
|
const customer = await service.createCustomers({ |
|
933
|
first_name: "John", |
|
934
|
last_name: "Doe", |
|
935
|
}) |
|
936
|
const address = await service.createCustomerAddresses({ |
|
937
|
customer_id: customer.id, |
|
938
|
address_name: "Home", |
|
939
|
address_1: "123 Main St", |
|
940
|
}) |
|
941
|
|
|
942
|
await service.updateCustomerAddresses(address.id, { |
|
943
|
address_name: "Work", |
|
944
|
address_1: "456 Main St", |
|
945
|
}) |
|
946
|
|
|
947
|
const updatedCustomer = await service.retrieveCustomer(customer.id, { |
|
948
|
select: ["id"], |
|
949
|
relations: ["addresses"], |
|
950
|
}) |
|
951
|
|
|
952
|
expect(updatedCustomer.addresses).toEqual([ |
|
953
|
expect.objectContaining({ |
|
954
|
id: address.id, |
|
955
|
address_name: "Work", |
|
956
|
address_1: "456 Main St", |
|
957
|
}), |
|
958
|
]) |
|
959
|
}) |
|
960
|
|
|
961
|
it("should update multiple addresses", async () => { |
|
962
|
const customer = await service.createCustomers({ |
|
963
|
first_name: "John", |
|
964
|
last_name: "Doe", |
|
965
|
}) |
|
966
|
const address1 = await service.createCustomerAddresses({ |
|
967
|
customer_id: customer.id, |
|
968
|
address_name: "Home", |
|
969
|
address_1: "123 Main St", |
|
970
|
}) |
|
971
|
const address2 = await service.createCustomerAddresses({ |
|
972
|
customer_id: customer.id, |
|
973
|
address_name: "Work", |
|
974
|
address_1: "456 Main St", |
|
975
|
}) |
|
976
|
|
|
977
|
await service.updateCustomerAddresses( |
|
978
|
{ customer_id: customer.id }, |
|
979
|
{ |
|
980
|
address_name: "Under Construction", |
|
981
|
} |
|
982
|
) |
|
983
|
|
|
984
|
const updatedCustomer = await service.retrieveCustomer(customer.id, { |
|
985
|
select: ["id"], |
|
986
|
relations: ["addresses"], |
|
987
|
}) |
|
988
|
|
|
989
|
expect(updatedCustomer.addresses).toEqual( |
|
990
|
expect.arrayContaining([ |
|
991
|
expect.objectContaining({ |
|
992
|
id: address1.id, |
|
993
|
address_name: "Under Construction", |
|
994
|
}), |
|
995
|
expect.objectContaining({ |
|
996
|
id: address2.id, |
|
997
|
address_name: "Under Construction", |
|
998
|
}), |
|
999
|
]) |
|
1000
|
) |
|
1001
|
}) |
|
1002
|
|
|
1003
|
it("should update multiple addresses with ids", async () => { |
|
1004
|
const customer = await service.createCustomers({ |
|
1005
|
first_name: "John", |
|
1006
|
last_name: "Doe", |
|
1007
|
}) |
|
1008
|
const [address1, address2] = await service.createCustomerAddresses([ |
|
1009
|
{ |
|
1010
|
customer_id: customer.id, |
|
1011
|
address_name: "Home", |
|
1012
|
address_1: "123 Main St", |
|
1013
|
}, |
|
1014
|
{ |
|
1015
|
customer_id: customer.id, |
|
1016
|
address_name: "Work", |
|
1017
|
address_1: "456 Main St", |
|
1018
|
}, |
|
1019
|
]) |
|
1020
|
|
|
1021
|
await service.updateCustomerAddresses([address1.id, address2.id], { |
|
1022
|
address_name: "Under Construction", |
|
1023
|
}) |
|
1024
|
|
|
1025
|
const updatedCustomer = await service.retrieveCustomer(customer.id, { |
|
1026
|
select: ["id"], |
|
1027
|
relations: ["addresses"], |
|
1028
|
}) |
|
1029
|
|
|
1030
|
expect(updatedCustomer.addresses).toEqual( |
|
1031
|
expect.arrayContaining([ |
|
1032
|
expect.objectContaining({ |
|
1033
|
id: address1.id, |
|
1034
|
address_name: "Under Construction", |
|
1035
|
}), |
|
1036
|
expect.objectContaining({ |
|
1037
|
id: address2.id, |
|
1038
|
address_name: "Under Construction", |
|
1039
|
}), |
|
1040
|
]) |
|
1041
|
) |
|
1042
|
}) |
|
1043
|
|
|
1044
|
it("should fail when updating address to a default shipping address when one already exists", async () => { |
|
1045
|
const customer = await service.createCustomers({ |
|
1046
|
first_name: "John", |
|
1047
|
last_name: "Doe", |
|
1048
|
addresses: [ |
|
1049
|
{ |
|
1050
|
address_name: "Home", |
|
1051
|
address_1: "123 Main St", |
|
1052
|
is_default_shipping: true, |
|
1053
|
}, |
|
1054
|
], |
|
1055
|
}) |
|
1056
|
const address = await service.createCustomerAddresses({ |
|
1057
|
customer_id: customer.id, |
|
1058
|
address_name: "Work", |
|
1059
|
address_1: "456 Main St", |
|
1060
|
}) |
|
1061
|
|
|
1062
|
await expect( |
|
1063
|
service.updateCustomerAddresses(address.id, { |
|
1064
|
is_default_shipping: true, |
|
1065
|
}) |
|
1066
|
).rejects.toThrow( |
|
1067
|
/Customer address with customer_id: .*? already exists./ |
|
1068
|
) |
|
1069
|
}) |
|
1070
|
}) |
|
1071
|
|
|
1072
|
describe("listCustomerAddresses", () => { |
|
1073
|
it("should list all addresses for a customer", async () => { |
|
1074
|
const customer = await service.createCustomers({ |
|
1075
|
first_name: "John", |
|
1076
|
last_name: "Doe", |
|
1077
|
}) |
|
1078
|
const [address1, address2] = await service.createCustomerAddresses([ |
|
1079
|
{ |
|
1080
|
customer_id: customer.id, |
|
1081
|
address_name: "Home", |
|
1082
|
address_1: "123 Main St", |
|
1083
|
}, |
|
1084
|
{ |
|
1085
|
customer_id: customer.id, |
|
1086
|
address_name: "Work", |
|
1087
|
|
|
1088
|
address_1: "456 Main St", |
|
1089
|
}, |
|
1090
|
]) |
|
1091
|
|
|
1092
|
const addresses = await service.listCustomerAddresses({ |
|
1093
|
customer_id: customer.id, |
|
1094
|
}) |
|
1095
|
|
|
1096
|
expect(addresses).toEqual( |
|
1097
|
expect.arrayContaining([ |
|
1098
|
expect.objectContaining({ |
|
1099
|
id: address1.id, |
|
1100
|
address_name: "Home", |
|
1101
|
address_1: "123 Main St", |
|
1102
|
}), |
|
1103
|
expect.objectContaining({ |
|
1104
|
id: address2.id, |
|
1105
|
address_name: "Work", |
|
1106
|
address_1: "456 Main St", |
|
1107
|
}), |
|
1108
|
]) |
|
1109
|
) |
|
1110
|
}) |
|
1111
|
}) |
|
1112
|
|
|
1113
|
describe("removeCustomerFromGroup", () => { |
|
1114
|
it("should remove a single customer from a group", async () => { |
|
1115
|
// Creating a customer and a group |
|
1116
|
const [customer] = await service.createCustomers([ |
|
1117
|
{ |
|
1118
|
first_name: "John", |
|
1119
|
last_name: "Doe", |
|
1120
|
email: "john.doe@example.com", |
|
1121
|
}, |
|
1122
|
]) |
|
1123
|
const [group] = await service.createCustomerGroups([{ name: "VIP" }]) |
|
1124
|
|
|
1125
|
// Adding the customer to the group |
|
1126
|
await service.addCustomerToGroup({ |
|
1127
|
customer_id: customer.id, |
|
1128
|
customer_group_id: group.id, |
|
1129
|
}) |
|
1130
|
|
|
1131
|
const [customerInGroup] = await service.listCustomers( |
|
1132
|
{ id: customer.id }, |
|
1133
|
{ relations: ["groups"] } |
|
1134
|
) |
|
1135
|
expect(customerInGroup.groups).toEqual([ |
|
1136
|
expect.objectContaining({ id: group.id }), |
|
1137
|
]) |
|
1138
|
|
|
1139
|
// Removing the customer from the group |
|
1140
|
await service.removeCustomerFromGroup({ |
|
1141
|
customer_id: customer.id, |
|
1142
|
customer_group_id: group.id, |
|
1143
|
}) |
|
1144
|
|
|
1145
|
const [updatedCustomer] = await service.listCustomers( |
|
1146
|
{ id: customer.id }, |
|
1147
|
{ relations: ["groups"] } |
|
1148
|
) |
|
1149
|
expect(updatedCustomer.groups).toEqual([]) |
|
1150
|
}) |
|
1151
|
|
|
1152
|
it("should remove multiple customers from groups", async () => { |
|
1153
|
// Creating multiple customers and groups |
|
1154
|
const customers = await service.createCustomers([ |
|
1155
|
{ |
|
1156
|
first_name: "John", |
|
1157
|
last_name: "Doe", |
|
1158
|
email: "john.doe@example.com", |
|
1159
|
}, |
|
1160
|
{ |
|
1161
|
first_name: "Jane", |
|
1162
|
last_name: "Smith", |
|
1163
|
email: "jane.smith@example.com", |
|
1164
|
}, |
|
1165
|
]) |
|
1166
|
const groups = await service.createCustomerGroups([ |
|
1167
|
{ name: "VIP" }, |
|
1168
|
{ name: "Regular" }, |
|
1169
|
]) |
|
1170
|
|
|
1171
|
// Adding customers to groups |
|
1172
|
const pairsToAdd = [ |
|
1173
|
{ customer_id: customers[0].id, customer_group_id: groups[0].id }, |
|
1174
|
{ customer_id: customers[1].id, customer_group_id: groups[1].id }, |
|
1175
|
] |
|
1176
|
await service.addCustomerToGroup(pairsToAdd) |
|
1177
|
|
|
1178
|
// Removing customers from groups |
|
1179
|
const pairsToRemove = [ |
|
1180
|
{ customer_id: customers[0].id, customer_group_id: groups[0].id }, |
|
1181
|
{ customer_id: customers[1].id, customer_group_id: groups[1].id }, |
|
1182
|
] |
|
1183
|
await service.removeCustomerFromGroup(pairsToRemove) |
|
1184
|
|
|
1185
|
// Verification for each customer |
|
1186
|
for (const pair of pairsToRemove) { |
|
1187
|
const [updatedCustomer] = await service.listCustomers( |
|
1188
|
{ id: pair.customer_id }, |
|
1189
|
{ relations: ["groups"] } |
|
1190
|
) |
|
1191
|
expect(updatedCustomer.groups).not.toContainEqual( |
|
1192
|
expect.objectContaining({ id: pair.customer_group_id }) |
|
1193
|
) |
|
1194
|
} |
|
1195
|
}) |
|
1196
|
}) |
|
1197
|
|
|
1198
|
describe("softDelete", () => { |
|
1199
|
it("should soft delete a single customer", async () => { |
|
1200
|
const [customer] = await service.createCustomers([ |
|
1201
|
{ first_name: "John", last_name: "Doe" }, |
|
1202
|
]) |
|
1203
|
await service.softDeleteCustomers([customer.id]) |
|
1204
|
|
|
1205
|
const res = await service.listCustomers({ id: customer.id }) |
|
1206
|
expect(res.length).toBe(0) |
|
1207
|
|
|
1208
|
const deletedCustomer = await service.retrieveCustomer(customer.id, { |
|
1209
|
withDeleted: true, |
|
1210
|
}) |
|
1211
|
|
|
1212
|
expect(deletedCustomer.deleted_at).not.toBeNull() |
|
1213
|
}) |
|
1214
|
|
|
1215
|
it("should soft delete multiple customers", async () => { |
|
1216
|
const customers = await service.createCustomers([ |
|
1217
|
{ first_name: "John", last_name: "Doe" }, |
|
1218
|
{ first_name: "Jane", last_name: "Smith" }, |
|
1219
|
]) |
|
1220
|
const customerIds = customers.map((customer) => customer.id) |
|
1221
|
await service.softDeleteCustomers(customerIds) |
|
1222
|
|
|
1223
|
const res = await service.listCustomers({ id: customerIds }) |
|
1224
|
expect(res.length).toBe(0) |
|
1225
|
|
|
1226
|
const deletedCustomers = await service.listCustomers( |
|
1227
|
{ id: customerIds }, |
|
1228
|
{ withDeleted: true } |
|
1229
|
) |
|
1230
|
expect(deletedCustomers.length).toBe(2) |
|
1231
|
}) |
|
1232
|
|
|
1233
|
it("should remove customer in group relation", async () => { |
|
1234
|
// Creating a customer and a group |
|
1235
|
const customer = await service.createCustomers({ |
|
1236
|
first_name: "John", |
|
1237
|
last_name: "Doe", |
|
1238
|
}) |
|
1239
|
const group = await service.createCustomerGroups({ name: "VIP" }) |
|
1240
|
|
|
1241
|
// Adding the customer to the group |
|
1242
|
await service.addCustomerToGroup({ |
|
1243
|
customer_id: customer.id, |
|
1244
|
customer_group_id: group.id, |
|
1245
|
}) |
|
1246
|
|
|
1247
|
await service.softDeleteCustomers([customer.id]) |
|
1248
|
|
|
1249
|
const resGroup = await service.retrieveCustomerGroup(group.id, { |
|
1250
|
relations: ["customers"], |
|
1251
|
}) |
|
1252
|
expect(resGroup.customers?.length).toBe(0) |
|
1253
|
}) |
|
1254
|
}) |
|
1255
|
|
|
1256
|
describe("restore", () => { |
|
1257
|
it("should restore a single customer", async () => { |
|
1258
|
const [customer] = await service.createCustomers([ |
|
1259
|
{ first_name: "John", last_name: "Doe" }, |
|
1260
|
]) |
|
1261
|
await service.softDeleteCustomers([customer.id]) |
|
1262
|
|
|
1263
|
const res = await service.listCustomers({ id: customer.id }) |
|
1264
|
expect(res.length).toBe(0) |
|
1265
|
|
|
1266
|
await service.restoreCustomers([customer.id]) |
|
1267
|
|
|
1268
|
const restoredCustomer = await service.retrieveCustomer(customer.id, { |
|
1269
|
withDeleted: true, |
|
1270
|
}) |
|
1271
|
expect(restoredCustomer.deleted_at).toBeNull() |
|
1272
|
}) |
|
1273
|
|
|
1274
|
it("should restore multiple customers", async () => { |
|
1275
|
const customers = await service.createCustomers([ |
|
1276
|
{ first_name: "John", last_name: "Doe" }, |
|
1277
|
{ first_name: "Jane", last_name: "Smith" }, |
|
1278
|
]) |
|
1279
|
const customerIds = customers.map((customer) => customer.id) |
|
1280
|
await service.softDeleteCustomers(customerIds) |
|
1281
|
|
|
1282
|
const res = await service.listCustomers({ id: customerIds }) |
|
1283
|
expect(res.length).toBe(0) |
|
1284
|
|
|
1285
|
await service.restoreCustomers(customerIds) |
|
1286
|
|
|
1287
|
const restoredCustomers = await service.listCustomers( |
|
1288
|
{ id: customerIds }, |
|
1289
|
{ withDeleted: true } |
|
1290
|
) |
|
1291
|
expect(restoredCustomers.length).toBe(2) |
|
1292
|
}) |
|
1293
|
}) |
|
1294
|
|
|
1295
|
describe("softDeleteCustomerGroup", () => { |
|
1296
|
it("should soft delete a single customer group", async () => { |
|
1297
|
const [group] = await service.createCustomerGroups([{ name: "VIP" }]) |
|
1298
|
await service.softDeleteCustomerGroups([group.id]) |
|
1299
|
|
|
1300
|
const res = await service.listCustomerGroups({ id: group.id }) |
|
1301
|
expect(res.length).toBe(0) |
|
1302
|
|
|
1303
|
const deletedGroup = await service.retrieveCustomerGroup(group.id, { |
|
1304
|
withDeleted: true, |
|
1305
|
}) |
|
1306
|
|
|
1307
|
expect(deletedGroup.deleted_at).not.toBeNull() |
|
1308
|
}) |
|
1309
|
|
|
1310
|
it("should soft delete multiple customer groups", async () => { |
|
1311
|
const groups = await service.createCustomerGroups([ |
|
1312
|
{ name: "VIP" }, |
|
1313
|
{ name: "Regular" }, |
|
1314
|
]) |
|
1315
|
const groupIds = groups.map((group) => group.id) |
|
1316
|
await service.softDeleteCustomerGroups(groupIds) |
|
1317
|
|
|
1318
|
const res = await service.listCustomerGroups({ id: groupIds }) |
|
1319
|
expect(res.length).toBe(0) |
|
1320
|
|
|
1321
|
const deletedGroups = await service.listCustomerGroups( |
|
1322
|
{ id: groupIds }, |
|
1323
|
{ withDeleted: true } |
|
1324
|
) |
|
1325
|
expect(deletedGroups.length).toBe(2) |
|
1326
|
}) |
|
1327
|
}) |
|
1328
|
|
|
1329
|
describe("restoreCustomerGroup", () => { |
|
1330
|
it("should restore a single customer group", async () => { |
|
1331
|
const [group] = await service.createCustomerGroups([{ name: "VIP" }]) |
|
1332
|
await service.softDeleteCustomerGroups([group.id]) |
|
1333
|
|
|
1334
|
const res = await service.listCustomerGroups({ id: group.id }) |
|
1335
|
expect(res.length).toBe(0) |
|
1336
|
|
|
1337
|
await service.restoreCustomerGroups([group.id]) |
|
1338
|
|
|
1339
|
const restoredGroup = await service.retrieveCustomerGroup(group.id, { |
|
1340
|
withDeleted: true, |
|
1341
|
}) |
|
1342
|
expect(restoredGroup.deleted_at).toBeNull() |
|
1343
|
}) |
|
1344
|
|
|
1345
|
it("should restore multiple customer groups", async () => { |
|
1346
|
const groups = await service.createCustomerGroups([ |
|
1347
|
{ name: "VIP" }, |
|
1348
|
{ name: "Regular" }, |
|
1349
|
]) |
|
1350
|
const groupIds = groups.map((group) => group.id) |
|
1351
|
await service.softDeleteCustomerGroups(groupIds) |
|
1352
|
|
|
1353
|
const res = await service.listCustomerGroups({ id: groupIds }) |
|
1354
|
expect(res.length).toBe(0) |
|
1355
|
|
|
1356
|
await service.restoreCustomerGroups(groupIds) |
|
1357
|
|
|
1358
|
const restoredGroups = await service.listCustomerGroups( |
|
1359
|
{ id: groupIds }, |
|
1360
|
{ withDeleted: true } |
|
1361
|
) |
|
1362
|
expect(restoredGroups.length).toBe(2) |
|
1363
|
}) |
|
1364
|
}) |
|
1365
|
}) |
|
1366
|
}, |
|
1367
|
}) |