|
1
|
import { |
|
2
|
createFulfillmentWorkflow, |
|
3
|
createFulfillmentWorkflowId, |
|
4
|
createShipmentWorkflow, |
|
5
|
createShipmentWorkflowId, |
|
6
|
updateFulfillmentWorkflow, |
|
7
|
updateFulfillmentWorkflowId, |
|
8
|
} from "@medusajs/core-flows" |
|
9
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
10
|
import { |
|
11
|
IFulfillmentModuleService, |
|
12
|
MedusaContainer, |
|
13
|
StockLocationDTO, |
|
14
|
} from "@medusajs/types" |
|
15
|
import { ContainerRegistrationKeys, Modules } from "@medusajs/utils" |
|
16
|
import { |
|
17
|
generateCreateFulfillmentData, |
|
18
|
generateCreateShippingOptionsData, |
|
19
|
} from "../fixtures" |
|
20
|
|
|
21
|
jest.setTimeout(50000) |
|
22
|
|
|
23
|
const providerId = "manual_test-provider" |
|
24
|
|
|
25
|
medusaIntegrationTestRunner({ |
|
26
|
env: {}, |
|
27
|
testSuite: ({ getContainer }) => { |
|
28
|
describe("Workflows: Fulfillment", () => { |
|
29
|
let location: StockLocationDTO |
|
30
|
let appContainer: MedusaContainer |
|
31
|
let service: IFulfillmentModuleService |
|
32
|
|
|
33
|
beforeAll(async () => { |
|
34
|
appContainer = getContainer() |
|
35
|
service = appContainer.resolve(Modules.FULFILLMENT) |
|
36
|
}) |
|
37
|
|
|
38
|
beforeEach(async () => { |
|
39
|
const stockLocationService = appContainer.resolve( |
|
40
|
Modules.STOCK_LOCATION |
|
41
|
) |
|
42
|
|
|
43
|
location = await stockLocationService.createStockLocations({ |
|
44
|
name: "Test Location", |
|
45
|
address: { |
|
46
|
address_1: "Test Address", |
|
47
|
address_2: "tttest", |
|
48
|
city: "Test City", |
|
49
|
country_code: "us", |
|
50
|
postal_code: "12345", |
|
51
|
metadata: { email: "test@mail.com" }, |
|
52
|
}, |
|
53
|
metadata: { custom_location: "yes" }, |
|
54
|
}) |
|
55
|
}) |
|
56
|
|
|
57
|
describe("createFulfillmentWorkflow", () => { |
|
58
|
describe("invoke", () => { |
|
59
|
it("should get stock location", async () => { |
|
60
|
const workflow = createFulfillmentWorkflow(appContainer) |
|
61
|
|
|
62
|
const link = appContainer.resolve( |
|
63
|
ContainerRegistrationKeys.REMOTE_LINK |
|
64
|
) |
|
65
|
|
|
66
|
const shippingProfile = await service.createShippingProfiles({ |
|
67
|
name: "test", |
|
68
|
type: "default", |
|
69
|
}) |
|
70
|
|
|
71
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
72
|
name: "test", |
|
73
|
type: "test-type", |
|
74
|
}) |
|
75
|
|
|
76
|
await link.create({ |
|
77
|
[Modules.STOCK_LOCATION]: { |
|
78
|
stock_location_id: location.id, |
|
79
|
}, |
|
80
|
[Modules.FULFILLMENT]: { |
|
81
|
fulfillment_set_id: fulfillmentSet.id, |
|
82
|
}, |
|
83
|
}) |
|
84
|
|
|
85
|
const serviceZone = await service.createServiceZones({ |
|
86
|
name: "test", |
|
87
|
fulfillment_set_id: fulfillmentSet.id, |
|
88
|
}) |
|
89
|
|
|
90
|
const shippingOption = await service.createShippingOptions( |
|
91
|
generateCreateShippingOptionsData({ |
|
92
|
provider_id: providerId, |
|
93
|
service_zone_id: serviceZone.id, |
|
94
|
shipping_profile_id: shippingProfile.id, |
|
95
|
}) |
|
96
|
) |
|
97
|
|
|
98
|
const data = generateCreateFulfillmentData({ |
|
99
|
provider_id: providerId, |
|
100
|
shipping_option_id: shippingOption.id, |
|
101
|
order_id: "fake-order", |
|
102
|
location_id: location.id, |
|
103
|
}) |
|
104
|
|
|
105
|
const { transaction } = await workflow.run({ |
|
106
|
input: data, |
|
107
|
throwOnError: true, |
|
108
|
}) |
|
109
|
|
|
110
|
expect( |
|
111
|
transaction.context.invoke["get-location"].output.output |
|
112
|
).toEqual({ |
|
113
|
id: expect.any(String), |
|
114
|
created_at: expect.any(Date), |
|
115
|
updated_at: expect.any(Date), |
|
116
|
name: "Test Location", |
|
117
|
address: { |
|
118
|
id: expect.any(String), |
|
119
|
address_1: "Test Address", |
|
120
|
address_2: "tttest", |
|
121
|
city: "Test City", |
|
122
|
country_code: "us", |
|
123
|
postal_code: "12345", |
|
124
|
metadata: { email: "test@mail.com" }, |
|
125
|
phone: null, |
|
126
|
province: null, |
|
127
|
}, |
|
128
|
metadata: { custom_location: "yes" }, |
|
129
|
}) |
|
130
|
}) |
|
131
|
}) |
|
132
|
|
|
133
|
describe("compensation", () => { |
|
134
|
it("should cancel created fulfillment if step following step throws error", async () => { |
|
135
|
const workflow = createFulfillmentWorkflow(appContainer) |
|
136
|
|
|
137
|
workflow.appendAction("throw", createFulfillmentWorkflowId, { |
|
138
|
invoke: async function failStep() { |
|
139
|
throw new Error( |
|
140
|
`Failed to do something after creating fulfillment` |
|
141
|
) |
|
142
|
}, |
|
143
|
}) |
|
144
|
|
|
145
|
const shippingProfile = await service.createShippingProfiles({ |
|
146
|
name: "test", |
|
147
|
type: "default", |
|
148
|
}) |
|
149
|
|
|
150
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
151
|
name: "test", |
|
152
|
type: "test-type", |
|
153
|
}) |
|
154
|
|
|
155
|
const serviceZone = await service.createServiceZones({ |
|
156
|
name: "test", |
|
157
|
fulfillment_set_id: fulfillmentSet.id, |
|
158
|
}) |
|
159
|
|
|
160
|
const shippingOption = await service.createShippingOptions( |
|
161
|
generateCreateShippingOptionsData({ |
|
162
|
provider_id: providerId, |
|
163
|
service_zone_id: serviceZone.id, |
|
164
|
shipping_profile_id: shippingProfile.id, |
|
165
|
}) |
|
166
|
) |
|
167
|
|
|
168
|
const data = generateCreateFulfillmentData({ |
|
169
|
provider_id: providerId, |
|
170
|
shipping_option_id: shippingOption.id, |
|
171
|
order_id: "fake-order", |
|
172
|
location_id: location.id, |
|
173
|
}) |
|
174
|
const { errors } = await workflow.run({ |
|
175
|
input: data, |
|
176
|
throwOnError: false, |
|
177
|
}) |
|
178
|
|
|
179
|
expect(errors).toEqual([ |
|
180
|
{ |
|
181
|
action: "throw", |
|
182
|
handlerType: "invoke", |
|
183
|
error: expect.objectContaining({ |
|
184
|
message: `Failed to do something after creating fulfillment`, |
|
185
|
}), |
|
186
|
}, |
|
187
|
]) |
|
188
|
|
|
189
|
const fulfillments = await service.listFulfillments() |
|
190
|
|
|
191
|
expect(fulfillments.filter((f) => !!f.canceled_at)).toHaveLength(1) |
|
192
|
}) |
|
193
|
}) |
|
194
|
}) |
|
195
|
|
|
196
|
describe("updateFulfillmentWorkflow", () => { |
|
197
|
describe("compensation", () => { |
|
198
|
it("should rollback updated fulfillment if step following step throws error", async () => { |
|
199
|
const workflow = updateFulfillmentWorkflow(appContainer) |
|
200
|
|
|
201
|
workflow.appendAction("throw", updateFulfillmentWorkflowId, { |
|
202
|
invoke: async function failStep() { |
|
203
|
throw new Error( |
|
204
|
`Failed to do something after updating fulfillment` |
|
205
|
) |
|
206
|
}, |
|
207
|
}) |
|
208
|
|
|
209
|
const shippingProfile = await service.createShippingProfiles({ |
|
210
|
name: "test", |
|
211
|
type: "default", |
|
212
|
}) |
|
213
|
|
|
214
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
215
|
name: "test", |
|
216
|
type: "test-type", |
|
217
|
}) |
|
218
|
|
|
219
|
const serviceZone = await service.createServiceZones({ |
|
220
|
name: "test", |
|
221
|
fulfillment_set_id: fulfillmentSet.id, |
|
222
|
}) |
|
223
|
|
|
224
|
const shippingOption = await service.createShippingOptions( |
|
225
|
generateCreateShippingOptionsData({ |
|
226
|
provider_id: providerId, |
|
227
|
service_zone_id: serviceZone.id, |
|
228
|
shipping_profile_id: shippingProfile.id, |
|
229
|
}) |
|
230
|
) |
|
231
|
|
|
232
|
const data = generateCreateFulfillmentData({ |
|
233
|
order_id: "fake-order", |
|
234
|
provider_id: providerId, |
|
235
|
shipping_option_id: shippingOption.id, |
|
236
|
location_id: location.id, |
|
237
|
}) |
|
238
|
|
|
239
|
const fulfillment = await service.createFulfillment({ |
|
240
|
...data, |
|
241
|
location, |
|
242
|
}) |
|
243
|
|
|
244
|
const date = new Date() |
|
245
|
const { errors } = await workflow.run({ |
|
246
|
input: { |
|
247
|
id: fulfillment.id, |
|
248
|
shipped_at: date, |
|
249
|
packed_at: date, |
|
250
|
location_id: location.id, |
|
251
|
}, |
|
252
|
throwOnError: false, |
|
253
|
}) |
|
254
|
|
|
255
|
expect(errors).toEqual([ |
|
256
|
{ |
|
257
|
action: "throw", |
|
258
|
handlerType: "invoke", |
|
259
|
error: expect.objectContaining({ |
|
260
|
message: `Failed to do something after updating fulfillment`, |
|
261
|
}), |
|
262
|
}, |
|
263
|
]) |
|
264
|
|
|
265
|
const fulfillmentAfterRollback = await service.retrieveFulfillment( |
|
266
|
fulfillment.id |
|
267
|
) |
|
268
|
|
|
269
|
expect(fulfillmentAfterRollback).toEqual( |
|
270
|
expect.objectContaining({ |
|
271
|
location_id: data.location_id, |
|
272
|
shipped_at: data.shipped_at, |
|
273
|
packed_at: data.packed_at, |
|
274
|
}) |
|
275
|
) |
|
276
|
}) |
|
277
|
}) |
|
278
|
}) |
|
279
|
|
|
280
|
describe("createShipmentWorkflow", () => { |
|
281
|
describe("compensation", () => { |
|
282
|
it("should rollback shipment workflow if following step throws error", async () => { |
|
283
|
const workflow = createShipmentWorkflow(appContainer) |
|
284
|
|
|
285
|
workflow.appendAction("throw", createShipmentWorkflowId, { |
|
286
|
invoke: async function failStep() { |
|
287
|
throw new Error( |
|
288
|
`Failed to do something after creating shipment` |
|
289
|
) |
|
290
|
}, |
|
291
|
}) |
|
292
|
|
|
293
|
const shippingProfile = await service.createShippingProfiles({ |
|
294
|
name: "test", |
|
295
|
type: "default", |
|
296
|
}) |
|
297
|
|
|
298
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
299
|
name: "test", |
|
300
|
type: "test-type", |
|
301
|
}) |
|
302
|
|
|
303
|
const serviceZone = await service.createServiceZones({ |
|
304
|
name: "test", |
|
305
|
fulfillment_set_id: fulfillmentSet.id, |
|
306
|
}) |
|
307
|
|
|
308
|
const shippingOption = await service.createShippingOptions( |
|
309
|
generateCreateShippingOptionsData({ |
|
310
|
provider_id: providerId, |
|
311
|
service_zone_id: serviceZone.id, |
|
312
|
shipping_profile_id: shippingProfile.id, |
|
313
|
}) |
|
314
|
) |
|
315
|
|
|
316
|
const data = generateCreateFulfillmentData({ |
|
317
|
order_id: "fake-order", |
|
318
|
provider_id: providerId, |
|
319
|
shipping_option_id: shippingOption.id, |
|
320
|
location_id: location.id, |
|
321
|
}) |
|
322
|
|
|
323
|
const fulfillment = await service.createFulfillment({ |
|
324
|
...data, |
|
325
|
location, |
|
326
|
labels: [], |
|
327
|
}) |
|
328
|
|
|
329
|
const { errors } = await workflow.run({ |
|
330
|
input: { |
|
331
|
id: fulfillment.id, |
|
332
|
labels: [ |
|
333
|
{ |
|
334
|
tracking_number: "test-tracking-number", |
|
335
|
tracking_url: "test-tracking-url", |
|
336
|
label_url: "test-label-url", |
|
337
|
}, |
|
338
|
], |
|
339
|
}, |
|
340
|
throwOnError: false, |
|
341
|
}) |
|
342
|
|
|
343
|
expect(errors).toEqual([ |
|
344
|
{ |
|
345
|
action: "throw", |
|
346
|
handlerType: "invoke", |
|
347
|
error: expect.objectContaining({ |
|
348
|
message: `Failed to do something after creating shipment`, |
|
349
|
}), |
|
350
|
}, |
|
351
|
]) |
|
352
|
|
|
353
|
const fulfillmentAfterRollback = await service.retrieveFulfillment( |
|
354
|
fulfillment.id, |
|
355
|
{ select: ["shipped_at"], relations: ["labels"] } |
|
356
|
) |
|
357
|
|
|
358
|
expect(fulfillmentAfterRollback).toEqual( |
|
359
|
expect.objectContaining({ |
|
360
|
shipped_at: null, |
|
361
|
// TODO: the revert isn't handling deleting the labels. This needs to be handled uniformly across. |
|
362
|
// labels: [], |
|
363
|
}) |
|
364
|
) |
|
365
|
}) |
|
366
|
}) |
|
367
|
}) |
|
368
|
}) |
|
369
|
}, |
|
370
|
}) |