|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { Modules } from "@medusajs/utils" |
|
3
|
import { |
|
4
|
adminHeaders, |
|
5
|
createAdminUser, |
|
6
|
} from "../../../../helpers/create-admin-user" |
|
7
|
import { updateInventoryItemsWorkflow } from "@medusajs/core-flows" |
|
8
|
|
|
9
|
jest.setTimeout(30000) |
|
10
|
|
|
11
|
medusaIntegrationTestRunner({ |
|
12
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
13
|
let inventoryItem1 |
|
14
|
let inventoryItem2 |
|
15
|
let stockLocation1 |
|
16
|
let stockLocation2 |
|
17
|
let stockLocation3 |
|
18
|
|
|
19
|
let shippingProfile |
|
20
|
let container |
|
21
|
beforeAll(async () => { |
|
22
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
23
|
|
|
24
|
container = getContainer() |
|
25
|
|
|
26
|
shippingProfile = ( |
|
27
|
await api.post( |
|
28
|
`/admin/shipping-profiles`, |
|
29
|
{ name: "Test", type: "default" }, |
|
30
|
adminHeaders |
|
31
|
) |
|
32
|
).data.shipping_profile |
|
33
|
|
|
34
|
stockLocation1 = ( |
|
35
|
await api.post(`/admin/stock-locations`, { name: "loc1" }, adminHeaders) |
|
36
|
).data.stock_location |
|
37
|
|
|
38
|
stockLocation2 = ( |
|
39
|
await api.post(`/admin/stock-locations`, { name: "loc2" }, adminHeaders) |
|
40
|
).data.stock_location |
|
41
|
|
|
42
|
stockLocation3 = ( |
|
43
|
await api.post(`/admin/stock-locations`, { name: "loc3" }, adminHeaders) |
|
44
|
).data.stock_location |
|
45
|
|
|
46
|
inventoryItem1 = ( |
|
47
|
await api.post( |
|
48
|
`/admin/inventory-items`, |
|
49
|
{ |
|
50
|
sku: "12345", |
|
51
|
origin_country: "UK", |
|
52
|
hs_code: "hs001", |
|
53
|
mid_code: "mids", |
|
54
|
material: "material", |
|
55
|
weight: 300, |
|
56
|
length: 100, |
|
57
|
height: 200, |
|
58
|
width: 150, |
|
59
|
}, |
|
60
|
adminHeaders |
|
61
|
) |
|
62
|
).data.inventory_item |
|
63
|
|
|
64
|
inventoryItem2 = ( |
|
65
|
await api.post( |
|
66
|
`/admin/inventory-items`, |
|
67
|
{ sku: "second", origin_country: "UK", hs_code: "hs001" }, |
|
68
|
adminHeaders |
|
69
|
) |
|
70
|
).data.inventory_item |
|
71
|
|
|
72
|
await dbUtils.snapshot() |
|
73
|
}) |
|
74
|
|
|
75
|
describe("Inventory Items", () => { |
|
76
|
describe("GET /admin/inventory-items/:id/location-levels", () => { |
|
77
|
beforeEach(async () => { |
|
78
|
await api.post( |
|
79
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
80
|
{ |
|
81
|
location_id: stockLocation1.id, |
|
82
|
stocked_quantity: 10, |
|
83
|
}, |
|
84
|
adminHeaders |
|
85
|
) |
|
86
|
|
|
87
|
await api.post( |
|
88
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
89
|
{ |
|
90
|
location_id: stockLocation2.id, |
|
91
|
stocked_quantity: 15, |
|
92
|
}, |
|
93
|
adminHeaders |
|
94
|
) |
|
95
|
}) |
|
96
|
|
|
97
|
it("should list the inventory levels", async () => { |
|
98
|
const response = await api.get( |
|
99
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
100
|
adminHeaders |
|
101
|
) |
|
102
|
|
|
103
|
expect(response.data).toEqual( |
|
104
|
expect.objectContaining({ |
|
105
|
count: 2, |
|
106
|
offset: 0, |
|
107
|
limit: 50, |
|
108
|
}) |
|
109
|
) |
|
110
|
|
|
111
|
expect(response.data.inventory_levels).toHaveLength(2) |
|
112
|
expect(response.data.inventory_levels).toEqual( |
|
113
|
expect.arrayContaining([ |
|
114
|
expect.objectContaining({ |
|
115
|
location_id: stockLocation1.id, |
|
116
|
stocked_quantity: 10, |
|
117
|
}), |
|
118
|
expect.objectContaining({ |
|
119
|
location_id: stockLocation2.id, |
|
120
|
stocked_quantity: 15, |
|
121
|
}), |
|
122
|
]) |
|
123
|
) |
|
124
|
}) |
|
125
|
|
|
126
|
it("should list the location levels based on id param constraint", async () => { |
|
127
|
const inventoryItemId = inventoryItem1.id |
|
128
|
|
|
129
|
const result = await api.get( |
|
130
|
`/admin/inventory-items/${inventoryItemId}/location-levels?location_id[]=${stockLocation1.id}`, |
|
131
|
adminHeaders |
|
132
|
) |
|
133
|
|
|
134
|
expect(result.status).toEqual(200) |
|
135
|
expect(result.data.inventory_levels).toHaveLength(1) |
|
136
|
expect(result.data.inventory_levels).toEqual( |
|
137
|
expect.arrayContaining([ |
|
138
|
expect.objectContaining({ |
|
139
|
stocked_quantity: 10, |
|
140
|
}), |
|
141
|
]) |
|
142
|
) |
|
143
|
}) |
|
144
|
}) |
|
145
|
|
|
146
|
describe("POST /admin/inventory-items/location-levels/batch", () => { |
|
147
|
let locationLevel1 |
|
148
|
let locationLevel2 |
|
149
|
|
|
150
|
beforeEach(async () => { |
|
151
|
const seed = await api.post( |
|
152
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/batch`, |
|
153
|
{ |
|
154
|
create: [ |
|
155
|
{ |
|
156
|
location_id: stockLocation1.id, |
|
157
|
stocked_quantity: 0, |
|
158
|
}, |
|
159
|
{ |
|
160
|
location_id: stockLocation2.id, |
|
161
|
stocked_quantity: 10, |
|
162
|
}, |
|
163
|
], |
|
164
|
}, |
|
165
|
adminHeaders |
|
166
|
) |
|
167
|
|
|
168
|
locationLevel1 = seed.data.created[0] |
|
169
|
locationLevel2 = seed.data.created[1] |
|
170
|
}) |
|
171
|
|
|
172
|
it("should batch update the inventory levels", async () => { |
|
173
|
const result = await api.post( |
|
174
|
`/admin/inventory-items/location-levels/batch`, |
|
175
|
{ |
|
176
|
update: [ |
|
177
|
{ |
|
178
|
location_id: stockLocation1.id, |
|
179
|
inventory_item_id: inventoryItem1.id, |
|
180
|
stocked_quantity: 10, |
|
181
|
}, |
|
182
|
{ |
|
183
|
location_id: stockLocation2.id, |
|
184
|
inventory_item_id: inventoryItem1.id, |
|
185
|
stocked_quantity: 20, |
|
186
|
}, |
|
187
|
], |
|
188
|
}, |
|
189
|
adminHeaders |
|
190
|
) |
|
191
|
|
|
192
|
expect(result.status).toEqual(200) |
|
193
|
expect(result.data).toEqual( |
|
194
|
expect.objectContaining({ |
|
195
|
updated: expect.arrayContaining([ |
|
196
|
expect.objectContaining({ |
|
197
|
location_id: stockLocation1.id, |
|
198
|
inventory_item_id: inventoryItem1.id, |
|
199
|
stocked_quantity: 10, |
|
200
|
}), |
|
201
|
expect.objectContaining({ |
|
202
|
location_id: stockLocation2.id, |
|
203
|
inventory_item_id: inventoryItem1.id, |
|
204
|
stocked_quantity: 20, |
|
205
|
}), |
|
206
|
]), |
|
207
|
}) |
|
208
|
) |
|
209
|
}) |
|
210
|
|
|
211
|
it("should batch create the inventory levels", async () => { |
|
212
|
const result = await api.post( |
|
213
|
`/admin/inventory-items/location-levels/batch`, |
|
214
|
{ |
|
215
|
create: [ |
|
216
|
{ |
|
217
|
location_id: stockLocation3.id, |
|
218
|
inventory_item_id: inventoryItem1.id, |
|
219
|
stocked_quantity: 10, |
|
220
|
}, |
|
221
|
], |
|
222
|
}, |
|
223
|
adminHeaders |
|
224
|
) |
|
225
|
|
|
226
|
expect(result.status).toEqual(200) |
|
227
|
expect(result.data).toEqual( |
|
228
|
expect.objectContaining({ |
|
229
|
created: expect.arrayContaining([ |
|
230
|
expect.objectContaining({ |
|
231
|
location_id: stockLocation3.id, |
|
232
|
inventory_item_id: inventoryItem1.id, |
|
233
|
stocked_quantity: 10, |
|
234
|
}), |
|
235
|
]), |
|
236
|
}) |
|
237
|
) |
|
238
|
}) |
|
239
|
|
|
240
|
it("should batch delete the inventory levels when stocked quantity is 0 and force is false", async () => { |
|
241
|
const result = await api.post( |
|
242
|
`/admin/inventory-items/location-levels/batch`, |
|
243
|
{ delete: [locationLevel1.id] }, |
|
244
|
adminHeaders |
|
245
|
) |
|
246
|
|
|
247
|
expect(result.status).toEqual(200) |
|
248
|
expect(result.data).toEqual( |
|
249
|
expect.objectContaining({ |
|
250
|
deleted: [locationLevel1.id], |
|
251
|
}) |
|
252
|
) |
|
253
|
}) |
|
254
|
|
|
255
|
it("should not delete the inventory levels when stocked quantity is greater than 0 and force is false", async () => { |
|
256
|
const error = await api |
|
257
|
.post( |
|
258
|
`/admin/inventory-items/location-levels/batch`, |
|
259
|
{ delete: [locationLevel2.id] }, |
|
260
|
adminHeaders |
|
261
|
) |
|
262
|
.catch((e) => e) |
|
263
|
|
|
264
|
expect(error.response.status).toEqual(400) |
|
265
|
expect(error.response.data).toEqual({ |
|
266
|
type: "not_allowed", |
|
267
|
message: `Cannot remove Inventory Levels for ${stockLocation2.id} because there are stocked items at the locations. Use force flag to delete anyway.`, |
|
268
|
}) |
|
269
|
}) |
|
270
|
|
|
271
|
it("should delete the inventory levels when stocked quantity is greater than 0 and force is true", async () => { |
|
272
|
const result = await api.post( |
|
273
|
`/admin/inventory-items/location-levels/batch`, |
|
274
|
{ delete: [locationLevel2.id], force: true }, |
|
275
|
adminHeaders |
|
276
|
) |
|
277
|
|
|
278
|
expect(result.status).toEqual(200) |
|
279
|
expect(result.data).toEqual( |
|
280
|
expect.objectContaining({ |
|
281
|
deleted: [locationLevel2.id], |
|
282
|
}) |
|
283
|
) |
|
284
|
}) |
|
285
|
|
|
286
|
it("should not call listInventoryLevels when update array is empty", async () => { |
|
287
|
const container = getContainer() |
|
288
|
const inventoryService = container.resolve(Modules.INVENTORY) |
|
289
|
|
|
290
|
const listInventoryLevelsSpy = jest.spyOn( |
|
291
|
inventoryService, |
|
292
|
"listInventoryLevels" |
|
293
|
) |
|
294
|
|
|
295
|
const result = await api.post( |
|
296
|
`/admin/inventory-items/location-levels/batch`, |
|
297
|
{ |
|
298
|
update: [], |
|
299
|
create: [], |
|
300
|
delete: [], |
|
301
|
}, |
|
302
|
adminHeaders |
|
303
|
) |
|
304
|
|
|
305
|
expect(result.status).toEqual(200) |
|
306
|
expect(result.data).toEqual( |
|
307
|
expect.objectContaining({ |
|
308
|
created: [], |
|
309
|
updated: [], |
|
310
|
deleted: [], |
|
311
|
}) |
|
312
|
) |
|
313
|
|
|
314
|
expect(listInventoryLevelsSpy).not.toHaveBeenCalled() |
|
315
|
|
|
316
|
listInventoryLevelsSpy.mockRestore() |
|
317
|
}) |
|
318
|
}) |
|
319
|
|
|
320
|
describe("POST /admin/inventory-items/:id/location-levels/batch", () => { |
|
321
|
let locationLevel1 |
|
322
|
|
|
323
|
beforeEach(async () => { |
|
324
|
const seed = await api.post( |
|
325
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
326
|
{ |
|
327
|
location_id: stockLocation1.id, |
|
328
|
stocked_quantity: 0, |
|
329
|
}, |
|
330
|
adminHeaders |
|
331
|
) |
|
332
|
|
|
333
|
locationLevel1 = seed.data.inventory_item.location_levels[0] |
|
334
|
}) |
|
335
|
|
|
336
|
it("should delete an inventory location level and create a new one", async () => { |
|
337
|
const result = await api.post( |
|
338
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/batch`, |
|
339
|
{ |
|
340
|
create: [{ location_id: "location_2" }], |
|
341
|
delete: [locationLevel1.id], |
|
342
|
force: true, |
|
343
|
}, |
|
344
|
adminHeaders |
|
345
|
) |
|
346
|
|
|
347
|
expect(result.status).toEqual(200) |
|
348
|
|
|
349
|
const levelsListResult = await api.get( |
|
350
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
351
|
adminHeaders |
|
352
|
) |
|
353
|
expect(levelsListResult.status).toEqual(200) |
|
354
|
expect(levelsListResult.data.inventory_levels).toHaveLength(1) |
|
355
|
}) |
|
356
|
|
|
357
|
it("should not delete an inventory location level when there is stocked items without force", async () => { |
|
358
|
await api.post( |
|
359
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/${stockLocation1.id}`, |
|
360
|
{ stocked_quantity: 10 }, |
|
361
|
adminHeaders |
|
362
|
) |
|
363
|
|
|
364
|
const { response } = await api |
|
365
|
.post( |
|
366
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/batch`, |
|
367
|
{ delete: [locationLevel1.id] }, |
|
368
|
adminHeaders |
|
369
|
) |
|
370
|
.catch((e) => e) |
|
371
|
|
|
372
|
expect(response.status).toEqual(400) |
|
373
|
expect(response.data).toEqual({ |
|
374
|
type: "not_allowed", |
|
375
|
message: `Cannot remove Inventory Levels for ${stockLocation1.id} because there are stocked items at the locations. Use force flag to delete anyway.`, |
|
376
|
}) |
|
377
|
}) |
|
378
|
|
|
379
|
it("should successfully add an inventory location", async () => { |
|
380
|
await api.post( |
|
381
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/${stockLocation1.id}`, |
|
382
|
{ stocked_quantity: 10 }, |
|
383
|
adminHeaders |
|
384
|
) |
|
385
|
|
|
386
|
await api.post( |
|
387
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/batch`, |
|
388
|
{ create: [{ location_id: stockLocation2.id }] }, |
|
389
|
adminHeaders |
|
390
|
) |
|
391
|
|
|
392
|
const { |
|
393
|
data: { inventory_levels: inventoryLevels }, |
|
394
|
} = await api.get( |
|
395
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
396
|
adminHeaders |
|
397
|
) |
|
398
|
|
|
399
|
expect(inventoryLevels).toEqual( |
|
400
|
expect.arrayContaining([ |
|
401
|
expect.objectContaining({ |
|
402
|
location_id: stockLocation1.id, |
|
403
|
}), |
|
404
|
expect.objectContaining({ |
|
405
|
location_id: stockLocation2.id, |
|
406
|
}), |
|
407
|
]) |
|
408
|
) |
|
409
|
}) |
|
410
|
}) |
|
411
|
|
|
412
|
describe("DELETE /admin/inventory-items/:id/location-levels/:id", () => { |
|
413
|
beforeEach(async () => { |
|
414
|
await api.post( |
|
415
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
416
|
{ |
|
417
|
location_id: stockLocation1.id, |
|
418
|
stocked_quantity: 0, |
|
419
|
}, |
|
420
|
adminHeaders |
|
421
|
) |
|
422
|
}) |
|
423
|
|
|
424
|
it("should delete an inventory location level without reservations", async () => { |
|
425
|
const result = await api.delete( |
|
426
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/${stockLocation1.id}`, |
|
427
|
adminHeaders |
|
428
|
) |
|
429
|
|
|
430
|
expect(result.status).toEqual(200) |
|
431
|
expect(result.data).toEqual({ |
|
432
|
id: expect.any(String), |
|
433
|
object: "inventory-level", |
|
434
|
deleted: true, |
|
435
|
parent: expect.any(Object), |
|
436
|
}) |
|
437
|
}) |
|
438
|
|
|
439
|
it("should fail delete an inventory location level with reservations", async () => { |
|
440
|
await api.post( |
|
441
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/${stockLocation1.id}`, |
|
442
|
{ stocked_quantity: 10 }, |
|
443
|
adminHeaders |
|
444
|
) |
|
445
|
|
|
446
|
await api.post( |
|
447
|
`/admin/reservations`, |
|
448
|
{ |
|
449
|
inventory_item_id: inventoryItem1.id, |
|
450
|
location_id: stockLocation1.id, |
|
451
|
quantity: 5, |
|
452
|
}, |
|
453
|
adminHeaders |
|
454
|
) |
|
455
|
|
|
456
|
let error |
|
457
|
await api |
|
458
|
.delete( |
|
459
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/${stockLocation1.id}`, |
|
460
|
adminHeaders |
|
461
|
) |
|
462
|
.catch((e) => (error = e)) |
|
463
|
expect(error.response.status).toEqual(400) |
|
464
|
expect(error.response.data).toEqual({ |
|
465
|
type: "not_allowed", |
|
466
|
message: `Cannot remove Inventory Level ${inventoryItem1.id} at Location ${stockLocation1.id} because there are reservations at location`, |
|
467
|
}) |
|
468
|
}) |
|
469
|
}) |
|
470
|
|
|
471
|
describe("POST /admin/inventory-items/:id/location-levels/:id", () => { |
|
472
|
beforeEach(async () => { |
|
473
|
await api.post( |
|
474
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
475
|
{ |
|
476
|
location_id: stockLocation1.id, |
|
477
|
stocked_quantity: 10, |
|
478
|
}, |
|
479
|
adminHeaders |
|
480
|
) |
|
481
|
}) |
|
482
|
|
|
483
|
it("should update the stocked and incoming quantity for an inventory level", async () => { |
|
484
|
const result = await api.post( |
|
485
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/${stockLocation1.id}`, |
|
486
|
{ |
|
487
|
stocked_quantity: 15, |
|
488
|
incoming_quantity: 5, |
|
489
|
}, |
|
490
|
adminHeaders |
|
491
|
) |
|
492
|
|
|
493
|
expect(result.status).toEqual(200) |
|
494
|
expect(result.data.inventory_item).toEqual( |
|
495
|
expect.objectContaining({ |
|
496
|
id: inventoryItem1.id, |
|
497
|
location_levels: expect.arrayContaining([ |
|
498
|
expect.objectContaining({ |
|
499
|
id: expect.any(String), |
|
500
|
inventory_item_id: inventoryItem1.id, |
|
501
|
location_id: stockLocation1.id, |
|
502
|
stocked_quantity: 15, |
|
503
|
reserved_quantity: 0, |
|
504
|
incoming_quantity: 5, |
|
505
|
metadata: null, |
|
506
|
}), |
|
507
|
]), |
|
508
|
}) |
|
509
|
) |
|
510
|
}) |
|
511
|
|
|
512
|
it("should fail to update a non-existing location level", async () => { |
|
513
|
const error = await api |
|
514
|
.post( |
|
515
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/does-not-exist`, |
|
516
|
{ |
|
517
|
stocked_quantity: 15, |
|
518
|
incoming_quantity: 5, |
|
519
|
}, |
|
520
|
adminHeaders |
|
521
|
) |
|
522
|
.catch((e) => e) |
|
523
|
|
|
524
|
expect(error.response.status).toEqual(404) |
|
525
|
expect(error.response.data).toEqual({ |
|
526
|
type: "not_found", |
|
527
|
message: `Item ${inventoryItem1.id} is not stocked at location does-not-exist`, |
|
528
|
}) |
|
529
|
}) |
|
530
|
|
|
531
|
it("should fail to update a non-existing inventory_item_id level", async () => { |
|
532
|
const error = await api |
|
533
|
.post( |
|
534
|
`/admin/inventory-items/does-not-exist/location-levels/${stockLocation1.id}`, |
|
535
|
{ |
|
536
|
stocked_quantity: 15, |
|
537
|
incoming_quantity: 5, |
|
538
|
}, |
|
539
|
adminHeaders |
|
540
|
) |
|
541
|
.catch((e) => e) |
|
542
|
|
|
543
|
expect(error.response.status).toEqual(404) |
|
544
|
expect(error.response.data).toEqual({ |
|
545
|
type: "not_found", |
|
546
|
message: `Item does-not-exist is not stocked at location ${stockLocation1.id}`, |
|
547
|
}) |
|
548
|
}) |
|
549
|
|
|
550
|
it("should fail to update the location level to negative quantity", async () => { |
|
551
|
const res = await api |
|
552
|
.post( |
|
553
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels/${stockLocation1.id}`, |
|
554
|
{ |
|
555
|
incoming_quantity: -1, |
|
556
|
stocked_quantity: -1, |
|
557
|
}, |
|
558
|
adminHeaders |
|
559
|
) |
|
560
|
.catch((error) => error) |
|
561
|
|
|
562
|
expect(res.response.status).toEqual(400) |
|
563
|
expect(res.response.data).toEqual({ |
|
564
|
type: "invalid_data", |
|
565
|
message: |
|
566
|
"Invalid request: Value for field 'stocked_quantity' too small, expected at least: '0'; Value for field 'incoming_quantity' too small, expected at least: '0'", |
|
567
|
}) |
|
568
|
}) |
|
569
|
}) |
|
570
|
|
|
571
|
describe("POST /admin/inventory-items/:id/location-levels", () => { |
|
572
|
it("should create location levels for an inventory item", async () => { |
|
573
|
await api.post( |
|
574
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
575
|
{ |
|
576
|
location_id: stockLocation1.id, |
|
577
|
stocked_quantity: 10, |
|
578
|
}, |
|
579
|
adminHeaders |
|
580
|
) |
|
581
|
|
|
582
|
await api.post( |
|
583
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
584
|
{ |
|
585
|
location_id: stockLocation2.id, |
|
586
|
stocked_quantity: 5, |
|
587
|
}, |
|
588
|
adminHeaders |
|
589
|
) |
|
590
|
|
|
591
|
const levels = ( |
|
592
|
await api.get( |
|
593
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
594
|
adminHeaders |
|
595
|
) |
|
596
|
).data.inventory_levels |
|
597
|
|
|
598
|
expect(levels).toHaveLength(2) |
|
599
|
expect(levels).toEqual([ |
|
600
|
expect.objectContaining({ |
|
601
|
location_id: stockLocation1.id, |
|
602
|
stocked_quantity: 10, |
|
603
|
}), |
|
604
|
expect.objectContaining({ |
|
605
|
location_id: stockLocation2.id, |
|
606
|
stocked_quantity: 5, |
|
607
|
}), |
|
608
|
]) |
|
609
|
}) |
|
610
|
|
|
611
|
it("should fail to create a location level for an inventory item", async () => { |
|
612
|
const error = await api |
|
613
|
.post( |
|
614
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
615
|
{ |
|
616
|
location_id: "does-not-exist", |
|
617
|
stocked_quantity: 10, |
|
618
|
}, |
|
619
|
adminHeaders |
|
620
|
) |
|
621
|
.catch((error) => error) |
|
622
|
|
|
623
|
expect(error.response.status).toEqual(404) |
|
624
|
expect(error.response.data).toEqual({ |
|
625
|
type: "not_found", |
|
626
|
message: `Stock locations with ids: does-not-exist was not found`, |
|
627
|
}) |
|
628
|
}) |
|
629
|
}) |
|
630
|
|
|
631
|
describe("GET /admin/inventory-items", () => { |
|
632
|
it("should list the inventory items", async () => { |
|
633
|
await api.post( |
|
634
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
635
|
{ |
|
636
|
location_id: stockLocation1.id, |
|
637
|
stocked_quantity: 10, |
|
638
|
}, |
|
639
|
adminHeaders |
|
640
|
) |
|
641
|
|
|
642
|
await api.post( |
|
643
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
644
|
{ |
|
645
|
location_id: stockLocation2.id, |
|
646
|
stocked_quantity: 5, |
|
647
|
}, |
|
648
|
adminHeaders |
|
649
|
) |
|
650
|
|
|
651
|
const response = await api.get( |
|
652
|
`/admin/inventory-items?location_levels[location_id]=${stockLocation1.id}`, |
|
653
|
adminHeaders |
|
654
|
) |
|
655
|
|
|
656
|
expect(response.data.inventory_items).toHaveLength(1) |
|
657
|
expect(response.data.inventory_items[0]).toEqual( |
|
658
|
expect.objectContaining({ |
|
659
|
id: inventoryItem1.id, |
|
660
|
sku: "12345", |
|
661
|
origin_country: "UK", |
|
662
|
hs_code: "hs001", |
|
663
|
mid_code: "mids", |
|
664
|
material: "material", |
|
665
|
weight: 300, |
|
666
|
length: 100, |
|
667
|
height: 200, |
|
668
|
width: 150, |
|
669
|
requires_shipping: true, |
|
670
|
metadata: null, |
|
671
|
location_levels: expect.arrayContaining([ |
|
672
|
expect.objectContaining({ |
|
673
|
id: expect.any(String), |
|
674
|
inventory_item_id: inventoryItem1.id, |
|
675
|
location_id: stockLocation1.id, |
|
676
|
stocked_quantity: 10, |
|
677
|
reserved_quantity: 0, |
|
678
|
incoming_quantity: 0, |
|
679
|
metadata: null, |
|
680
|
available_quantity: 10, |
|
681
|
}), |
|
682
|
expect.objectContaining({ |
|
683
|
id: expect.any(String), |
|
684
|
inventory_item_id: inventoryItem1.id, |
|
685
|
location_id: stockLocation2.id, |
|
686
|
stocked_quantity: 5, |
|
687
|
reserved_quantity: 0, |
|
688
|
incoming_quantity: 0, |
|
689
|
metadata: null, |
|
690
|
available_quantity: 5, |
|
691
|
}), |
|
692
|
]), |
|
693
|
reserved_quantity: 0, |
|
694
|
stocked_quantity: 15, |
|
695
|
}) |
|
696
|
) |
|
697
|
}) |
|
698
|
|
|
699
|
it("should list the inventory items searching by title, description and sku", async () => { |
|
700
|
await api.post( |
|
701
|
`/admin/inventory-items`, |
|
702
|
{ |
|
703
|
title: "Test Item", |
|
704
|
}, |
|
705
|
adminHeaders |
|
706
|
) |
|
707
|
await api.post( |
|
708
|
`/admin/inventory-items`, |
|
709
|
{ |
|
710
|
description: "Test Desc", |
|
711
|
}, |
|
712
|
adminHeaders |
|
713
|
) |
|
714
|
await api.post( |
|
715
|
`/admin/inventory-items`, |
|
716
|
{ |
|
717
|
sku: "Test Sku", |
|
718
|
}, |
|
719
|
adminHeaders |
|
720
|
) |
|
721
|
|
|
722
|
const response = await api.get( |
|
723
|
`/admin/inventory-items?q=test`, |
|
724
|
adminHeaders |
|
725
|
) |
|
726
|
|
|
727
|
expect(response.data.inventory_items).not.toEqual( |
|
728
|
expect.arrayContaining([ |
|
729
|
expect.objectContaining({ |
|
730
|
sku: "MY_SKU", |
|
731
|
}), |
|
732
|
]) |
|
733
|
) |
|
734
|
expect(response.data.inventory_items).toHaveLength(3) |
|
735
|
expect(response.data.inventory_items).toEqual( |
|
736
|
expect.arrayContaining([ |
|
737
|
expect.objectContaining({ |
|
738
|
sku: "Test Sku", |
|
739
|
}), |
|
740
|
expect.objectContaining({ |
|
741
|
description: "Test Desc", |
|
742
|
}), |
|
743
|
expect.objectContaining({ |
|
744
|
title: "Test Item", |
|
745
|
}), |
|
746
|
]) |
|
747
|
) |
|
748
|
}) |
|
749
|
}) |
|
750
|
|
|
751
|
describe("GET /admin/inventory-items/:id", () => { |
|
752
|
it("should retrieve the inventory item", async () => { |
|
753
|
await api.post( |
|
754
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
755
|
{ |
|
756
|
location_id: stockLocation1.id, |
|
757
|
stocked_quantity: 15, |
|
758
|
incoming_quantity: 5, |
|
759
|
}, |
|
760
|
adminHeaders |
|
761
|
) |
|
762
|
|
|
763
|
await api.post( |
|
764
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
765
|
{ |
|
766
|
location_id: stockLocation2.id, |
|
767
|
stocked_quantity: 7, |
|
768
|
incoming_quantity: 0, |
|
769
|
}, |
|
770
|
adminHeaders |
|
771
|
) |
|
772
|
|
|
773
|
await api.post( |
|
774
|
`/admin/reservations`, |
|
775
|
{ |
|
776
|
line_item_id: "line-item-id-1", |
|
777
|
inventory_item_id: inventoryItem1.id, |
|
778
|
location_id: stockLocation2.id, |
|
779
|
description: "test description", |
|
780
|
quantity: 1, |
|
781
|
}, |
|
782
|
adminHeaders |
|
783
|
) |
|
784
|
|
|
785
|
const response = await api.get( |
|
786
|
`/admin/inventory-items/${inventoryItem1.id}`, |
|
787
|
adminHeaders |
|
788
|
) |
|
789
|
|
|
790
|
expect(response.data.inventory_item).toEqual( |
|
791
|
expect.objectContaining({ |
|
792
|
id: inventoryItem1.id, |
|
793
|
sku: "12345", |
|
794
|
origin_country: "UK", |
|
795
|
hs_code: "hs001", |
|
796
|
material: "material", |
|
797
|
mid_code: "mids", |
|
798
|
requires_shipping: true, |
|
799
|
weight: 300, |
|
800
|
length: 100, |
|
801
|
height: 200, |
|
802
|
width: 150, |
|
803
|
stocked_quantity: 22, |
|
804
|
reserved_quantity: 1, |
|
805
|
location_levels: [ |
|
806
|
expect.objectContaining({ |
|
807
|
id: expect.any(String), |
|
808
|
inventory_item_id: inventoryItem1.id, |
|
809
|
location_id: stockLocation1.id, |
|
810
|
stocked_quantity: 15, |
|
811
|
reserved_quantity: 0, |
|
812
|
incoming_quantity: 5, |
|
813
|
available_quantity: 15, |
|
814
|
metadata: null, |
|
815
|
}), |
|
816
|
expect.objectContaining({ |
|
817
|
id: expect.any(String), |
|
818
|
inventory_item_id: inventoryItem1.id, |
|
819
|
location_id: stockLocation2.id, |
|
820
|
stocked_quantity: 7, |
|
821
|
reserved_quantity: 1, |
|
822
|
incoming_quantity: 0, |
|
823
|
available_quantity: 6, |
|
824
|
metadata: null, |
|
825
|
}), |
|
826
|
], |
|
827
|
}) |
|
828
|
) |
|
829
|
}) |
|
830
|
|
|
831
|
it("should retrieve the inventory item with correct stocked quantity given location levels have been deleted", async () => { |
|
832
|
await api.post( |
|
833
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
834
|
{ |
|
835
|
location_id: stockLocation1.id, |
|
836
|
stocked_quantity: 10, |
|
837
|
incoming_quantity: 0, |
|
838
|
}, |
|
839
|
adminHeaders |
|
840
|
) |
|
841
|
|
|
842
|
const item = ( |
|
843
|
await api.post( |
|
844
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
845
|
{ |
|
846
|
location_id: stockLocation2.id, |
|
847
|
stocked_quantity: 10, |
|
848
|
incoming_quantity: 0, |
|
849
|
}, |
|
850
|
adminHeaders |
|
851
|
) |
|
852
|
).data.inventory_item |
|
853
|
|
|
854
|
await api.post( |
|
855
|
`/admin/reservations`, |
|
856
|
{ |
|
857
|
line_item_id: "line-item-id-1", |
|
858
|
inventory_item_id: inventoryItem1.id, |
|
859
|
location_id: stockLocation2.id, |
|
860
|
description: "test description", |
|
861
|
quantity: 1, |
|
862
|
}, |
|
863
|
adminHeaders |
|
864
|
) |
|
865
|
|
|
866
|
const reservation = ( |
|
867
|
await api.post( |
|
868
|
`/admin/reservations`, |
|
869
|
{ |
|
870
|
line_item_id: "line-item-id-2", |
|
871
|
inventory_item_id: inventoryItem1.id, |
|
872
|
location_id: stockLocation2.id, |
|
873
|
description: "test description 2", |
|
874
|
quantity: 1, |
|
875
|
}, |
|
876
|
adminHeaders |
|
877
|
) |
|
878
|
).data.reservation |
|
879
|
|
|
880
|
await api.delete( |
|
881
|
`/admin/reservations/${reservation.id}`, |
|
882
|
adminHeaders |
|
883
|
) |
|
884
|
|
|
885
|
const response = await api.get( |
|
886
|
`/admin/inventory-items/${inventoryItem1.id}`, |
|
887
|
adminHeaders |
|
888
|
) |
|
889
|
|
|
890
|
expect(response.data.inventory_item).toEqual( |
|
891
|
expect.objectContaining({ |
|
892
|
id: inventoryItem1.id, |
|
893
|
stocked_quantity: 20, |
|
894
|
reserved_quantity: 1, |
|
895
|
}) |
|
896
|
) |
|
897
|
}) |
|
898
|
|
|
899
|
it("should throw if inventory item doesn't exist", async () => { |
|
900
|
const error = await api |
|
901
|
.get(`/admin/inventory-items/does-not-exist`, adminHeaders) |
|
902
|
.catch((e) => e) |
|
903
|
|
|
904
|
expect(error.response.status).toEqual(404) |
|
905
|
expect(error.response.data).toEqual({ |
|
906
|
type: "not_found", |
|
907
|
message: "Inventory item with id: does-not-exist was not found", |
|
908
|
}) |
|
909
|
}) |
|
910
|
}) |
|
911
|
|
|
912
|
describe("POST /admin/inventory-items", () => { |
|
913
|
it("should create inventory items", async () => { |
|
914
|
const response = await api.post( |
|
915
|
`/admin/inventory-items`, |
|
916
|
{ |
|
917
|
sku: "test-sku", |
|
918
|
}, |
|
919
|
adminHeaders |
|
920
|
) |
|
921
|
|
|
922
|
expect(response.status).toEqual(200) |
|
923
|
expect(response.data.inventory_item).toEqual( |
|
924
|
expect.objectContaining({ |
|
925
|
sku: "test-sku", |
|
926
|
}) |
|
927
|
) |
|
928
|
}) |
|
929
|
|
|
930
|
it("should create inventory items with a unit of measure", async () => { |
|
931
|
const response = await api.post( |
|
932
|
`/admin/inventory-items`, |
|
933
|
{ |
|
934
|
sku: "test-sku-lb", |
|
935
|
unit_of_measure: "lb", |
|
936
|
}, |
|
937
|
adminHeaders |
|
938
|
) |
|
939
|
|
|
940
|
expect(response.status).toEqual(200) |
|
941
|
expect(response.data.inventory_item).toEqual( |
|
942
|
expect.objectContaining({ |
|
943
|
sku: "test-sku-lb", |
|
944
|
unit_of_measure: "lb", |
|
945
|
}) |
|
946
|
) |
|
947
|
|
|
948
|
const updateResponse = await api.post( |
|
949
|
`/admin/inventory-items/${response.data.inventory_item.id}`, |
|
950
|
{ |
|
951
|
unit_of_measure: "kg", |
|
952
|
}, |
|
953
|
adminHeaders |
|
954
|
) |
|
955
|
|
|
956
|
expect(updateResponse.data.inventory_item).toEqual( |
|
957
|
expect.objectContaining({ |
|
958
|
sku: "test-sku-lb", |
|
959
|
unit_of_measure: "kg", |
|
960
|
}) |
|
961
|
) |
|
962
|
}) |
|
963
|
|
|
964
|
it("should create inventory items along with location levels", async () => { |
|
965
|
const response = await api.post( |
|
966
|
`/admin/inventory-items?fields=*location_levels`, |
|
967
|
{ |
|
968
|
sku: "test-sku", |
|
969
|
location_levels: [ |
|
970
|
{ |
|
971
|
location_id: stockLocation1.id, |
|
972
|
stocked_quantity: 20, |
|
973
|
incoming_quantity: 40, |
|
974
|
}, |
|
975
|
], |
|
976
|
}, |
|
977
|
adminHeaders |
|
978
|
) |
|
979
|
|
|
980
|
expect(response.status).toEqual(200) |
|
981
|
expect(response.data.inventory_item).toEqual( |
|
982
|
expect.objectContaining({ |
|
983
|
sku: "test-sku", |
|
984
|
location_levels: [ |
|
985
|
expect.objectContaining({ |
|
986
|
id: expect.any(String), |
|
987
|
inventory_item_id: expect.any(String), |
|
988
|
stocked_quantity: 20, |
|
989
|
incoming_quantity: 40, |
|
990
|
}), |
|
991
|
], |
|
992
|
}) |
|
993
|
) |
|
994
|
}) |
|
995
|
}) |
|
996
|
|
|
997
|
describe("POST /admin/inventory-items/:id", () => { |
|
998
|
it("should update the inventory item", async () => { |
|
999
|
const response = await api.post( |
|
1000
|
`/admin/inventory-items/${inventoryItem1.id}`, |
|
1001
|
{ |
|
1002
|
mid_code: "updated mid_code", |
|
1003
|
weight: 120, |
|
1004
|
}, |
|
1005
|
adminHeaders |
|
1006
|
) |
|
1007
|
|
|
1008
|
expect(response.data.inventory_item).toEqual( |
|
1009
|
expect.objectContaining({ |
|
1010
|
sku: "12345", |
|
1011
|
mid_code: "updated mid_code", |
|
1012
|
weight: 120, |
|
1013
|
}) |
|
1014
|
) |
|
1015
|
}) |
|
1016
|
}) |
|
1017
|
|
|
1018
|
describe("DELETE /admin/inventory-items/:id", () => { |
|
1019
|
it("should throw if inventory item with reservations is being removed", async () => { |
|
1020
|
await api.post( |
|
1021
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels`, |
|
1022
|
{ |
|
1023
|
location_id: stockLocation1.id, |
|
1024
|
stocked_quantity: 10, |
|
1025
|
}, |
|
1026
|
adminHeaders |
|
1027
|
) |
|
1028
|
|
|
1029
|
await api.post( |
|
1030
|
`/admin/reservations`, |
|
1031
|
{ |
|
1032
|
line_item_id: "line-item-id-1", |
|
1033
|
inventory_item_id: inventoryItem1.id, |
|
1034
|
location_id: stockLocation1.id, |
|
1035
|
description: "test description", |
|
1036
|
quantity: 5, |
|
1037
|
}, |
|
1038
|
adminHeaders |
|
1039
|
) |
|
1040
|
|
|
1041
|
const reservationsResponse = ( |
|
1042
|
await api.get( |
|
1043
|
`/admin/reservations?location_id[]=${stockLocation1.id}`, |
|
1044
|
adminHeaders |
|
1045
|
) |
|
1046
|
).data |
|
1047
|
|
|
1048
|
expect(reservationsResponse.count).toEqual(1) |
|
1049
|
|
|
1050
|
const levelsResponse = ( |
|
1051
|
await api.get( |
|
1052
|
`/admin/inventory-items/${inventoryItem1.id}/location-levels?location_id[]=${stockLocation1.id}`, |
|
1053
|
adminHeaders |
|
1054
|
) |
|
1055
|
).data |
|
1056
|
expect(levelsResponse.count).toEqual(1) |
|
1057
|
|
|
1058
|
const res = await api |
|
1059
|
.delete(`/admin/inventory-items/${inventoryItem1.id}`, adminHeaders) |
|
1060
|
.catch((err) => { |
|
1061
|
return err.response |
|
1062
|
}) |
|
1063
|
|
|
1064
|
expect(res.status).toEqual(400) |
|
1065
|
expect(res.data.message).toEqual( |
|
1066
|
`Cannot remove following inventory item(s) since they have reservations: [${inventoryItem1.id}].` |
|
1067
|
) |
|
1068
|
}) |
|
1069
|
|
|
1070
|
it("should remove the product variant associations when deleting an inventory item", async () => { |
|
1071
|
const product = ( |
|
1072
|
await api.post( |
|
1073
|
"/admin/products", |
|
1074
|
{ |
|
1075
|
title: "product 1", |
|
1076
|
options: [{ title: "size", values: ["large"] }], |
|
1077
|
shipping_profile_id: shippingProfile.id, |
|
1078
|
variants: [ |
|
1079
|
{ |
|
1080
|
title: "variant 1", |
|
1081
|
options: { size: "large" }, |
|
1082
|
prices: [{ currency_code: "usd", amount: 100 }], |
|
1083
|
inventory_items: [ |
|
1084
|
{ |
|
1085
|
inventory_item_id: inventoryItem1.id, |
|
1086
|
required_quantity: 10, |
|
1087
|
}, |
|
1088
|
], |
|
1089
|
}, |
|
1090
|
{ |
|
1091
|
title: "variant 2", |
|
1092
|
prices: [{ currency_code: "usd", amount: 100 }], |
|
1093
|
inventory_items: [ |
|
1094
|
{ |
|
1095
|
inventory_item_id: inventoryItem1.id, |
|
1096
|
required_quantity: 5, |
|
1097
|
}, |
|
1098
|
], |
|
1099
|
}, |
|
1100
|
], |
|
1101
|
}, |
|
1102
|
adminHeaders |
|
1103
|
) |
|
1104
|
).data.product |
|
1105
|
|
|
1106
|
const variant1 = product.variants[0] |
|
1107
|
const variant2 = product.variants[1] |
|
1108
|
|
|
1109
|
await api.delete( |
|
1110
|
`/admin/inventory-items/${inventoryItem1.id}`, |
|
1111
|
adminHeaders |
|
1112
|
) |
|
1113
|
|
|
1114
|
const updatedVariant1 = ( |
|
1115
|
await api.get( |
|
1116
|
`/admin/products/${product.id}/variants/${variant1.id}?fields=inventory_items.inventory.*,inventory_items.*`, |
|
1117
|
adminHeaders |
|
1118
|
) |
|
1119
|
).data.variant |
|
1120
|
|
|
1121
|
expect(updatedVariant1.inventory_items).toHaveLength(0) |
|
1122
|
|
|
1123
|
const updatedVariant2 = ( |
|
1124
|
await api.get( |
|
1125
|
`/admin/products/${product.id}/variants/${variant2.id}?fields=inventory_items.inventory.*,inventory_items.*`, |
|
1126
|
adminHeaders |
|
1127
|
) |
|
1128
|
).data.variant |
|
1129
|
|
|
1130
|
expect(updatedVariant2.inventory_items).toHaveLength(0) |
|
1131
|
}) |
|
1132
|
}) |
|
1133
|
|
|
1134
|
describe("workflows", () => { |
|
1135
|
describe("updateInventoryItemsWorkflow", () => { |
|
1136
|
it("should not call listInventoryItems when updates is empty", async () => { |
|
1137
|
const inventoryService = container.resolve(Modules.INVENTORY) |
|
1138
|
const listInventoryItemsSpy = jest.spyOn( |
|
1139
|
inventoryService, |
|
1140
|
"listInventoryItems" |
|
1141
|
) |
|
1142
|
|
|
1143
|
const { result } = await updateInventoryItemsWorkflow( |
|
1144
|
container |
|
1145
|
).run({ |
|
1146
|
input: { updates: [] }, |
|
1147
|
}) |
|
1148
|
|
|
1149
|
expect(result).toEqual([]) |
|
1150
|
expect(listInventoryItemsSpy).not.toHaveBeenCalled() |
|
1151
|
|
|
1152
|
listInventoryItemsSpy.mockRestore() |
|
1153
|
}) |
|
1154
|
}) |
|
1155
|
}) |
|
1156
|
}) |
|
1157
|
}, |
|
1158
|
}) |