|
1
|
import { |
|
2
|
createShippingOptionsWorkflow, |
|
3
|
updateShippingOptionsWorkflow, |
|
4
|
} from "@medusajs/core-flows" |
|
5
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
6
|
import { |
|
7
|
FulfillmentSetDTO, |
|
8
|
FulfillmentWorkflow, |
|
9
|
IFulfillmentModuleService, |
|
10
|
IRegionModuleService, |
|
11
|
ServiceZoneDTO, |
|
12
|
ShippingProfileDTO, |
|
13
|
UpdateShippingOptionsWorkflowInput, |
|
14
|
} from "@medusajs/types" |
|
15
|
import { |
|
16
|
ContainerRegistrationKeys, |
|
17
|
Modules, |
|
18
|
RuleOperator, |
|
19
|
remoteQueryObjectFromString, |
|
20
|
} from "@medusajs/utils" |
|
21
|
|
|
22
|
jest.setTimeout(100000) |
|
23
|
|
|
24
|
const env = {} |
|
25
|
const provider_id = "manual_test-provider" |
|
26
|
|
|
27
|
medusaIntegrationTestRunner({ |
|
28
|
env, |
|
29
|
testSuite: ({ getContainer }) => { |
|
30
|
let service: IFulfillmentModuleService |
|
31
|
let container |
|
32
|
|
|
33
|
beforeAll(() => { |
|
34
|
container = getContainer() |
|
35
|
service = container.resolve(Modules.FULFILLMENT) |
|
36
|
}) |
|
37
|
|
|
38
|
describe("Fulfillment workflows", () => { |
|
39
|
let fulfillmentSet: FulfillmentSetDTO |
|
40
|
let serviceZone: ServiceZoneDTO |
|
41
|
let shippingProfile: ShippingProfileDTO |
|
42
|
|
|
43
|
beforeEach(async () => { |
|
44
|
shippingProfile = await service.createShippingProfiles({ |
|
45
|
name: "test", |
|
46
|
type: "default", |
|
47
|
}) |
|
48
|
|
|
49
|
fulfillmentSet = await service.createFulfillmentSets({ |
|
50
|
name: "Test fulfillment set", |
|
51
|
type: "manual_test", |
|
52
|
}) |
|
53
|
|
|
54
|
serviceZone = await service.createServiceZones({ |
|
55
|
name: "Test service zone", |
|
56
|
fulfillment_set_id: fulfillmentSet.id, |
|
57
|
geo_zones: [ |
|
58
|
{ |
|
59
|
type: "country", |
|
60
|
country_code: "US", |
|
61
|
}, |
|
62
|
], |
|
63
|
}) |
|
64
|
|
|
65
|
const stockLocationModule = container.resolve(Modules.STOCK_LOCATION) |
|
66
|
|
|
67
|
const location = await stockLocationModule.createStockLocations({ |
|
68
|
name: "Europe", |
|
69
|
}) |
|
70
|
|
|
71
|
const remoteLink = container.resolve( |
|
72
|
ContainerRegistrationKeys.REMOTE_LINK |
|
73
|
) |
|
74
|
|
|
75
|
await remoteLink.create([ |
|
76
|
{ |
|
77
|
[Modules.STOCK_LOCATION]: { |
|
78
|
stock_location_id: location.id, |
|
79
|
}, |
|
80
|
[Modules.FULFILLMENT]: { |
|
81
|
fulfillment_set_id: fulfillmentSet.id, |
|
82
|
}, |
|
83
|
}, |
|
84
|
{ |
|
85
|
[Modules.STOCK_LOCATION]: { |
|
86
|
stock_location_id: location.id, |
|
87
|
}, |
|
88
|
[Modules.FULFILLMENT]: { |
|
89
|
fulfillment_provider_id: "manual_test-provider", |
|
90
|
}, |
|
91
|
}, |
|
92
|
]) |
|
93
|
}) |
|
94
|
|
|
95
|
it("should update shipping options", async () => { |
|
96
|
const regionService = container.resolve( |
|
97
|
Modules.REGION |
|
98
|
) as IRegionModuleService |
|
99
|
|
|
100
|
const [region] = await regionService.createRegions([ |
|
101
|
{ |
|
102
|
name: "Test region", |
|
103
|
currency_code: "eur", |
|
104
|
countries: ["fr"], |
|
105
|
}, |
|
106
|
]) |
|
107
|
|
|
108
|
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput = |
|
109
|
{ |
|
110
|
name: "Test shipping option", |
|
111
|
price_type: "flat", |
|
112
|
service_zone_id: serviceZone.id, |
|
113
|
shipping_profile_id: shippingProfile.id, |
|
114
|
provider_id, |
|
115
|
type: { |
|
116
|
code: "manual-type", |
|
117
|
label: "Manual Type", |
|
118
|
description: "Manual Type Description", |
|
119
|
}, |
|
120
|
prices: [ |
|
121
|
{ |
|
122
|
currency_code: "usd", |
|
123
|
amount: 10, |
|
124
|
}, |
|
125
|
{ |
|
126
|
region_id: region.id, |
|
127
|
amount: 100, |
|
128
|
}, |
|
129
|
{ |
|
130
|
currency_code: "dkk", |
|
131
|
amount: 1000, |
|
132
|
}, |
|
133
|
], |
|
134
|
rules: [ |
|
135
|
{ |
|
136
|
attribute: "total", |
|
137
|
operator: RuleOperator.EQ, |
|
138
|
value: "100", |
|
139
|
}, |
|
140
|
], |
|
141
|
} |
|
142
|
|
|
143
|
const { result } = await createShippingOptionsWorkflow(container).run({ |
|
144
|
input: [shippingOptionData], |
|
145
|
}) |
|
146
|
|
|
147
|
const remoteQuery = container.resolve( |
|
148
|
ContainerRegistrationKeys.REMOTE_QUERY |
|
149
|
) |
|
150
|
|
|
151
|
let remoteQueryObject = remoteQueryObjectFromString({ |
|
152
|
entryPoint: "shipping_option", |
|
153
|
variables: { |
|
154
|
id: result[0].id, |
|
155
|
}, |
|
156
|
fields: [ |
|
157
|
"id", |
|
158
|
"name", |
|
159
|
"price_type", |
|
160
|
"service_zone_id", |
|
161
|
"shipping_profile_id", |
|
162
|
"provider_id", |
|
163
|
"data", |
|
164
|
"metadata", |
|
165
|
"type.*", |
|
166
|
"created_at", |
|
167
|
"updated_at", |
|
168
|
"deleted_at", |
|
169
|
"shipping_option_type_id", |
|
170
|
"prices.*", |
|
171
|
], |
|
172
|
}) |
|
173
|
|
|
174
|
const [createdShippingOption] = await remoteQuery(remoteQueryObject) |
|
175
|
|
|
176
|
const usdPrice = createdShippingOption.prices.find((price) => { |
|
177
|
return price.currency_code === "usd" |
|
178
|
}) |
|
179
|
|
|
180
|
const dkkPrice = createdShippingOption.prices.find((price) => { |
|
181
|
return price.currency_code === "dkk" |
|
182
|
}) |
|
183
|
|
|
184
|
const updateData: UpdateShippingOptionsWorkflowInput = { |
|
185
|
id: createdShippingOption.id, |
|
186
|
name: "Test shipping option", |
|
187
|
price_type: "flat", |
|
188
|
type: { |
|
189
|
code: "manual-type", |
|
190
|
label: "Manual Type", |
|
191
|
description: "Manual Type Description", |
|
192
|
}, |
|
193
|
prices: [ |
|
194
|
// We keep the usd price as is |
|
195
|
// update the dkk price to 100 |
|
196
|
// delete the third price eur |
|
197
|
// create a new eur one instead |
|
198
|
usdPrice, |
|
199
|
{ |
|
200
|
...dkkPrice, |
|
201
|
amount: 100, |
|
202
|
}, |
|
203
|
{ |
|
204
|
region_id: region.id, |
|
205
|
amount: 1000, |
|
206
|
}, |
|
207
|
], |
|
208
|
} |
|
209
|
|
|
210
|
await updateShippingOptionsWorkflow(container).run({ |
|
211
|
input: [updateData], |
|
212
|
}) |
|
213
|
|
|
214
|
const [updatedShippingOption] = await remoteQuery(remoteQueryObject) |
|
215
|
|
|
216
|
const prices = updatedShippingOption.prices |
|
217
|
delete updatedShippingOption.prices |
|
218
|
|
|
219
|
expect(updatedShippingOption).toEqual( |
|
220
|
expect.objectContaining({ |
|
221
|
id: result[0].id, |
|
222
|
name: updateData.name, |
|
223
|
price_type: updateData.price_type, |
|
224
|
service_zone_id: serviceZone.id, |
|
225
|
shipping_profile_id: shippingProfile.id, |
|
226
|
provider_id: provider_id, |
|
227
|
data: null, |
|
228
|
metadata: null, |
|
229
|
type: expect.objectContaining({ |
|
230
|
id: expect.any(String), |
|
231
|
code: updateData.type.code, |
|
232
|
label: updateData.type.label, |
|
233
|
description: updateData.type.description, |
|
234
|
}), |
|
235
|
shipping_option_type_id: expect.any(String), |
|
236
|
}) |
|
237
|
) |
|
238
|
|
|
239
|
expect(prices).toHaveLength(3) |
|
240
|
expect(prices).toContainEqual( |
|
241
|
expect.objectContaining({ |
|
242
|
currency_code: "usd", |
|
243
|
amount: 10, |
|
244
|
}) |
|
245
|
) |
|
246
|
expect(prices).toContainEqual( |
|
247
|
expect.objectContaining({ |
|
248
|
currency_code: "eur", |
|
249
|
amount: 1000, |
|
250
|
}) |
|
251
|
) |
|
252
|
expect(prices).toContainEqual( |
|
253
|
expect.objectContaining({ |
|
254
|
currency_code: "dkk", |
|
255
|
amount: 100, |
|
256
|
}) |
|
257
|
) |
|
258
|
}) |
|
259
|
|
|
260
|
it("should fail on non-existent region update shipping options", async () => { |
|
261
|
const regionService = container.resolve( |
|
262
|
Modules.REGION |
|
263
|
) as IRegionModuleService |
|
264
|
|
|
265
|
const [region] = await regionService.createRegions([ |
|
266
|
{ |
|
267
|
name: "Test region", |
|
268
|
currency_code: "eur", |
|
269
|
countries: ["fr"], |
|
270
|
}, |
|
271
|
]) |
|
272
|
|
|
273
|
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput = |
|
274
|
{ |
|
275
|
name: "Test shipping option", |
|
276
|
price_type: "flat", |
|
277
|
service_zone_id: serviceZone.id, |
|
278
|
shipping_profile_id: shippingProfile.id, |
|
279
|
provider_id, |
|
280
|
type: { |
|
281
|
code: "manual-type", |
|
282
|
label: "Manual Type", |
|
283
|
description: "Manual Type Description", |
|
284
|
}, |
|
285
|
prices: [ |
|
286
|
{ |
|
287
|
currency_code: "usd", |
|
288
|
amount: 10, |
|
289
|
}, |
|
290
|
{ |
|
291
|
region_id: region.id, |
|
292
|
amount: 100, |
|
293
|
}, |
|
294
|
{ |
|
295
|
currency_code: "dkk", |
|
296
|
amount: 1000, |
|
297
|
}, |
|
298
|
], |
|
299
|
rules: [ |
|
300
|
{ |
|
301
|
attribute: "total", |
|
302
|
operator: RuleOperator.EQ, |
|
303
|
value: "100", |
|
304
|
}, |
|
305
|
], |
|
306
|
} |
|
307
|
|
|
308
|
const { result } = await createShippingOptionsWorkflow(container).run({ |
|
309
|
input: [shippingOptionData], |
|
310
|
}) |
|
311
|
|
|
312
|
const remoteQuery = container.resolve( |
|
313
|
ContainerRegistrationKeys.REMOTE_QUERY |
|
314
|
) |
|
315
|
|
|
316
|
let remoteQueryObject = remoteQueryObjectFromString({ |
|
317
|
entryPoint: "shipping_option", |
|
318
|
variables: { |
|
319
|
id: result[0].id, |
|
320
|
}, |
|
321
|
fields: [ |
|
322
|
"id", |
|
323
|
"name", |
|
324
|
"price_type", |
|
325
|
"service_zone_id", |
|
326
|
"shipping_profile_id", |
|
327
|
"provider_id", |
|
328
|
"data", |
|
329
|
"metadata", |
|
330
|
"type.*", |
|
331
|
"created_at", |
|
332
|
"updated_at", |
|
333
|
"deleted_at", |
|
334
|
"shipping_option_type_id", |
|
335
|
"prices.*", |
|
336
|
], |
|
337
|
}) |
|
338
|
|
|
339
|
const [createdShippingOption] = await remoteQuery(remoteQueryObject) |
|
340
|
|
|
341
|
const usdPrice = createdShippingOption.prices.find((price) => { |
|
342
|
return price.currency_code === "usd" |
|
343
|
}) |
|
344
|
|
|
345
|
const dkkPrice = createdShippingOption.prices.find((price) => { |
|
346
|
return price.currency_code === "dkk" |
|
347
|
}) |
|
348
|
|
|
349
|
const updateData: UpdateShippingOptionsWorkflowInput = { |
|
350
|
id: createdShippingOption.id, |
|
351
|
name: "Test shipping option", |
|
352
|
price_type: "flat", |
|
353
|
type: { |
|
354
|
code: "manual-type", |
|
355
|
label: "Manual Type", |
|
356
|
description: "Manual Type Description", |
|
357
|
}, |
|
358
|
prices: [ |
|
359
|
// We keep the usd price as is |
|
360
|
// update the dkk price to 100 |
|
361
|
// delete the third price eur |
|
362
|
// create a new eur one instead |
|
363
|
usdPrice, |
|
364
|
{ |
|
365
|
...dkkPrice, |
|
366
|
amount: 100, |
|
367
|
}, |
|
368
|
{ |
|
369
|
region_id: region.id, |
|
370
|
amount: 1000, |
|
371
|
}, |
|
372
|
], |
|
373
|
} |
|
374
|
|
|
375
|
await regionService.softDeleteRegions([region.id]) |
|
376
|
|
|
377
|
const { errors } = await updateShippingOptionsWorkflow(container).run({ |
|
378
|
input: [updateData], |
|
379
|
throwOnError: false, |
|
380
|
}) |
|
381
|
|
|
382
|
expect(errors[0].error.message).toEqual( |
|
383
|
`Cannot create prices for non-existent regions. Region with ids [${region.id}] were not found.` |
|
384
|
) |
|
385
|
}) |
|
386
|
|
|
387
|
it("should revert the shipping options", async () => { |
|
388
|
const regionService = container.resolve( |
|
389
|
Modules.REGION |
|
390
|
) as IRegionModuleService |
|
391
|
|
|
392
|
const [region] = await regionService.createRegions([ |
|
393
|
{ |
|
394
|
name: "Test region", |
|
395
|
currency_code: "eur", |
|
396
|
countries: ["fr"], |
|
397
|
}, |
|
398
|
]) |
|
399
|
|
|
400
|
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput = |
|
401
|
{ |
|
402
|
name: "Test shipping option", |
|
403
|
price_type: "flat", |
|
404
|
service_zone_id: serviceZone.id, |
|
405
|
shipping_profile_id: shippingProfile.id, |
|
406
|
provider_id, |
|
407
|
type: { |
|
408
|
code: "manual-type", |
|
409
|
label: "Manual Type", |
|
410
|
description: "Manual Type Description", |
|
411
|
}, |
|
412
|
prices: [ |
|
413
|
{ |
|
414
|
currency_code: "usd", |
|
415
|
amount: 10, |
|
416
|
}, |
|
417
|
{ |
|
418
|
region_id: region.id, |
|
419
|
amount: 100, |
|
420
|
}, |
|
421
|
], |
|
422
|
rules: [ |
|
423
|
{ |
|
424
|
attribute: "total", |
|
425
|
operator: RuleOperator.EQ, |
|
426
|
value: "100", |
|
427
|
}, |
|
428
|
], |
|
429
|
} |
|
430
|
|
|
431
|
const updateWorkflow = await updateShippingOptionsWorkflow(container) |
|
432
|
|
|
433
|
updateWorkflow.addAction( |
|
434
|
"throw", |
|
435
|
{ |
|
436
|
invoke: async function failStep() { |
|
437
|
throw new Error(`Failed to update shipping options`) |
|
438
|
}, |
|
439
|
}, |
|
440
|
{ |
|
441
|
noCompensation: true, |
|
442
|
} |
|
443
|
) |
|
444
|
|
|
445
|
const { result } = await createShippingOptionsWorkflow(container).run({ |
|
446
|
input: [shippingOptionData], |
|
447
|
}) |
|
448
|
|
|
449
|
const updateData: UpdateShippingOptionsWorkflowInput = { |
|
450
|
id: result[0].id, |
|
451
|
name: "Test shipping option", |
|
452
|
price_type: "flat", |
|
453
|
type: { |
|
454
|
code: "manual-type", |
|
455
|
label: "Manual Type", |
|
456
|
description: "Manual Type Description", |
|
457
|
}, |
|
458
|
} |
|
459
|
|
|
460
|
const { errors } = await updateWorkflow.run({ |
|
461
|
input: [updateData], |
|
462
|
throwOnError: false, |
|
463
|
}) |
|
464
|
|
|
465
|
expect(errors).toHaveLength(1) |
|
466
|
expect(errors[0].error.message).toEqual( |
|
467
|
`Failed to update shipping options` |
|
468
|
) |
|
469
|
|
|
470
|
const remoteQuery = container.resolve( |
|
471
|
ContainerRegistrationKeys.REMOTE_QUERY |
|
472
|
) |
|
473
|
|
|
474
|
const remoteQueryObject = remoteQueryObjectFromString({ |
|
475
|
entryPoint: "shipping_option", |
|
476
|
fields: [ |
|
477
|
"id", |
|
478
|
"name", |
|
479
|
"price_type", |
|
480
|
"service_zone_id", |
|
481
|
"shipping_profile_id", |
|
482
|
"provider_id", |
|
483
|
"data", |
|
484
|
"metadata", |
|
485
|
"type.*", |
|
486
|
"created_at", |
|
487
|
"updated_at", |
|
488
|
"deleted_at", |
|
489
|
"shipping_option_type_id", |
|
490
|
], |
|
491
|
}) |
|
492
|
|
|
493
|
const createdShippingOptions = await remoteQuery(remoteQueryObject) |
|
494
|
|
|
495
|
expect(createdShippingOptions).toHaveLength(1) |
|
496
|
expect(createdShippingOptions[0]).toEqual( |
|
497
|
expect.objectContaining({ |
|
498
|
name: shippingOptionData.name, |
|
499
|
price_type: shippingOptionData.price_type, |
|
500
|
service_zone_id: serviceZone.id, |
|
501
|
shipping_profile_id: shippingProfile.id, |
|
502
|
provider_id: provider_id, |
|
503
|
data: null, |
|
504
|
metadata: null, |
|
505
|
shipping_option_type_id: expect.any(String), |
|
506
|
type: expect.objectContaining({ |
|
507
|
id: expect.any(String), |
|
508
|
code: shippingOptionData.type.code, |
|
509
|
label: shippingOptionData.type.label, |
|
510
|
description: shippingOptionData.type.description, |
|
511
|
}), |
|
512
|
}) |
|
513
|
) |
|
514
|
}) |
|
515
|
}) |
|
516
|
}, |
|
517
|
}) |