1 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
2 import {
3 adminHeaders,
4 createAdminUser,
5 } from "../../../../helpers/create-admin-user"
6 import { Modules } from "@medusajs/utils"
7 import { updateReservationsWorkflow } from "@medusajs/core-flows"
8
9 jest.setTimeout(50000)
10
11 medusaIntegrationTestRunner({
12 testSuite: ({ dbConnection, getContainer, api, dbUtils }) => {
13 let inventoryItem1
14 let inventoryItem2
15 let stockLocation1
16 let stockLocation2
17 let appContainer
18
19 beforeAll(async () => {
20 appContainer = getContainer()
21 })
22
23 beforeAll(async () => {
24 await createAdminUser(dbConnection, adminHeaders, appContainer)
25
26 stockLocation1 = (
27 await api.post(`/admin/stock-locations`, { name: "loc1" }, adminHeaders)
28 ).data.stock_location
29
30 stockLocation2 = (
31 await api.post(`/admin/stock-locations`, { name: "loc2" }, adminHeaders)
32 ).data.stock_location
33
34 inventoryItem1 = (
35 await api.post(
36 `/admin/inventory-items`,
37 {
38 sku: "12345",
39 origin_country: "UK",
40 hs_code: "hs001",
41 mid_code: "mids",
42 material: "material",
43 weight: 300,
44 length: 100,
45 height: 200,
46 width: 150,
47 },
48 adminHeaders
49 )
50 ).data.inventory_item
51
52 inventoryItem2 = (
53 await api.post(
54 `/admin/inventory-items`,
55 { sku: "second", origin_country: "UK", hs_code: "hs001" },
56 adminHeaders
57 )
58 ).data.inventory_item
59
60 await dbUtils.snapshot()
61 })
62
63 describe("POST /admin/reservations", () => {
64 beforeEach(async () => {
65 await api.post(
66 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
67 {
68 location_id: stockLocation1.id,
69 stocked_quantity: 100,
70 },
71 adminHeaders
72 )
73 })
74
75 it("should create a reservation item", async () => {
76 const reservationResponse = await api.post(
77 `/admin/reservations`,
78 {
79 line_item_id: "line-item-id-1",
80 inventory_item_id: inventoryItem1.id,
81 location_id: stockLocation1.id,
82 description: "test description",
83 quantity: 1,
84 },
85 adminHeaders
86 )
87
88 expect(reservationResponse.status).toEqual(200)
89 expect(reservationResponse.data.reservation).toEqual(
90 expect.objectContaining({
91 line_item_id: "line-item-id-1",
92 inventory_item_id: inventoryItem1.id,
93 location_id: stockLocation1.id,
94 description: "test description",
95 quantity: 1,
96 })
97 )
98 })
99
100 it("should throw if available quantity is less than reservation quantity", async () => {
101 const payload = {
102 quantity: 10000,
103 inventory_item_id: inventoryItem1.id,
104 line_item_id: "line-item-id",
105 location_id: stockLocation1.id,
106 }
107
108 const res = await api
109 .post(
110 `/admin/reservations?fields=*inventory_item.location_levels`,
111 payload,
112 adminHeaders
113 )
114 .catch((err) => err)
115
116 const res1 = await api.get(
117 `/admin/inventory-items/${inventoryItem1.id}`,
118 adminHeaders
119 )
120
121 expect(res.response.status).toBe(400)
122 expect(res.response.data).toEqual({
123 type: "not_allowed",
124 message: `Not enough stock available for item ${inventoryItem1.id} at location ${stockLocation1.id}`,
125 })
126 })
127 })
128
129 describe("POST /admin/reservations/:id", () => {
130 let reservationId
131
132 beforeEach(async () => {
133 await api.post(
134 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
135 {
136 location_id: stockLocation1.id,
137 stocked_quantity: 100,
138 },
139 adminHeaders
140 )
141
142 const reservationResponse = await api.post(
143 `/admin/reservations`,
144 {
145 line_item_id: "line-item-id-1",
146 inventory_item_id: inventoryItem1.id,
147 location_id: stockLocation1.id,
148 description: "test description",
149 quantity: 1,
150 },
151 adminHeaders
152 )
153 reservationId = reservationResponse.data.reservation.id
154 })
155
156 it("should throw if available quantity is less than reservation quantity", async () => {
157 const payload = { quantity: 100000 }
158 const res = await api
159 .post(`/admin/reservations/${reservationId}`, payload, adminHeaders)
160 .catch((err) => err)
161
162 expect(res.response.status).toBe(400)
163 expect(res.response.data).toEqual({
164 type: "not_allowed",
165 message: `Not enough stock available for item ${inventoryItem1.id} at location ${stockLocation1.id}`,
166 })
167 })
168
169 it("should update a reservation item description", async () => {
170 const reservationResponse = await api.post(
171 `/admin/reservations/${reservationId}`,
172 {
173 description: "test description 1",
174 },
175 adminHeaders
176 )
177
178 expect(reservationResponse.status).toEqual(200)
179 expect(reservationResponse.data.reservation).toEqual(
180 expect.objectContaining({
181 description: "test description 1",
182 })
183 )
184 })
185
186 it("should update a reservation item", async () => {
187 const reservationResponse = await api.post(
188 `/admin/reservations/${reservationId}`,
189 {
190 quantity: 3,
191 },
192 adminHeaders
193 )
194
195 expect(reservationResponse.status).toEqual(200)
196 expect(reservationResponse.data.reservation).toEqual(
197 expect.objectContaining({
198 quantity: 3,
199 })
200 )
201 })
202 })
203
204 describe("DELETE /admin/reservations/:id", () => {
205 let reservationId
206
207 beforeEach(async () => {
208 await api.post(
209 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
210 {
211 location_id: stockLocation1.id,
212 stocked_quantity: 100,
213 },
214 adminHeaders
215 )
216
217 const reservationResponse = await api.post(
218 `/admin/reservations`,
219 {
220 line_item_id: "line-item-id-1",
221 inventory_item_id: inventoryItem1.id,
222 location_id: stockLocation1.id,
223 description: "test description",
224 quantity: 1,
225 },
226 adminHeaders
227 )
228 reservationId = reservationResponse.data.reservation.id
229 })
230
231 it("should update a reservation item", async () => {
232 const reservationResponse = await api.delete(
233 `/admin/reservations/${reservationId}`,
234 adminHeaders
235 )
236
237 expect(reservationResponse.status).toEqual(200)
238 expect(reservationResponse.data).toEqual(
239 expect.objectContaining({
240 id: reservationId,
241 object: "reservation",
242 deleted: true,
243 })
244 )
245
246 let error
247 await api
248 .get(`/admin/reservations/${reservationId}`, adminHeaders)
249 .catch((err) => {
250 error = err
251 })
252
253 expect(error.response.status).toBe(404)
254 })
255 })
256
257 describe("GET /admin/reservations", () => {
258 let reservation
259 let reservation2
260 let scId
261
262 beforeEach(async () => {
263 const scResponse = await api.post(
264 `/admin/sales-channels`,
265 { name: "test" },
266 adminHeaders
267 )
268 scId = scResponse.data.sales_channel.id
269
270 await api.post(
271 `/admin/stock-locations/${stockLocation1.id}/sales-channels`,
272 {
273 add: [scId],
274 },
275 adminHeaders
276 )
277 await api.post(
278 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
279 {
280 location_id: stockLocation1.id,
281 stocked_quantity: 100,
282 },
283 adminHeaders
284 )
285
286 await api.post(
287 `/admin/inventory-items/${inventoryItem2.id}/location-levels`,
288 {
289 location_id: stockLocation1.id,
290 stocked_quantity: 100,
291 },
292 adminHeaders
293 )
294
295 const reservationResponse = await api.post(
296 `/admin/reservations`,
297 {
298 line_item_id: "line-item-id-1",
299 inventory_item_id: inventoryItem1.id,
300 location_id: stockLocation1.id,
301 quantity: 2,
302 },
303 adminHeaders
304 )
305
306 reservation = reservationResponse.data.reservation
307
308 const reservationResponse2 = await api.post(
309 `/admin/reservations`,
310 {
311 line_item_id: "line-item-id-2",
312 inventory_item_id: inventoryItem2.id,
313 location_id: stockLocation1.id,
314 description: "test description",
315 quantity: 1,
316 },
317 adminHeaders
318 )
319
320 reservation2 = reservationResponse2.data.reservation
321 })
322
323 it("lists reservation items", async () => {
324 const reservationsRes = await api
325 .get(`/admin/reservations`, adminHeaders)
326 .catch(console.warn)
327
328 expect(reservationsRes.data.reservations.length).toBe(2)
329 expect(reservationsRes.data.reservations).toEqual(
330 expect.arrayContaining([
331 expect.objectContaining({
332 id: reservation.id,
333 }),
334 expect.objectContaining({
335 id: reservation2.id,
336 }),
337 ])
338 )
339 })
340
341 it("lists reservation items with free text search on SKU on inventory item", async () => {
342 const reservationsRes = await api
343 .get(`/admin/reservations?q=second`, adminHeaders)
344 .catch(console.warn)
345
346 expect(reservationsRes.data.reservations.length).toBe(1)
347 expect(reservationsRes.data.reservations).toEqual([
348 {
349 id: reservation2.id,
350 created_at: expect.any(String),
351 description: "test description",
352 inventory_item: {
353 created_at: expect.any(String),
354 description: null,
355 height: null,
356 hs_code: "hs001",
357 id: inventoryItem2.id,
358 length: null,
359 material: null,
360 metadata: null,
361 mid_code: null,
362 origin_country: "UK",
363 requires_shipping: true,
364 unit_of_measure: null,
365 reserved_quantity: 1,
366 sku: "second",
367 stocked_quantity: 100,
368 thumbnail: null,
369 title: null,
370 updated_at: expect.any(String),
371 weight: null,
372 width: null,
373 },
374 inventory_item_id: inventoryItem2.id,
375 line_item_id: "line-item-id-2",
376 location_id: stockLocation1.id,
377 metadata: null,
378 quantity: 1,
379 updated_at: expect.any(String),
380 },
381 ])
382 })
383
384 it("lists reservation items with free text search on description", async () => {
385 const reservationsRes = await api
386 .get(`/admin/reservations?q=test`, adminHeaders)
387 .catch(console.warn)
388
389 expect(reservationsRes.data.reservations.length).toBe(1)
390 expect(reservationsRes.data.reservations).toEqual([
391 {
392 id: reservation2.id,
393 created_at: expect.any(String),
394 description: "test description",
395 inventory_item: {
396 created_at: expect.any(String),
397 description: null,
398 height: null,
399 hs_code: "hs001",
400 id: inventoryItem2.id,
401 length: null,
402 material: null,
403 metadata: null,
404 mid_code: null,
405 origin_country: "UK",
406 requires_shipping: true,
407 unit_of_measure: null,
408 reserved_quantity: 1,
409 sku: "second",
410 stocked_quantity: 100,
411 thumbnail: null,
412 title: null,
413 updated_at: expect.any(String),
414 weight: null,
415 width: null,
416 },
417 inventory_item_id: inventoryItem2.id,
418 line_item_id: "line-item-id-2",
419 location_id: stockLocation1.id,
420 metadata: null,
421 quantity: 1,
422 updated_at: expect.any(String),
423 },
424 ])
425 })
426
427 it("lists reservations with inventory_items", async () => {
428 const res = await api.get(
429 `/admin/reservations?fields=%2binventory_item.*`,
430 adminHeaders
431 )
432
433 expect(res.status).toEqual(200)
434 expect(res.data.reservations.length).toEqual(2)
435 expect(res.data.reservations).toEqual(
436 expect.arrayContaining([
437 expect.objectContaining({
438 inventory_item: expect.objectContaining({
439 id: inventoryItem1.id,
440 }),
441 }),
442 expect.objectContaining({
443 inventory_item: expect.objectContaining({
444 id: inventoryItem2.id,
445 }),
446 }),
447 ])
448 )
449 })
450
451 describe("Filters reservation items", () => {
452 it("filters by location", async () => {
453 const reservationsRes = await api.get(
454 `/admin/reservations?location_id[]=${stockLocation1.id}`,
455 adminHeaders
456 )
457
458 expect(reservationsRes.data.reservations.length).toBe(2)
459 expect(reservationsRes.data.reservations[0].location_id).toBe(
460 stockLocation1.id
461 )
462 })
463
464 it("filters by itemID", async () => {
465 const reservationsRes = await api.get(
466 `/admin/reservations?inventory_item_id[]=${inventoryItem1.id}`,
467 adminHeaders
468 )
469
470 expect(reservationsRes.data.reservations.length).toBe(1)
471 expect(reservationsRes.data.reservations[0].inventory_item_id).toBe(
472 inventoryItem1.id
473 )
474 })
475
476 it("filters by quantity", async () => {
477 const reservationsRes = await api.get(
478 `/admin/reservations?quantity[$gt]=1`,
479 adminHeaders
480 )
481
482 expect(reservationsRes.data.reservations.length).toBe(1)
483 expect(reservationsRes.data.reservations[0].id).toBe(reservation.id)
484 })
485
486 it("filters by date", async () => {
487 const reservationsRes = await api.get(
488 `/admin/reservations?created_at[$gte]=${new Date(
489 reservation2.created_at
490 ).toISOString()}`,
491 adminHeaders
492 )
493
494 expect(reservationsRes.data.reservations.length).toBe(1)
495 expect(reservationsRes.data.reservations[0].id).toBe(reservation2.id)
496 })
497
498 it("filters by description using equals", async () => {
499 const reservationsRes = await api
500 .get(
501 `/admin/reservations?description=test%20description`,
502 adminHeaders
503 )
504 .catch(console.warn)
505
506 expect(reservationsRes.data.reservations.length).toBe(1)
507 expect(reservationsRes.data.reservations[0].id).toBe(reservation2.id)
508 })
509
510 it("filters by description using equals removes results", async () => {
511 const reservationsRes = await api.get(
512 `/admin/reservations?description=description`,
513 adminHeaders
514 )
515
516 expect(reservationsRes.data.reservations.length).toBe(0)
517 })
518
519 it("filters by description using contains", async () => {
520 const reservationsRes = await api.get(
521 `/admin/reservations?description[$ilike]=%descri%`,
522 adminHeaders
523 )
524
525 expect(reservationsRes.data.reservations.length).toBe(1)
526 expect(reservationsRes.data.reservations[0].id).toBe(reservation2.id)
527 })
528
529 it("filters by description using starts_with", async () => {
530 const reservationsRes = await api
531 .get(`/admin/reservations?description[$ilike]=test%`, adminHeaders)
532 .catch(console.log)
533
534 expect(reservationsRes.data.reservations.length).toBe(1)
535 expect(reservationsRes.data.reservations[0].id).toBe(reservation2.id)
536 })
537
538 it("filters by description using starts_with removes results", async () => {
539 const reservationsRes = await api.get(
540 `/admin/reservations?description[$ilike]=description%`,
541 adminHeaders
542 )
543
544 expect(reservationsRes.data.reservations.length).toBe(0)
545 })
546
547 it("filters by description using ends_with", async () => {
548 const reservationsRes = await api.get(
549 `/admin/reservations?description[$ilike]=%test`,
550 adminHeaders
551 )
552
553 expect(reservationsRes.data.reservations.length).toBe(0)
554 })
555
556 it("filters by description using ends_with removes results", async () => {
557 const reservationsRes = await api.get(
558 `/admin/reservations?description[$ilike]=%description`,
559 adminHeaders
560 )
561
562 expect(reservationsRes.data.reservations.length).toBe(1)
563 expect(reservationsRes.data.reservations[0].id).toBe(reservation2.id)
564 })
565 })
566 })
567
568 describe("workflows", () => {
569 it("updateReservationsWorklow - should not call listReservations when data is empty", async () => {
570 const reservationService = appContainer.resolve(Modules.INVENTORY)
571
572 const listReservationItemsSpy = jest.spyOn(
573 reservationService,
574 "listReservationItems"
575 )
576
577 const { result } = await updateReservationsWorkflow(appContainer).run({
578 input: { updates: [] },
579 })
580
581 expect(result).toEqual([])
582 expect(listReservationItemsSpy).not.toHaveBeenCalled()
583
584 listReservationItemsSpy.mockRestore()
585 })
586
587 describe("reserved_quantity consistency after soft-delete and restore", () => {
588 let reservationId: string
589 let inventoryService: any
590
591 const getLevel = async () => {
592 const res = await api.get(
593 `/admin/inventory-items/${inventoryItem1.id}/location-levels?location_id[]=${stockLocation1.id}&fields=reserved_quantity,available_quantity,stocked_quantity`,
594 adminHeaders
595 )
596 return res.data.inventory_levels[0]
597 }
598
599 beforeEach(async () => {
600 inventoryService = appContainer.resolve(Modules.INVENTORY)
601
602 await api.post(
603 `/admin/inventory-items/${inventoryItem1.id}/location-levels`,
604 { location_id: stockLocation1.id, stocked_quantity: 10 },
605 adminHeaders
606 )
607
608 const res = await api.post(
609 `/admin/reservations`,
610 {
611 line_item_id: "line-item-id-1",
612 inventory_item_id: inventoryItem1.id,
613 location_id: stockLocation1.id,
614 quantity: 5,
615 },
616 adminHeaders
617 )
618 reservationId = res.data.reservation.id
619 })
620
621 // Scenario 1: a single soft-delete + restore cycle must leave reserved_quantity unchanged.
622 it("should restore reserved_quantity to its pre-deletion value after a soft-delete/restore cycle", async () => {
623 const before = await getLevel()
624 expect(before.reserved_quantity).toBe(5)
625 expect(before.available_quantity).toBe(5)
626
627 await inventoryService.softDeleteReservationItems([reservationId])
628
629 const afterDelete = await getLevel()
630 expect(afterDelete.reserved_quantity).toBe(0)
631
632 await inventoryService.restoreReservationItems([reservationId])
633
634 const afterRestore = await getLevel()
635
636 expect(afterRestore.reserved_quantity).toBe(5)
637 expect(afterRestore.available_quantity).toBe(5)
638 })
639
640 // Scenario 2: multiple delete/restore cycles don't cause negative reserved_quantity
641 it("should not accumulate negative reserved_quantity across repeated soft-delete/restore cycles", async () => {
642 for (let i = 0; i < 3; i++) {
643 await inventoryService.softDeleteReservationItems([reservationId])
644 await inventoryService.restoreReservationItems([reservationId])
645 }
646
647 // Final deletion: leaves no visible reservation records.
648 await inventoryService.softDeleteReservationItems([reservationId])
649
650 const reservations = await api.get(
651 `/admin/reservations?inventory_item_id[]=${inventoryItem1.id}`,
652 adminHeaders
653 )
654 expect(reservations.data.reservations).toHaveLength(0)
655
656 const level = await getLevel()
657
658 expect(level.reserved_quantity).toBe(0)
659 expect(level.available_quantity).toBe(10)
660 })
661 })
662 })
663 },
664 })