|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { IFulfillmentModuleService, StockLocationDTO } from "@medusajs/types" |
|
3
|
import { Modules } from "@medusajs/utils" |
|
4
|
import { createAdminUser } from "../../../helpers/create-admin-user" |
|
5
|
import { |
|
6
|
generateCreateFulfillmentData, |
|
7
|
generateCreateShippingOptionsData, |
|
8
|
setupFullDataFulfillmentStructure, |
|
9
|
} from "../fixtures" |
|
10
|
|
|
11
|
jest.setTimeout(100000) |
|
12
|
|
|
13
|
const env = {} |
|
14
|
const adminHeaders = { |
|
15
|
headers: { "x-medusa-access-token": "test_token" }, |
|
16
|
} |
|
17
|
const providerId = "manual_test-provider" |
|
18
|
|
|
19
|
medusaIntegrationTestRunner({ |
|
20
|
env, |
|
21
|
testSuite: ({ getContainer, api, dbConnection, dbUtils }) => { |
|
22
|
let service: IFulfillmentModuleService |
|
23
|
let container |
|
24
|
let location: StockLocationDTO |
|
25
|
|
|
26
|
beforeAll(() => { |
|
27
|
container = getContainer() |
|
28
|
service = container.resolve(Modules.FULFILLMENT) |
|
29
|
}) |
|
30
|
|
|
31
|
beforeAll(async () => { |
|
32
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
33
|
const stockLocationService = container.resolve(Modules.STOCK_LOCATION) |
|
34
|
|
|
35
|
location = await stockLocationService.createStockLocations({ |
|
36
|
name: "Test Location", |
|
37
|
address: { |
|
38
|
address_1: "Test Address", |
|
39
|
address_2: "tttest", |
|
40
|
city: "Test City", |
|
41
|
country_code: "us", |
|
42
|
postal_code: "12345", |
|
43
|
metadata: { email: "test@mail.com" }, |
|
44
|
}, |
|
45
|
metadata: { custom_location: "yes" }, |
|
46
|
}) |
|
47
|
|
|
48
|
await dbUtils.snapshot() |
|
49
|
}) |
|
50
|
|
|
51
|
/** |
|
52
|
* The test runner run both the medusa migrations as well as the modules |
|
53
|
* migrations. In order to ensure the backward compatibility |
|
54
|
* of the migration works, we will create a full data structure. |
|
55
|
*/ |
|
56
|
describe("Fulfillment module migrations backward compatibility", () => { |
|
57
|
it("should allow to create a full data structure after the backward compatible migration have run on top of the medusa v1 database", async () => { |
|
58
|
await setupFullDataFulfillmentStructure(service, { |
|
59
|
providerId, |
|
60
|
locationId: location.id, |
|
61
|
}) |
|
62
|
|
|
63
|
const fulfillmentSets = await service.listFulfillmentSets( |
|
64
|
{}, |
|
65
|
{ |
|
66
|
relations: [ |
|
67
|
"service_zones.geo_zones", |
|
68
|
"service_zones.shipping_options.shipping_profile", |
|
69
|
"service_zones.shipping_options.provider", |
|
70
|
"service_zones.shipping_options.type", |
|
71
|
"service_zones.shipping_options.rules", |
|
72
|
"service_zones.shipping_options.fulfillments.labels", |
|
73
|
"service_zones.shipping_options.fulfillments.items", |
|
74
|
"service_zones.shipping_options.fulfillments.delivery_address", |
|
75
|
], |
|
76
|
} |
|
77
|
) |
|
78
|
|
|
79
|
expect(fulfillmentSets).toHaveLength(1) |
|
80
|
|
|
81
|
let fulfillmentSet = fulfillmentSets[0] |
|
82
|
expect(fulfillmentSet.service_zones).toHaveLength(1) |
|
83
|
|
|
84
|
let serviceZone = fulfillmentSet.service_zones[0] |
|
85
|
expect(serviceZone.geo_zones).toHaveLength(1) |
|
86
|
expect(serviceZone.shipping_options).toHaveLength(1) |
|
87
|
|
|
88
|
let geoZone = serviceZone.geo_zones[0] |
|
89
|
|
|
90
|
let shippingOption = serviceZone.shipping_options[0] |
|
91
|
expect(!!shippingOption.shipping_profile.deleted_at).toEqual(false) |
|
92
|
expect(shippingOption.fulfillments).toHaveLength(1) |
|
93
|
expect(shippingOption.rules).toHaveLength(1) |
|
94
|
|
|
95
|
let fulfillment = shippingOption.fulfillments[0] |
|
96
|
expect(fulfillment.labels).toHaveLength(1) |
|
97
|
expect(fulfillment.items).toHaveLength(1) |
|
98
|
}) |
|
99
|
}) |
|
100
|
|
|
101
|
describe("POST /admin/fulfillments/:id/cancel", () => { |
|
102
|
it("should throw an error when id is not found", async () => { |
|
103
|
const error = await api |
|
104
|
.post(`/admin/fulfillments/does-not-exist/cancel`, {}, adminHeaders) |
|
105
|
.catch((e) => e) |
|
106
|
|
|
107
|
expect(error.response.status).toEqual(404) |
|
108
|
expect(error.response.data).toEqual({ |
|
109
|
type: "not_found", |
|
110
|
message: "Fulfillment with id: does-not-exist was not found", |
|
111
|
}) |
|
112
|
}) |
|
113
|
|
|
114
|
it("should cancel a fulfillment", async () => { |
|
115
|
await setupFullDataFulfillmentStructure(service, { |
|
116
|
providerId, |
|
117
|
locationId: location.id, |
|
118
|
}) |
|
119
|
|
|
120
|
const [fulfillment] = await service.listFulfillments() |
|
121
|
|
|
122
|
const response = await api.post( |
|
123
|
`/admin/fulfillments/${fulfillment.id}/cancel`, |
|
124
|
{}, |
|
125
|
adminHeaders |
|
126
|
) |
|
127
|
|
|
128
|
expect(response.status).toEqual(200) |
|
129
|
|
|
130
|
const canceledFulfillment = await service.retrieveFulfillment( |
|
131
|
fulfillment.id |
|
132
|
) |
|
133
|
|
|
134
|
expect(canceledFulfillment.canceled_at).toBeTruthy() |
|
135
|
}) |
|
136
|
}) |
|
137
|
|
|
138
|
describe("POST /admin/fulfillments", () => { |
|
139
|
it("should create a fulfillment", async () => { |
|
140
|
const shippingProfile = await service.createShippingProfiles({ |
|
141
|
name: "test", |
|
142
|
type: "default", |
|
143
|
}) |
|
144
|
|
|
145
|
const fulfillmentSet = await service.createFulfillmentSets({ |
|
146
|
name: "test", |
|
147
|
type: "test-type", |
|
148
|
}) |
|
149
|
|
|
150
|
const serviceZone = await service.createServiceZones({ |
|
151
|
name: "test", |
|
152
|
fulfillment_set_id: fulfillmentSet.id, |
|
153
|
}) |
|
154
|
|
|
155
|
const shippingOption = await service.createShippingOptions( |
|
156
|
generateCreateShippingOptionsData({ |
|
157
|
provider_id: providerId, |
|
158
|
service_zone_id: serviceZone.id, |
|
159
|
shipping_profile_id: shippingProfile.id, |
|
160
|
}) |
|
161
|
) |
|
162
|
|
|
163
|
const data = generateCreateFulfillmentData({ |
|
164
|
location_id: location.id, |
|
165
|
provider_id: providerId, |
|
166
|
shipping_option_id: shippingOption.id, |
|
167
|
order_id: "order_123", |
|
168
|
}) |
|
169
|
|
|
170
|
const response = await api |
|
171
|
.post(`/admin/fulfillments`, data, adminHeaders) |
|
172
|
.catch((e) => e) |
|
173
|
|
|
174
|
expect(response.status).toEqual(200) |
|
175
|
expect(response.data.fulfillment).toEqual( |
|
176
|
expect.objectContaining({ |
|
177
|
id: expect.any(String), |
|
178
|
location_id: location.id, |
|
179
|
packed_at: null, |
|
180
|
shipped_at: null, |
|
181
|
delivered_at: null, |
|
182
|
canceled_at: null, |
|
183
|
created_by: expect.any(String), |
|
184
|
provider_id: "manual_test-provider", |
|
185
|
delivery_address: expect.objectContaining({ |
|
186
|
address_1: expect.any(String), |
|
187
|
address_2: expect.any(String), |
|
188
|
city: expect.any(String), |
|
189
|
country_code: expect.any(String), |
|
190
|
province: expect.any(String), |
|
191
|
postal_code: expect.any(String), |
|
192
|
}), |
|
193
|
items: [ |
|
194
|
expect.objectContaining({ |
|
195
|
id: expect.any(String), |
|
196
|
title: expect.any(String), |
|
197
|
sku: expect.any(String), |
|
198
|
barcode: expect.any(String), |
|
199
|
raw_quantity: { |
|
200
|
value: "1", |
|
201
|
precision: 20, |
|
202
|
}, |
|
203
|
quantity: 1, |
|
204
|
}), |
|
205
|
], |
|
206
|
labels: [ |
|
207
|
expect.objectContaining({ |
|
208
|
id: expect.any(String), |
|
209
|
tracking_number: expect.any(String), |
|
210
|
tracking_url: expect.any(String), |
|
211
|
label_url: expect.any(String), |
|
212
|
}), |
|
213
|
], |
|
214
|
}) |
|
215
|
) |
|
216
|
}) |
|
217
|
}) |
|
218
|
|
|
219
|
describe("POST /admin/fulfillments/:id/shipment", () => { |
|
220
|
it("should throw an error when id is not found", async () => { |
|
221
|
const error = await api |
|
222
|
.post( |
|
223
|
`/admin/fulfillments/does-not-exist/shipment`, |
|
224
|
{ |
|
225
|
labels: [ |
|
226
|
{ |
|
227
|
tracking_number: "test-tracking-number", |
|
228
|
tracking_url: "https://example.com/tracking", |
|
229
|
label_url: "https://example.com/label", |
|
230
|
}, |
|
231
|
], |
|
232
|
}, |
|
233
|
adminHeaders |
|
234
|
) |
|
235
|
.catch((e) => e) |
|
236
|
|
|
237
|
expect(error.response.status).toEqual(404) |
|
238
|
expect(error.response.data).toEqual({ |
|
239
|
type: "not_found", |
|
240
|
message: "Fulfillment with id: does-not-exist was not found", |
|
241
|
}) |
|
242
|
}) |
|
243
|
|
|
244
|
it("should update a fulfillment to be shipped", async () => { |
|
245
|
await setupFullDataFulfillmentStructure(service, { |
|
246
|
providerId, |
|
247
|
locationId: location.id, |
|
248
|
}) |
|
249
|
|
|
250
|
const [fulfillment] = await service.listFulfillments() |
|
251
|
|
|
252
|
const response = await api.post( |
|
253
|
`/admin/fulfillments/${fulfillment.id}/shipment`, |
|
254
|
{ |
|
255
|
labels: [ |
|
256
|
{ |
|
257
|
tracking_number: "test-tracking-number", |
|
258
|
tracking_url: "https://example.com/tracking", |
|
259
|
label_url: "https://example.com/label", |
|
260
|
}, |
|
261
|
], |
|
262
|
}, |
|
263
|
adminHeaders |
|
264
|
) |
|
265
|
|
|
266
|
expect(response.status).toEqual(200) |
|
267
|
expect(response.data.fulfillment).toEqual( |
|
268
|
expect.objectContaining({ |
|
269
|
id: fulfillment.id, |
|
270
|
shipped_at: expect.any(String), |
|
271
|
marked_shipped_by: expect.any(String), |
|
272
|
labels: [ |
|
273
|
expect.objectContaining({ |
|
274
|
id: expect.any(String), |
|
275
|
tracking_number: "test-tracking-number", |
|
276
|
tracking_url: "https://example.com/tracking", |
|
277
|
label_url: "https://example.com/label", |
|
278
|
}), |
|
279
|
], |
|
280
|
}) |
|
281
|
) |
|
282
|
}) |
|
283
|
|
|
284
|
it("should throw error when already shipped", async () => { |
|
285
|
await setupFullDataFulfillmentStructure(service, { |
|
286
|
providerId, |
|
287
|
locationId: location.id, |
|
288
|
}) |
|
289
|
|
|
290
|
const [fulfillment] = await service.listFulfillments() |
|
291
|
|
|
292
|
await service.updateFulfillment(fulfillment.id, { |
|
293
|
shipped_at: new Date(), |
|
294
|
}) |
|
295
|
|
|
296
|
const error = await api |
|
297
|
.post( |
|
298
|
`/admin/fulfillments/${fulfillment.id}/shipment`, |
|
299
|
{ |
|
300
|
labels: [ |
|
301
|
{ |
|
302
|
tracking_number: "test-tracking-number", |
|
303
|
tracking_url: "https://example.com/tracking", |
|
304
|
label_url: "https://example.com/label", |
|
305
|
}, |
|
306
|
], |
|
307
|
}, |
|
308
|
adminHeaders |
|
309
|
) |
|
310
|
.catch((e) => e) |
|
311
|
|
|
312
|
expect(error.response.status).toEqual(400) |
|
313
|
expect(error.response.data).toEqual({ |
|
314
|
type: "not_allowed", |
|
315
|
message: "Shipment has already been created", |
|
316
|
}) |
|
317
|
}) |
|
318
|
}) |
|
319
|
}, |
|
320
|
}) |