|
1
|
import { |
|
2
|
CreateServiceZoneDTO, |
|
3
|
GeoZoneDTO, |
|
4
|
IFulfillmentModuleService, |
|
5
|
UpdateServiceZoneDTO, |
|
6
|
} from "@medusajs/framework/types" |
|
7
|
import { |
|
8
|
FulfillmentEvents, |
|
9
|
GeoZoneType, |
|
10
|
Modules, |
|
11
|
} from "@medusajs/framework/utils" |
|
12
|
import { |
|
13
|
MockEventBusService, |
|
14
|
moduleIntegrationTestRunner, |
|
15
|
} from "@medusajs/test-utils" |
|
16
|
import { buildExpectedEventMessageShape } from "../../__fixtures__" |
|
17
|
|
|
18
|
jest.setTimeout(100000) |
|
19
|
|
|
20
|
moduleIntegrationTestRunner<IFulfillmentModuleService>({ |
|
21
|
moduleName: Modules.FULFILLMENT, |
|
22
|
testSuite: ({ service }) => { |
|
23
|
let eventBusEmitSpy |
|
24
|
|
|
25
|
beforeEach(() => { |
|
26
|
eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit") |
|
27
|
}) |
|
28
|
|
|
29
|
afterEach(() => { |
|
30
|
jest.clearAllMocks() |
|
31
|
}) |
|
32
|
|
|
33
|
describe("Fulfillment Module Service", () => { |
|
34
|
describe("read", () => { |
|
35
|
it("should list service zones with a filter", async function () { |
|
36
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
37
|
name: "test", |
|
38
|
type: "test-type", |
|
39
|
}) |
|
40
|
|
|
41
|
const createdZone1 = await service.createServiceZones({ |
|
42
|
name: "test", |
|
43
|
fulfillment_set_id: fulfillmentSet.id, |
|
44
|
}) |
|
45
|
const createdZone2 = await service.createServiceZones({ |
|
46
|
name: "test2", |
|
47
|
fulfillment_set_id: fulfillmentSet.id, |
|
48
|
geo_zones: [ |
|
49
|
{ |
|
50
|
type: GeoZoneType.COUNTRY, |
|
51
|
country_code: "fr", |
|
52
|
}, |
|
53
|
], |
|
54
|
}) |
|
55
|
|
|
56
|
let listedZones = await service.listServiceZones({ |
|
57
|
name: createdZone2.name, |
|
58
|
}) |
|
59
|
|
|
60
|
expect(listedZones).toEqual( |
|
61
|
expect.arrayContaining([ |
|
62
|
expect.objectContaining({ id: createdZone2.id }), |
|
63
|
]) |
|
64
|
) |
|
65
|
expect(listedZones).not.toEqual( |
|
66
|
expect.arrayContaining([ |
|
67
|
expect.objectContaining({ id: createdZone1.id }), |
|
68
|
]) |
|
69
|
) |
|
70
|
|
|
71
|
listedZones = await service.listServiceZones({ |
|
72
|
geo_zones: { country_code: "fr" }, |
|
73
|
}) |
|
74
|
|
|
75
|
expect(listedZones).toEqual( |
|
76
|
expect.arrayContaining([ |
|
77
|
expect.objectContaining({ id: createdZone2.id }), |
|
78
|
]) |
|
79
|
) |
|
80
|
expect(listedZones).not.toEqual( |
|
81
|
expect.arrayContaining([ |
|
82
|
expect.objectContaining({ id: createdZone1.id }), |
|
83
|
]) |
|
84
|
) |
|
85
|
}) |
|
86
|
}) |
|
87
|
|
|
88
|
describe("mutations", () => { |
|
89
|
describe("on create", () => { |
|
90
|
it("should create a new service zone", async function () { |
|
91
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
92
|
name: "test", |
|
93
|
type: "test-type", |
|
94
|
}) |
|
95
|
|
|
96
|
const data: CreateServiceZoneDTO = { |
|
97
|
name: "test", |
|
98
|
fulfillment_set_id: fulfillmentSet.id, |
|
99
|
geo_zones: [ |
|
100
|
{ |
|
101
|
type: GeoZoneType.COUNTRY, |
|
102
|
country_code: "fr", |
|
103
|
}, |
|
104
|
], |
|
105
|
} |
|
106
|
|
|
107
|
const serviceZone = await service.createServiceZones(data) |
|
108
|
|
|
109
|
expect(serviceZone).toEqual( |
|
110
|
expect.objectContaining({ |
|
111
|
id: expect.any(String), |
|
112
|
name: data.name, |
|
113
|
geo_zones: expect.arrayContaining([ |
|
114
|
expect.objectContaining({ |
|
115
|
type: (data.geo_zones![0] as GeoZoneDTO).type, |
|
116
|
country_code: (data.geo_zones![0] as GeoZoneDTO) |
|
117
|
.country_code, |
|
118
|
}), |
|
119
|
]), |
|
120
|
}) |
|
121
|
) |
|
122
|
}) |
|
123
|
|
|
124
|
it("should create a collection of service zones", async function () { |
|
125
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
126
|
name: "test", |
|
127
|
type: "test-type", |
|
128
|
}) |
|
129
|
|
|
130
|
const data: CreateServiceZoneDTO[] = [ |
|
131
|
{ |
|
132
|
name: "test", |
|
133
|
fulfillment_set_id: fulfillmentSet.id, |
|
134
|
geo_zones: [ |
|
135
|
{ |
|
136
|
type: GeoZoneType.COUNTRY, |
|
137
|
country_code: "fr", |
|
138
|
}, |
|
139
|
], |
|
140
|
}, |
|
141
|
{ |
|
142
|
name: "test2", |
|
143
|
fulfillment_set_id: fulfillmentSet.id, |
|
144
|
geo_zones: [ |
|
145
|
{ |
|
146
|
type: GeoZoneType.COUNTRY, |
|
147
|
country_code: "fr", |
|
148
|
}, |
|
149
|
], |
|
150
|
}, |
|
151
|
{ |
|
152
|
name: "test3", |
|
153
|
fulfillment_set_id: fulfillmentSet.id, |
|
154
|
geo_zones: [ |
|
155
|
{ |
|
156
|
type: GeoZoneType.COUNTRY, |
|
157
|
country_code: "uk", |
|
158
|
}, |
|
159
|
], |
|
160
|
}, |
|
161
|
] |
|
162
|
|
|
163
|
const serviceZones = await service.createServiceZones(data) |
|
164
|
|
|
165
|
expect(serviceZones).toHaveLength(3) |
|
166
|
|
|
167
|
let i = 0 |
|
168
|
for (const data_ of data) { |
|
169
|
expect(serviceZones[i]).toEqual( |
|
170
|
expect.objectContaining({ |
|
171
|
id: expect.any(String), |
|
172
|
name: data_.name, |
|
173
|
geo_zones: expect.arrayContaining([ |
|
174
|
expect.objectContaining({ |
|
175
|
type: (data_.geo_zones![0] as GeoZoneDTO).type, |
|
176
|
country_code: (data_.geo_zones![0] as GeoZoneDTO) |
|
177
|
.country_code, |
|
178
|
}), |
|
179
|
]), |
|
180
|
}) |
|
181
|
) |
|
182
|
++i |
|
183
|
} |
|
184
|
}) |
|
185
|
|
|
186
|
it("should fail on duplicated service zone name", async function () { |
|
187
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
188
|
name: "test", |
|
189
|
type: "test-type", |
|
190
|
}) |
|
191
|
|
|
192
|
const data: CreateServiceZoneDTO = { |
|
193
|
name: "test", |
|
194
|
fulfillment_set_id: fulfillmentSet.id, |
|
195
|
geo_zones: [ |
|
196
|
{ |
|
197
|
type: GeoZoneType.COUNTRY, |
|
198
|
country_code: "fr", |
|
199
|
}, |
|
200
|
], |
|
201
|
} |
|
202
|
|
|
203
|
await service.createServiceZones(data) |
|
204
|
const err = await service.createServiceZones(data).catch((e) => e) |
|
205
|
|
|
206
|
expect(err).toBeDefined() |
|
207
|
expect(err.message).toContain("exists") |
|
208
|
}) |
|
209
|
|
|
210
|
it("should fail on creating a service zone and new geo zones that are not valid", async function () { |
|
211
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
212
|
name: "test", |
|
213
|
type: "test-type", |
|
214
|
}) |
|
215
|
|
|
216
|
let data: CreateServiceZoneDTO = { |
|
217
|
name: "test", |
|
218
|
fulfillment_set_id: fulfillmentSet.id, |
|
219
|
geo_zones: [ |
|
220
|
{ |
|
221
|
type: GeoZoneType.PROVINCE, |
|
222
|
country_code: "fr", |
|
223
|
} as any, |
|
224
|
], |
|
225
|
} |
|
226
|
|
|
227
|
let err = await service.createServiceZones(data).catch((e) => e) |
|
228
|
expect(err.message).toBe( |
|
229
|
"Missing required property province_code for geo zone type province" |
|
230
|
) |
|
231
|
|
|
232
|
data = { |
|
233
|
name: "test", |
|
234
|
fulfillment_set_id: fulfillmentSet.id, |
|
235
|
geo_zones: [ |
|
236
|
{ |
|
237
|
type: GeoZoneType.CITY, |
|
238
|
country_code: "fr", |
|
239
|
province_code: "test", |
|
240
|
} as any, |
|
241
|
], |
|
242
|
} |
|
243
|
|
|
244
|
err = await service.createServiceZones(data).catch((e) => e) |
|
245
|
expect(err.message).toBe( |
|
246
|
"Missing required property city for geo zone type city" |
|
247
|
) |
|
248
|
|
|
249
|
data = { |
|
250
|
name: "test", |
|
251
|
fulfillment_set_id: fulfillmentSet.id, |
|
252
|
geo_zones: [ |
|
253
|
{ |
|
254
|
type: GeoZoneType.ZIP, |
|
255
|
postal_expression: "test", |
|
256
|
} as any, |
|
257
|
], |
|
258
|
} |
|
259
|
|
|
260
|
err = await service.createServiceZones(data).catch((e) => e) |
|
261
|
expect(err.message).toBe( |
|
262
|
"Missing required property country_code for geo zone type zip" |
|
263
|
) |
|
264
|
|
|
265
|
data = { |
|
266
|
name: "test", |
|
267
|
fulfillment_set_id: fulfillmentSet.id, |
|
268
|
geo_zones: [ |
|
269
|
{ |
|
270
|
type: "unknown", |
|
271
|
postal_expression: "test", |
|
272
|
} as any, |
|
273
|
], |
|
274
|
} |
|
275
|
|
|
276
|
err = await service.createServiceZones(data).catch((e) => e) |
|
277
|
expect(err.message).toBe(`Invalid geo zone type: unknown`) |
|
278
|
}) |
|
279
|
}) |
|
280
|
|
|
281
|
describe("on update", () => { |
|
282
|
it("should update an existing service zone", async function () { |
|
283
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
284
|
name: "test", |
|
285
|
type: "test-type", |
|
286
|
}) |
|
287
|
|
|
288
|
const createData: CreateServiceZoneDTO = { |
|
289
|
name: "service-zone-test", |
|
290
|
fulfillment_set_id: fulfillmentSet.id, |
|
291
|
geo_zones: [ |
|
292
|
{ |
|
293
|
type: GeoZoneType.COUNTRY, |
|
294
|
country_code: "fr", |
|
295
|
}, |
|
296
|
{ |
|
297
|
type: GeoZoneType.COUNTRY, |
|
298
|
country_code: "us", |
|
299
|
}, |
|
300
|
{ |
|
301
|
type: GeoZoneType.COUNTRY, |
|
302
|
country_code: "uk", |
|
303
|
}, |
|
304
|
], |
|
305
|
} |
|
306
|
|
|
307
|
const createdServiceZone = await service.createServiceZones( |
|
308
|
createData |
|
309
|
) |
|
310
|
|
|
311
|
const usGeoZone = createdServiceZone.geo_zones.find( |
|
312
|
(geoZone) => geoZone.country_code === "us" |
|
313
|
)! |
|
314
|
const frGeoZone = createdServiceZone.geo_zones.find( |
|
315
|
(geoZone) => geoZone.country_code === "fr" |
|
316
|
)! |
|
317
|
const ukGeoZone = createdServiceZone.geo_zones.find( |
|
318
|
(geoZone) => geoZone.country_code === "uk" |
|
319
|
)! |
|
320
|
|
|
321
|
const updateData: UpdateServiceZoneDTO = { |
|
322
|
name: "updated-service-zone-test", |
|
323
|
geo_zones: [ |
|
324
|
{ |
|
325
|
id: usGeoZone.id, |
|
326
|
type: GeoZoneType.COUNTRY, |
|
327
|
country_code: "es", |
|
328
|
}, |
|
329
|
{ |
|
330
|
type: GeoZoneType.COUNTRY, |
|
331
|
country_code: "ch", |
|
332
|
}, |
|
333
|
{ id: frGeoZone.id }, |
|
334
|
], |
|
335
|
} |
|
336
|
|
|
337
|
jest.clearAllMocks() |
|
338
|
|
|
339
|
const updatedServiceZone = await service.updateServiceZones( |
|
340
|
createdServiceZone.id, |
|
341
|
updateData |
|
342
|
) |
|
343
|
|
|
344
|
expect(updatedServiceZone.geo_zones).toHaveLength(3) |
|
345
|
|
|
346
|
expect(updatedServiceZone).toEqual( |
|
347
|
expect.objectContaining({ |
|
348
|
id: createdServiceZone.id, |
|
349
|
name: updateData.name, |
|
350
|
geo_zones: expect.arrayContaining([ |
|
351
|
expect.objectContaining({ |
|
352
|
id: frGeoZone.id, |
|
353
|
}), |
|
354
|
expect.objectContaining({ |
|
355
|
id: usGeoZone.id, |
|
356
|
type: GeoZoneType.COUNTRY, |
|
357
|
country_code: "es", |
|
358
|
}), |
|
359
|
expect.objectContaining({ |
|
360
|
id: expect.any(String), |
|
361
|
type: GeoZoneType.COUNTRY, |
|
362
|
country_code: "ch", |
|
363
|
}), |
|
364
|
]), |
|
365
|
}) |
|
366
|
) |
|
367
|
|
|
368
|
const chGeoZone = updatedServiceZone.geo_zones.find( |
|
369
|
(geoZone) => geoZone.country_code === "ch" |
|
370
|
)! |
|
371
|
|
|
372
|
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(4) |
|
373
|
expect(eventBusEmitSpy).toHaveBeenCalledWith( |
|
374
|
expect.arrayContaining([ |
|
375
|
buildExpectedEventMessageShape({ |
|
376
|
eventName: FulfillmentEvents.GEO_ZONE_DELETED, |
|
377
|
action: "deleted", |
|
378
|
object: "geo_zone", |
|
379
|
data: { id: ukGeoZone.id }, |
|
380
|
}), |
|
381
|
buildExpectedEventMessageShape({ |
|
382
|
eventName: FulfillmentEvents.SERVICE_ZONE_UPDATED, |
|
383
|
action: "updated", |
|
384
|
object: "service_zone", |
|
385
|
data: { id: updatedServiceZone.id }, |
|
386
|
}), |
|
387
|
buildExpectedEventMessageShape({ |
|
388
|
eventName: FulfillmentEvents.GEO_ZONE_CREATED, |
|
389
|
action: "created", |
|
390
|
object: "geo_zone", |
|
391
|
data: { id: chGeoZone.id }, |
|
392
|
}), |
|
393
|
buildExpectedEventMessageShape({ |
|
394
|
eventName: FulfillmentEvents.GEO_ZONE_UPDATED, |
|
395
|
action: "updated", |
|
396
|
object: "geo_zone", |
|
397
|
data: { id: usGeoZone.id }, |
|
398
|
}), |
|
399
|
]), |
|
400
|
{ |
|
401
|
internal: true, |
|
402
|
} |
|
403
|
) |
|
404
|
}) |
|
405
|
|
|
406
|
it("should fail on duplicated service zone name", async function () { |
|
407
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
408
|
name: "test", |
|
409
|
type: "test-type", |
|
410
|
}) |
|
411
|
|
|
412
|
const createData: CreateServiceZoneDTO[] = [ |
|
413
|
{ |
|
414
|
name: "service-zone-test", |
|
415
|
fulfillment_set_id: fulfillmentSet.id, |
|
416
|
geo_zones: [ |
|
417
|
{ |
|
418
|
type: GeoZoneType.COUNTRY, |
|
419
|
country_code: "fr", |
|
420
|
}, |
|
421
|
], |
|
422
|
}, |
|
423
|
{ |
|
424
|
name: "service-zone-test2", |
|
425
|
fulfillment_set_id: fulfillmentSet.id, |
|
426
|
geo_zones: [ |
|
427
|
{ |
|
428
|
type: GeoZoneType.COUNTRY, |
|
429
|
country_code: "us", |
|
430
|
}, |
|
431
|
], |
|
432
|
}, |
|
433
|
] |
|
434
|
|
|
435
|
const createdServiceZones = await service.createServiceZones( |
|
436
|
createData |
|
437
|
) |
|
438
|
|
|
439
|
const updateData: UpdateServiceZoneDTO = { |
|
440
|
name: "service-zone-test", |
|
441
|
geo_zones: [ |
|
442
|
{ |
|
443
|
type: GeoZoneType.COUNTRY, |
|
444
|
country_code: "us", |
|
445
|
}, |
|
446
|
], |
|
447
|
} |
|
448
|
|
|
449
|
const err = await service |
|
450
|
.updateServiceZones(createdServiceZones[1].id, updateData) |
|
451
|
.catch((e) => e) |
|
452
|
|
|
453
|
expect(err).toBeDefined() |
|
454
|
expect(err.message).toContain("exists") |
|
455
|
}) |
|
456
|
}) |
|
457
|
|
|
458
|
describe("on upsert", () => { |
|
459
|
it("should upsert a collection of service zones", async function () { |
|
460
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
461
|
name: "test", |
|
462
|
type: "test-type", |
|
463
|
}) |
|
464
|
|
|
465
|
const createData: CreateServiceZoneDTO[] = [ |
|
466
|
{ |
|
467
|
name: "service-zone-test", |
|
468
|
fulfillment_set_id: fulfillmentSet.id, |
|
469
|
geo_zones: [ |
|
470
|
{ |
|
471
|
type: GeoZoneType.COUNTRY, |
|
472
|
country_code: "fr", |
|
473
|
}, |
|
474
|
], |
|
475
|
}, |
|
476
|
{ |
|
477
|
name: "service-zone-test2", |
|
478
|
fulfillment_set_id: fulfillmentSet.id, |
|
479
|
geo_zones: [ |
|
480
|
{ |
|
481
|
type: GeoZoneType.COUNTRY, |
|
482
|
country_code: "us", |
|
483
|
}, |
|
484
|
], |
|
485
|
}, |
|
486
|
] |
|
487
|
|
|
488
|
const createdServiceZones = await service.createServiceZones( |
|
489
|
createData |
|
490
|
) |
|
491
|
|
|
492
|
const updateData: UpdateServiceZoneDTO[] = createdServiceZones.map( |
|
493
|
(serviceZone, index) => ({ |
|
494
|
id: serviceZone.id, |
|
495
|
name: `updated-service-zone-test${index + 1}`, |
|
496
|
geo_zones: [ |
|
497
|
{ |
|
498
|
type: GeoZoneType.COUNTRY, |
|
499
|
country_code: index % 2 === 0 ? "us" : "fr", |
|
500
|
}, |
|
501
|
], |
|
502
|
}) |
|
503
|
) |
|
504
|
|
|
505
|
jest.clearAllMocks() |
|
506
|
|
|
507
|
const updatedServiceZones = await service.upsertServiceZones( |
|
508
|
updateData |
|
509
|
) |
|
510
|
|
|
511
|
expect(updatedServiceZones).toHaveLength(2) |
|
512
|
|
|
513
|
expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(6) // Since the update only calls create and update which are already tested, only check the length |
|
514
|
|
|
515
|
for (const data_ of updateData) { |
|
516
|
const expectedServiceZone = updatedServiceZones.find( |
|
517
|
(serviceZone) => serviceZone.id === data_.id |
|
518
|
) |
|
519
|
expect(expectedServiceZone).toEqual( |
|
520
|
expect.objectContaining({ |
|
521
|
id: data_.id, |
|
522
|
name: data_.name, |
|
523
|
geo_zones: expect.arrayContaining([ |
|
524
|
expect.objectContaining({ |
|
525
|
type: (data_.geo_zones![0] as GeoZoneDTO).type, |
|
526
|
country_code: (data_.geo_zones![0] as GeoZoneDTO) |
|
527
|
.country_code, |
|
528
|
}), |
|
529
|
]), |
|
530
|
}) |
|
531
|
) |
|
532
|
} |
|
533
|
}) |
|
534
|
}) |
|
535
|
}) |
|
536
|
}) |
|
537
|
}, |
|
538
|
}) |