|
1
|
import { |
|
2
|
createOrderFulfillmentWorkflow, |
|
3
|
createOrderShipmentWorkflow, |
|
4
|
createShippingOptionsWorkflow, |
|
5
|
} from "@medusajs/core-flows" |
|
6
|
import { |
|
7
|
medusaIntegrationTestRunner, |
|
8
|
normalizeBigNumbers, |
|
9
|
} from "@medusajs/test-utils" |
|
10
|
import { |
|
11
|
FulfillmentWorkflow, |
|
12
|
IOrderModuleService, |
|
13
|
IRegionModuleService, |
|
14
|
IStockLocationService, |
|
15
|
OrderWorkflow, |
|
16
|
ProductDTO, |
|
17
|
RegionDTO, |
|
18
|
ShippingOptionDTO, |
|
19
|
StockLocationDTO, |
|
20
|
} from "@medusajs/types" |
|
21
|
import { |
|
22
|
ContainerRegistrationKeys, |
|
23
|
Modules, |
|
24
|
RuleOperator, |
|
25
|
remoteQueryObjectFromString, |
|
26
|
} from "@medusajs/utils" |
|
27
|
|
|
28
|
jest.setTimeout(500000) |
|
29
|
|
|
30
|
const env = {} |
|
31
|
const providerId = "manual_test-provider" |
|
32
|
let inventoryItem |
|
33
|
|
|
34
|
async function prepareDataFixtures({ container }) { |
|
35
|
const fulfillmentService = container.resolve(Modules.FULFILLMENT) |
|
36
|
const salesChannelService = container.resolve(Modules.SALES_CHANNEL) |
|
37
|
const stockLocationModule: IStockLocationService = container.resolve( |
|
38
|
Modules.STOCK_LOCATION |
|
39
|
) |
|
40
|
const productModule = container.resolve(Modules.PRODUCT) |
|
41
|
const inventoryModule = container.resolve(Modules.INVENTORY) |
|
42
|
|
|
43
|
const shippingProfile = await fulfillmentService.createShippingProfiles({ |
|
44
|
name: "test", |
|
45
|
type: "default", |
|
46
|
}) |
|
47
|
|
|
48
|
const fulfillmentSet = await fulfillmentService.createFulfillmentSets({ |
|
49
|
name: "Test fulfillment set", |
|
50
|
type: "manual_test", |
|
51
|
}) |
|
52
|
|
|
53
|
const serviceZone = await fulfillmentService.createServiceZones({ |
|
54
|
name: "Test service zone", |
|
55
|
fulfillment_set_id: fulfillmentSet.id, |
|
56
|
geo_zones: [ |
|
57
|
{ |
|
58
|
type: "country", |
|
59
|
country_code: "US", |
|
60
|
}, |
|
61
|
], |
|
62
|
}) |
|
63
|
|
|
64
|
const regionService = container.resolve( |
|
65
|
Modules.REGION |
|
66
|
) as IRegionModuleService |
|
67
|
|
|
68
|
const [region] = await regionService.createRegions([ |
|
69
|
{ |
|
70
|
name: "Test region", |
|
71
|
currency_code: "eur", |
|
72
|
countries: ["fr"], |
|
73
|
}, |
|
74
|
]) |
|
75
|
|
|
76
|
const salesChannel = await salesChannelService.createSalesChannels({ |
|
77
|
name: "Webshop", |
|
78
|
}) |
|
79
|
|
|
80
|
const location: StockLocationDTO = |
|
81
|
await stockLocationModule.createStockLocations({ |
|
82
|
name: "Warehouse", |
|
83
|
address: { |
|
84
|
address_1: "Test", |
|
85
|
city: "Test", |
|
86
|
country_code: "US", |
|
87
|
postal_code: "12345", |
|
88
|
phone: "12345", |
|
89
|
}, |
|
90
|
}) |
|
91
|
|
|
92
|
const [product] = await productModule.createProducts([ |
|
93
|
{ |
|
94
|
title: "Test product", |
|
95
|
variants: [ |
|
96
|
{ |
|
97
|
title: "Test variant", |
|
98
|
sku: "test-variant", |
|
99
|
}, |
|
100
|
], |
|
101
|
}, |
|
102
|
]) |
|
103
|
|
|
104
|
inventoryItem = await inventoryModule.createInventoryItems({ |
|
105
|
sku: "inv-1234", |
|
106
|
}) |
|
107
|
|
|
108
|
await inventoryModule.createInventoryLevels([ |
|
109
|
{ |
|
110
|
inventory_item_id: inventoryItem.id, |
|
111
|
location_id: location.id, |
|
112
|
stocked_quantity: 2, |
|
113
|
reserved_quantity: 0, |
|
114
|
}, |
|
115
|
]) |
|
116
|
|
|
117
|
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK) |
|
118
|
|
|
119
|
await remoteLink.create([ |
|
120
|
{ |
|
121
|
[Modules.STOCK_LOCATION]: { |
|
122
|
stock_location_id: location.id, |
|
123
|
}, |
|
124
|
[Modules.FULFILLMENT]: { |
|
125
|
fulfillment_provider_id: "manual_test-provider", |
|
126
|
}, |
|
127
|
}, |
|
128
|
]) |
|
129
|
|
|
130
|
await remoteLink.create([ |
|
131
|
{ |
|
132
|
[Modules.PRODUCT]: { |
|
133
|
product_id: product.id, |
|
134
|
}, |
|
135
|
[Modules.FULFILLMENT]: { |
|
136
|
shipping_profile_id: shippingProfile.id, |
|
137
|
}, |
|
138
|
}, |
|
139
|
]) |
|
140
|
|
|
141
|
await remoteLink.create([ |
|
142
|
{ |
|
143
|
[Modules.STOCK_LOCATION]: { |
|
144
|
stock_location_id: location.id, |
|
145
|
}, |
|
146
|
[Modules.FULFILLMENT]: { |
|
147
|
fulfillment_set_id: fulfillmentSet.id, |
|
148
|
}, |
|
149
|
}, |
|
150
|
{ |
|
151
|
[Modules.SALES_CHANNEL]: { |
|
152
|
sales_channel_id: salesChannel.id, |
|
153
|
}, |
|
154
|
[Modules.STOCK_LOCATION]: { |
|
155
|
stock_location_id: location.id, |
|
156
|
}, |
|
157
|
}, |
|
158
|
{ |
|
159
|
[Modules.PRODUCT]: { |
|
160
|
variant_id: product.variants[0].id, |
|
161
|
}, |
|
162
|
[Modules.INVENTORY]: { |
|
163
|
inventory_item_id: inventoryItem.id, |
|
164
|
}, |
|
165
|
}, |
|
166
|
]) |
|
167
|
|
|
168
|
const shippingOptionData: FulfillmentWorkflow.CreateShippingOptionsWorkflowInput = |
|
169
|
{ |
|
170
|
name: "Return shipping option", |
|
171
|
price_type: "flat", |
|
172
|
service_zone_id: serviceZone.id, |
|
173
|
shipping_profile_id: shippingProfile.id, |
|
174
|
provider_id: providerId, |
|
175
|
type: { |
|
176
|
code: "manual-type", |
|
177
|
label: "Manual Type", |
|
178
|
description: "Manual Type Description", |
|
179
|
}, |
|
180
|
prices: [ |
|
181
|
{ |
|
182
|
currency_code: "usd", |
|
183
|
amount: 10, |
|
184
|
}, |
|
185
|
{ |
|
186
|
region_id: region.id, |
|
187
|
amount: 100, |
|
188
|
}, |
|
189
|
], |
|
190
|
rules: [ |
|
191
|
{ |
|
192
|
attribute: "is_return", |
|
193
|
operator: RuleOperator.EQ, |
|
194
|
value: "true", |
|
195
|
}, |
|
196
|
], |
|
197
|
} |
|
198
|
|
|
199
|
const { result } = await createShippingOptionsWorkflow(container).run({ |
|
200
|
input: [shippingOptionData], |
|
201
|
}) |
|
202
|
|
|
203
|
const remoteQueryObject = remoteQueryObjectFromString({ |
|
204
|
entryPoint: "shipping_option", |
|
205
|
variables: { |
|
206
|
id: result[0].id, |
|
207
|
}, |
|
208
|
fields: [ |
|
209
|
"id", |
|
210
|
"name", |
|
211
|
"price_type", |
|
212
|
"service_zone_id", |
|
213
|
"shipping_profile_id", |
|
214
|
"provider_id", |
|
215
|
"data", |
|
216
|
"metadata", |
|
217
|
"type.*", |
|
218
|
"created_at", |
|
219
|
"updated_at", |
|
220
|
"deleted_at", |
|
221
|
"shipping_option_type_id", |
|
222
|
"prices.*", |
|
223
|
], |
|
224
|
}) |
|
225
|
|
|
226
|
const remoteQuery = container.resolve(ContainerRegistrationKeys.REMOTE_QUERY) |
|
227
|
|
|
228
|
const [createdShippingOption] = await remoteQuery(remoteQueryObject) |
|
229
|
return { |
|
230
|
shippingOption: createdShippingOption, |
|
231
|
region, |
|
232
|
salesChannel, |
|
233
|
location, |
|
234
|
product, |
|
235
|
} |
|
236
|
} |
|
237
|
|
|
238
|
async function createOrderFixture({ container, product, location }) { |
|
239
|
const orderService: IOrderModuleService = container.resolve(Modules.ORDER) |
|
240
|
let order = await orderService.createOrders({ |
|
241
|
region_id: "test_region_id", |
|
242
|
email: "foo@bar.com", |
|
243
|
items: [ |
|
244
|
{ |
|
245
|
title: "Custom Item 2", |
|
246
|
variant_sku: product.variants[0].sku, |
|
247
|
variant_title: product.variants[0].title, |
|
248
|
quantity: 1, |
|
249
|
unit_price: 50, |
|
250
|
adjustments: [ |
|
251
|
{ |
|
252
|
code: "VIP_25 ETH", |
|
253
|
amount: "0.000000000000000005", |
|
254
|
description: "VIP discount", |
|
255
|
promotion_id: "prom_123", |
|
256
|
provider_id: "coupon_kings", |
|
257
|
}, |
|
258
|
], |
|
259
|
} as any, |
|
260
|
], |
|
261
|
transactions: [ |
|
262
|
{ |
|
263
|
amount: 50, |
|
264
|
currency_code: "usd", |
|
265
|
}, |
|
266
|
], |
|
267
|
sales_channel_id: "test", |
|
268
|
shipping_address: { |
|
269
|
first_name: "Test", |
|
270
|
last_name: "Test", |
|
271
|
address_1: "Test", |
|
272
|
city: "Test", |
|
273
|
country_code: "US", |
|
274
|
postal_code: "12345", |
|
275
|
phone: "12345", |
|
276
|
}, |
|
277
|
billing_address: { |
|
278
|
first_name: "Test", |
|
279
|
last_name: "Test", |
|
280
|
address_1: "Test", |
|
281
|
city: "Test", |
|
282
|
country_code: "US", |
|
283
|
postal_code: "12345", |
|
284
|
}, |
|
285
|
shipping_methods: [ |
|
286
|
{ |
|
287
|
name: "Test shipping method", |
|
288
|
amount: 10, |
|
289
|
data: {}, |
|
290
|
tax_lines: [ |
|
291
|
{ |
|
292
|
description: "shipping Tax 1", |
|
293
|
tax_rate_id: "tax_usa_shipping", |
|
294
|
code: "code", |
|
295
|
rate: 10, |
|
296
|
}, |
|
297
|
], |
|
298
|
adjustments: [ |
|
299
|
{ |
|
300
|
code: "VIP_10", |
|
301
|
amount: 1, |
|
302
|
description: "VIP discount", |
|
303
|
promotion_id: "prom_123", |
|
304
|
}, |
|
305
|
], |
|
306
|
}, |
|
307
|
], |
|
308
|
currency_code: "usd", |
|
309
|
customer_id: "joe", |
|
310
|
}) |
|
311
|
|
|
312
|
const inventoryModule = container.resolve(Modules.INVENTORY) |
|
313
|
const reservation = await inventoryModule.createReservationItems([ |
|
314
|
{ |
|
315
|
line_item_id: order.items![0].id, |
|
316
|
inventory_item_id: inventoryItem.id, |
|
317
|
location_id: location.id, |
|
318
|
quantity: order.items![0].quantity, |
|
319
|
}, |
|
320
|
]) |
|
321
|
|
|
322
|
order = await orderService.retrieveOrder(order.id, { |
|
323
|
relations: ["items"], |
|
324
|
}) |
|
325
|
|
|
326
|
return order |
|
327
|
} |
|
328
|
|
|
329
|
medusaIntegrationTestRunner({ |
|
330
|
env, |
|
331
|
testSuite: ({ getContainer }) => { |
|
332
|
let container |
|
333
|
|
|
334
|
beforeAll(() => { |
|
335
|
container = getContainer() |
|
336
|
}) |
|
337
|
|
|
338
|
describe("Create order shipment workflow", () => { |
|
339
|
let shippingOption: ShippingOptionDTO |
|
340
|
let region: RegionDTO |
|
341
|
let location: StockLocationDTO |
|
342
|
let product: ProductDTO |
|
343
|
|
|
344
|
let orderService: IOrderModuleService |
|
345
|
|
|
346
|
beforeEach(async () => { |
|
347
|
const fixtures = await prepareDataFixtures({ |
|
348
|
container, |
|
349
|
}) |
|
350
|
|
|
351
|
shippingOption = fixtures.shippingOption |
|
352
|
region = fixtures.region |
|
353
|
location = fixtures.location |
|
354
|
product = fixtures.product |
|
355
|
|
|
356
|
orderService = container.resolve(Modules.ORDER) |
|
357
|
}) |
|
358
|
|
|
359
|
it("should create an order shipment", async () => { |
|
360
|
const order = await createOrderFixture({ container, product, location }) |
|
361
|
const createReturnOrderData: OrderWorkflow.CreateOrderFulfillmentWorkflowInput = |
|
362
|
{ |
|
363
|
order_id: order.id, |
|
364
|
created_by: "user_1", |
|
365
|
items: [ |
|
366
|
{ |
|
367
|
id: order.items![0].id, |
|
368
|
quantity: 1, |
|
369
|
}, |
|
370
|
], |
|
371
|
no_notification: false, |
|
372
|
location_id: undefined, |
|
373
|
} |
|
374
|
|
|
375
|
const { result: fulfillment } = await createOrderFulfillmentWorkflow( |
|
376
|
container |
|
377
|
).run({ |
|
378
|
input: createReturnOrderData, |
|
379
|
}) |
|
380
|
|
|
381
|
const createShipmentData: OrderWorkflow.CreateOrderShipmentWorkflowInput = |
|
382
|
{ |
|
383
|
order_id: order.id, |
|
384
|
fulfillment_id: fulfillment.id, |
|
385
|
created_by: "user_1", |
|
386
|
items: [ |
|
387
|
{ |
|
388
|
id: order.items![0].id, |
|
389
|
quantity: 1, |
|
390
|
}, |
|
391
|
], |
|
392
|
labels: [ |
|
393
|
{ |
|
394
|
tracking_number: "123456", |
|
395
|
tracking_url: "abcdef-xpress.com/track/123456", |
|
396
|
label_url: "http://abcdef-xpress.com/label/123456", |
|
397
|
}, |
|
398
|
], |
|
399
|
} |
|
400
|
|
|
401
|
await createOrderShipmentWorkflow(container).run({ |
|
402
|
input: createShipmentData, |
|
403
|
}) |
|
404
|
|
|
405
|
const remoteQuery = container.resolve( |
|
406
|
ContainerRegistrationKeys.REMOTE_QUERY |
|
407
|
) |
|
408
|
const remoteQueryObject = remoteQueryObjectFromString({ |
|
409
|
entryPoint: "order", |
|
410
|
variables: { |
|
411
|
id: order.id, |
|
412
|
}, |
|
413
|
fields: [ |
|
414
|
"*", |
|
415
|
"items.*", |
|
416
|
"shipping_methods.*", |
|
417
|
"total", |
|
418
|
"item_total", |
|
419
|
"fulfillments.*", |
|
420
|
], |
|
421
|
}) |
|
422
|
|
|
423
|
const [orderFulfill] = await remoteQuery(remoteQueryObject) |
|
424
|
|
|
425
|
expect(orderFulfill.fulfillments).toHaveLength(1) |
|
426
|
expect(normalizeBigNumbers(orderFulfill.items[0].detail.fulfilled_quantity)).toEqual( |
|
427
|
1 |
|
428
|
) |
|
429
|
expect(orderFulfill.fulfillments[0].marked_shipped_by).toEqual("user_1") |
|
430
|
|
|
431
|
const inventoryModule = container.resolve(Modules.INVENTORY) |
|
432
|
const reservation = await inventoryModule.listReservationItems({ |
|
433
|
line_item_id: order.items![0].id, |
|
434
|
}) |
|
435
|
expect(reservation).toHaveLength(0) |
|
436
|
|
|
437
|
const stockAvailability = await inventoryModule.retrieveStockedQuantity( |
|
438
|
inventoryItem.id, |
|
439
|
[location.id] |
|
440
|
) |
|
441
|
expect(stockAvailability.valueOf()).toEqual(1) |
|
442
|
}) |
|
443
|
}) |
|
444
|
}, |
|
445
|
}) |