1 import { IInventoryService, InventoryItemDTO, ReservationItemDTO } from "@medusajs/framework/types"
2 import {
3 BigNumber,
4 CommonEvents,
5 composeMessage,
6 InventoryEvents,
7 Module,
8 Modules,
9 } from "@medusajs/framework/utils"
10 import {
11 MockEventBusService,
12 moduleIntegrationTestRunner,
13 } from "@medusajs/test-utils"
14 import { InventoryModuleService } from "../../src/services"
15
16 jest.setTimeout(100000)
17
18 moduleIntegrationTestRunner<IInventoryService>({
19 moduleName: Modules.INVENTORY,
20 injectedDependencies: {
21 [Modules.EVENT_BUS]: new MockEventBusService(),
22 },
23 testSuite: ({ service }) => {
24 describe("Inventory Module Service", () => {
25 it(`should export the appropriate linkable configuration`, () => {
26 const linkable = Module(Modules.INVENTORY, {
27 service: InventoryModuleService,
28 }).linkable
29
30 expect(Object.keys(linkable)).toEqual([
31 "inventoryItem",
32 "inventoryLevel",
33 "reservationItem",
34 ])
35
36 Object.keys(linkable).forEach((key) => {
37 delete linkable[key].toJSON
38 })
39
40 expect(linkable).toEqual({
41 inventoryItem: {
42 id: {
43 field: "inventoryItem",
44 entity: "InventoryItem",
45 linkable: "inventory_item_id",
46 primaryKey: "id",
47 serviceName: "inventory",
48 },
49 },
50 inventoryLevel: {
51 id: {
52 field: "inventoryLevel",
53 entity: "InventoryLevel",
54 linkable: "inventory_level_id",
55 primaryKey: "id",
56 serviceName: "inventory",
57 },
58 },
59 reservationItem: {
60 id: {
61 field: "reservationItem",
62 entity: "ReservationItem",
63 linkable: "reservation_item_id",
64 primaryKey: "id",
65 serviceName: "inventory",
66 },
67 },
68 })
69 })
70
71 describe("create", () => {
72 it("should create an inventory item", async () => {
73 const data = { sku: "test-sku", origin_country: "test-country" }
74 const inventoryItem = await service.createInventoryItems(data)
75
76 expect(inventoryItem).toEqual(
77 expect.objectContaining({ id: expect.any(String), ...data })
78 )
79 })
80
81 it("should create inventory items from array", async () => {
82 const data = [
83 { sku: "test-sku", origin_country: "test-country" },
84 { sku: "test-sku-1", origin_country: "test-country-1" },
85 ]
86 const inventoryItems = await service.createInventoryItems(data)
87
88 expect(inventoryItems).toEqual([
89 expect.objectContaining({ id: expect.any(String), ...data[0] }),
90 expect.objectContaining({ id: expect.any(String), ...data[1] }),
91 ])
92 })
93 })
94
95 describe("createReservationItem", () => {
96 let inventoryItem: InventoryItemDTO
97 beforeEach(async () => {
98 inventoryItem = await service.createInventoryItems({
99 sku: "test-sku",
100 origin_country: "test-country",
101 })
102
103 await service.createInventoryLevels([
104 {
105 inventory_item_id: inventoryItem.id,
106 location_id: "location-1",
107 stocked_quantity: 2,
108 },
109 {
110 inventory_item_id: inventoryItem.id,
111 location_id: "location-2",
112 stocked_quantity: 2,
113 },
114 ])
115 })
116
117 it("should create a reservationItem", async () => {
118 const data = {
119 inventory_item_id: inventoryItem.id,
120 location_id: "location-1",
121 quantity: 2,
122 }
123
124 const reservationItem = await service.createReservationItems(data)
125
126 expect(reservationItem).toEqual(
127 expect.objectContaining({ id: expect.any(String), ...data })
128 )
129 })
130
131 it("should create adjust reserved_quantity of inventory level after creation", async () => {
132 await service.createReservationItems({
133 inventory_item_id: inventoryItem.id,
134 location_id: "location-1",
135 quantity: 2,
136 })
137
138 const inventoryLevel =
139 await service.retrieveInventoryLevelByItemAndLocation(
140 inventoryItem.id,
141 "location-1"
142 )
143
144 expect(inventoryLevel.reserved_quantity).toEqual(2)
145 })
146
147 it("should check inventory levels before creating reservation", async () => {
148 const reserveMoreThanInStock = service.createReservationItems({
149 inventory_item_id: inventoryItem.id,
150 location_id: "location-1",
151 quantity: 3,
152 })
153
154 await expect(reserveMoreThanInStock).rejects.toThrow(
155 `Not enough stock available for item ${inventoryItem.id} at location location-1`
156 )
157
158 let inventoryLevel =
159 await service.retrieveInventoryLevelByItemAndLocation(
160 inventoryItem.id,
161 "location-1"
162 )
163
164 expect(inventoryLevel.reserved_quantity).toEqual(0)
165
166 await service.createReservationItems({
167 inventory_item_id: inventoryItem.id,
168 location_id: "location-1",
169 allow_backorder: true,
170 quantity: 3,
171 })
172
173 inventoryLevel =
174 await service.retrieveInventoryLevelByItemAndLocation(
175 inventoryItem.id,
176 "location-1"
177 )
178
179 expect(inventoryLevel.reserved_quantity).toEqual(3)
180 })
181
182 it("should create reservationItems from array", async () => {
183 const data = [
184 {
185 inventory_item_id: inventoryItem.id,
186 location_id: "location-1",
187 quantity: 2,
188 },
189 {
190 inventory_item_id: inventoryItem.id,
191 location_id: "location-2",
192 allow_backorder: true,
193 quantity: 3,
194 },
195 ]
196 const reservationItems = await service.createReservationItems(data)
197
198 expect(reservationItems).toEqual([
199 expect.objectContaining({ id: expect.any(String), ...data[0] }),
200 expect.objectContaining({ id: expect.any(String), ...data[1] }),
201 ])
202 })
203
204 it("should fail to create a reservationItem for a non-existing location", async () => {
205 const data = [
206 {
207 inventory_item_id: inventoryItem.id,
208 location_id: "location-3",
209 quantity: 2,
210 },
211 ]
212
213 const err = await service
214 .createReservationItems(data)
215 .catch((error) => error)
216
217 expect(err.message).toEqual(
218 `Item ${inventoryItem.id} is not stocked at location location-3`
219 )
220 })
221 })
222
223 describe("createInventoryLevel", () => {
224 let inventoryItem: InventoryItemDTO
225
226 beforeEach(async () => {
227 inventoryItem = await service.createInventoryItems({
228 sku: "test-sku",
229 origin_country: "test-country",
230 })
231 })
232
233 it("should create an inventoryLevel", async () => {
234 const data = {
235 inventory_item_id: inventoryItem.id,
236 location_id: "location-1",
237 stocked_quantity: 2,
238 }
239
240 const inventoryLevel = await service.createInventoryLevels(data)
241
242 expect(inventoryLevel).toEqual(
243 expect.objectContaining({ id: expect.any(String), ...data })
244 )
245
246 const getItems = await service.listInventoryItems(
247 {},
248 {
249 select: [
250 "id",
251 "sku",
252 "origin_country",
253 "reserved_quantity",
254 "stocked_quantity",
255 ],
256 }
257 )
258
259 expect(getItems).toEqual([
260 {
261 id: inventoryItem.id,
262 sku: "test-sku",
263 origin_country: "test-country",
264 reserved_quantity: 0,
265 stocked_quantity: 2,
266 },
267 ])
268 })
269
270 it("should support fractional quantities on levels, reservations and item rollups", async () => {
271 await service.createInventoryLevels([
272 {
273 inventory_item_id: inventoryItem.id,
274 location_id: "location-1",
275 stocked_quantity: 49.5,
276 },
277 {
278 inventory_item_id: inventoryItem.id,
279 location_id: "location-2",
280 stocked_quantity: 48.75,
281 },
282 ])
283
284 await service.createReservationItems({
285 inventory_item_id: inventoryItem.id,
286 location_id: "location-1",
287 quantity: 0.25,
288 })
289
290 const getItems = await service.listInventoryItems(
291 {},
292 {
293 select: ["id", "reserved_quantity", "stocked_quantity"],
294 }
295 )
296
297 expect(getItems).toEqual([
298 expect.objectContaining({
299 id: inventoryItem.id,
300 stocked_quantity: 98.25,
301 reserved_quantity: 0.25,
302 }),
303 ])
304
305 const inventoryLevel =
306 await service.retrieveInventoryLevelByItemAndLocation(
307 inventoryItem.id,
308 "location-1"
309 )
310
311 expect(inventoryLevel.reserved_quantity).toEqual(0.25)
312 expect(Number(inventoryLevel.available_quantity)).toEqual(49.25)
313 })
314
315 it("should create inventoryLevels from array", async () => {
316 const data = [
317 {
318 inventory_item_id: inventoryItem.id,
319 location_id: "location-1",
320 stocked_quantity: 2,
321 },
322 {
323 inventory_item_id: inventoryItem.id,
324 location_id: "location-2",
325 stocked_quantity: 3,
326 },
327 ]
328 const inventoryLevels = await service.createInventoryLevels(data)
329
330 expect(inventoryLevels).toEqual([
331 expect.objectContaining({ id: expect.any(String), ...data[0] }),
332 expect.objectContaining({ id: expect.any(String), ...data[1] }),
333 ])
334 })
335
336 it("should not create with required quantity", async () => {
337 const data = {
338 inventory_item_id: inventoryItem.id,
339 location_id: "location-1",
340 stocked_quantity: 2,
341 reserved_quantity: 1000,
342 }
343
344 const inventoryLevel = await service.createInventoryLevels(data)
345
346 expect(inventoryLevel).toEqual(
347 expect.objectContaining({
348 id: expect.any(String),
349 ...data,
350 reserved_quantity: 0,
351 })
352 )
353 })
354 })
355
356 describe("update", () => {
357 let inventoryItem: InventoryItemDTO
358
359 beforeEach(async () => {
360 inventoryItem = await service.createInventoryItems({
361 sku: "test-sku",
362 origin_country: "test-country",
363 })
364 })
365
366 it("should update the inventory item", async () => {
367 const update = {
368 id: inventoryItem.id,
369 sku: "updated-sku",
370 }
371 const updated = await service.updateInventoryItems(update)
372
373 expect(updated).toEqual(expect.objectContaining(update))
374 })
375
376 it("should update multiple inventory items", async () => {
377 const item2 = await service.createInventoryItems({
378 sku: "test-sku-1",
379 })
380
381 const updates = [
382 {
383 id: inventoryItem.id,
384 sku: "updated-sku",
385 },
386 {
387 id: item2.id,
388 sku: "updated-sku-2",
389 },
390 ]
391 const updated = await service.updateInventoryItems(updates)
392
393 expect(updated).toEqual([
394 expect.objectContaining(updates[0]),
395 expect.objectContaining(updates[1]),
396 ])
397 })
398 })
399
400 describe("updateInventoryLevels", () => {
401 let inventoryLevel
402 let inventoryItem
403
404 beforeEach(async () => {
405 inventoryItem = await service.createInventoryItems({
406 sku: "test-sku",
407 origin_country: "test-country",
408 })
409
410 const data = {
411 inventory_item_id: inventoryItem.id,
412 location_id: "location-1",
413 stocked_quantity: 2,
414 }
415
416 inventoryLevel = await service.createInventoryLevels(data)
417 })
418
419 it("should update inventory level", async () => {
420 const updatedLevel = await service.updateInventoryLevels({
421 location_id: "location-1",
422 inventory_item_id: inventoryItem.id,
423 incoming_quantity: 4,
424 })
425
426 expect(updatedLevel.incoming_quantity).toEqual(4)
427 })
428
429 it("should fail to update inventory level for item in location that isn't stocked", async () => {
430 const error = await service
431 .updateInventoryLevels({
432 inventory_item_id: inventoryItem.id,
433 location_id: "does-not-exist",
434 stocked_quantity: 10,
435 })
436 .catch((error) => error)
437
438 expect(error.message).toEqual(
439 `Item ${inventoryItem.id} is not stocked at location does-not-exist`
440 )
441 })
442 })
443
444 describe("updateReservationItems", () => {
445 let reservationItem
446 let inventoryItem
447
448 beforeEach(async () => {
449 inventoryItem = await service.createInventoryItems({
450 sku: "test-sku",
451 origin_country: "test-country",
452 })
453
454 await service.createInventoryLevels([
455 {
456 inventory_item_id: inventoryItem.id,
457 location_id: "location-1",
458 stocked_quantity: 10,
459 },
460 {
461 inventory_item_id: inventoryItem.id,
462 location_id: "location-2",
463 stocked_quantity: 10,
464 },
465 ])
466
467 reservationItem = await service.createReservationItems({
468 inventory_item_id: inventoryItem.id,
469 location_id: "location-1",
470 quantity: 3,
471 })
472 })
473
474 it("should update a reservationItem", async () => {
475 const eventBusSpy = jest.spyOn(MockEventBusService.prototype, "emit")
476
477 const update = {
478 id: reservationItem.id,
479 quantity: 1,
480 }
481
482 const updated = await service.updateReservationItems(update)
483
484 const inventoryLevel =
485 await service.retrieveInventoryLevelByItemAndLocation(
486 reservationItem.inventory_item_id,
487 "location-1"
488 )
489
490 expect(updated).toEqual(expect.objectContaining(update))
491
492 expect(eventBusSpy).toHaveBeenCalledTimes(1)
493 expect(eventBusSpy.mock.calls[0][0]).toHaveLength(2)
494 expect(eventBusSpy).toHaveBeenNthCalledWith(
495 1,
496 expect.arrayContaining([
497 composeMessage(InventoryEvents.RESERVATION_ITEM_UPDATED, {
498 data: { id: reservationItem.id },
499 object: "reservation_item",
500 source: Modules.INVENTORY,
501 action: CommonEvents.UPDATED,
502 }),
503 composeMessage(InventoryEvents.INVENTORY_LEVEL_UPDATED, {
504 data: { id: inventoryLevel.id },
505 object: "inventory_level",
506 source: Modules.INVENTORY,
507 action: CommonEvents.UPDATED,
508 }),
509 ]),
510 {
511 internal: true,
512 }
513 )
514
515 const update2 = {
516 id: reservationItem.id,
517 quantity: 10,
518 }
519
520 eventBusSpy.mockClear()
521
522 const updated2 = await service.updateReservationItems(update2)
523
524 expect(updated2).toEqual(expect.objectContaining(update2))
525
526 expect(eventBusSpy).toHaveBeenCalledTimes(1)
527 expect(eventBusSpy.mock.calls[0][0]).toHaveLength(2)
528 expect(eventBusSpy).toHaveBeenNthCalledWith(
529 1,
530 expect.arrayContaining([
531 composeMessage(InventoryEvents.RESERVATION_ITEM_UPDATED, {
532 data: { id: reservationItem.id },
533 object: "reservation_item",
534 source: Modules.INVENTORY,
535 action: CommonEvents.UPDATED,
536 }),
537 composeMessage(InventoryEvents.INVENTORY_LEVEL_UPDATED, {
538 data: { id: inventoryLevel.id },
539 object: "inventory_level",
540 source: Modules.INVENTORY,
541 action: CommonEvents.UPDATED,
542 }),
543 ]),
544 {
545 internal: true,
546 }
547 )
548 })
549
550 it("should adjust reserved_quantity of inventory level after updates increasing reserved quantity", async () => {
551 const update = {
552 id: reservationItem.id,
553 quantity: 7,
554 }
555
556 await service.updateReservationItems(update)
557
558 const inventoryLevel =
559 await service.retrieveInventoryLevelByItemAndLocation(
560 reservationItem.inventory_item_id,
561 "location-1"
562 )
563
564 expect(inventoryLevel.reserved_quantity).toEqual(update.quantity)
565 })
566
567 it("should adjust reserved_quantity of inventory level after updates decreasing reserved quantity", async () => {
568 const update = {
569 id: reservationItem.id,
570 quantity: 2,
571 }
572
573 await service.updateReservationItems(update)
574
575 const inventoryLevel =
576 await service.retrieveInventoryLevelByItemAndLocation(
577 reservationItem.inventory_item_id,
578 "location-1"
579 )
580
581 expect(inventoryLevel.reserved_quantity).toEqual(update.quantity)
582 })
583
584 it("should throw error when increasing reserved quantity beyond availability", async () => {
585 const update = {
586 id: reservationItem.id,
587 quantity: 11,
588 }
589
590 const error = await service
591 .updateReservationItems(update)
592 .catch((e) => e)
593
594 expect(error.message).toEqual(
595 `Not enough stock available for item ${inventoryItem.id} at location location-1`
596 )
597 })
598 })
599
600 describe("deleteReservationItems", () => {
601 let inventoryItem: InventoryItemDTO
602 let reservationItems: ReservationItemDTO[]
603
604 beforeEach(async () => {
605 inventoryItem = await service.createInventoryItems({
606 sku: "test-sku",
607 origin_country: "test-country",
608 })
609
610 await service.createInventoryLevels([
611 {
612 inventory_item_id: inventoryItem.id,
613 location_id: "location-1",
614 stocked_quantity: 2,
615 },
616 {
617 inventory_item_id: inventoryItem.id,
618 location_id: "location-2",
619 stocked_quantity: 2,
620 },
621 ])
622
623 reservationItems = await service.createReservationItems([
624 {
625 inventory_item_id: inventoryItem.id,
626 location_id: "location-1",
627 quantity: 2,
628 },
629 {
630 inventory_item_id: inventoryItem.id,
631 location_id: "location-2",
632 quantity: 2,
633 },
634 ])
635 })
636
637 it("should delete reservation items by id", async () => {
638 const reservationItemsPreDeleted = await service.listReservationItems(
639 {
640 id: reservationItems.map((item) => item.id),
641 }
642 )
643
644 expect(reservationItemsPreDeleted).toEqual(
645 expect.arrayContaining([
646 expect.objectContaining({
647 inventory_item_id: inventoryItem.id,
648 location_id: "location-1",
649 quantity: 2,
650 }),
651 expect.objectContaining({
652 inventory_item_id: inventoryItem.id,
653 location_id: "location-2",
654 quantity: 2,
655 }),
656 ])
657 )
658 expect(reservationItemsPreDeleted).toHaveLength(2)
659
660 await service.deleteReservationItems(
661 reservationItems.map((item) => item.id)
662 )
663
664 const reservationItemsPostDeleted =
665 await service.listReservationItems({
666 id: reservationItems.map((item) => item.id),
667 })
668
669 expect(reservationItemsPostDeleted).toEqual([])
670 })
671
672 it("should adjust inventory levels accordingly when removing reservations by id", async () => {
673 const reservationItem = (await service.listReservationItems({ location_id: "location-1"}))[0]
674
675 const inventoryLevelBefore =
676 await service.retrieveInventoryLevelByItemAndLocation(
677 inventoryItem.id,
678 "location-1"
679 )
680
681 expect(inventoryLevelBefore).toEqual(
682 expect.objectContaining({ reserved_quantity: 2 })
683 )
684
685 await service.deleteReservationItems(reservationItem.id)
686
687 const inventoryLevelAfter =
688 await service.retrieveInventoryLevelByItemAndLocation(
689 inventoryItem.id,
690 "location-1"
691 )
692
693 expect(inventoryLevelAfter).toEqual(
694 expect.objectContaining({ reserved_quantity: 0 })
695 )
696 })
697 })
698
699 describe("deleteReservationItemsByLineItem", () => {
700 let inventoryItem: InventoryItemDTO
701
702 beforeEach(async () => {
703 inventoryItem = await service.createInventoryItems({
704 sku: "test-sku",
705 origin_country: "test-country",
706 })
707
708 await service.createInventoryLevels([
709 {
710 inventory_item_id: inventoryItem.id,
711 location_id: "location-1",
712 stocked_quantity: 2,
713 },
714 {
715 inventory_item_id: inventoryItem.id,
716 location_id: "location-2",
717 stocked_quantity: 2,
718 },
719 ])
720
721 await service.createReservationItems([
722 {
723 inventory_item_id: inventoryItem.id,
724 location_id: "location-1",
725 quantity: 2,
726 },
727 {
728 inventory_item_id: inventoryItem.id,
729 location_id: "location-1",
730 quantity: 2,
731 line_item_id: "line-item-id",
732 },
733 {
734 inventory_item_id: inventoryItem.id,
735 location_id: "location-1",
736 quantity: 2,
737 line_item_id: "line-item-id",
738 },
739 ])
740 })
741
742 it("should delete reseravation items by line item and restore them", async () => {
743 const reservationsPreDeleted = await service.listReservationItems({
744 line_item_id: "line-item-id",
745 })
746
747 expect(reservationsPreDeleted).toEqual([
748 expect.objectContaining({
749 location_id: "location-1",
750 quantity: 2,
751 line_item_id: "line-item-id",
752 }),
753 expect.objectContaining({
754 location_id: "location-1",
755 quantity: 2,
756 line_item_id: "line-item-id",
757 }),
758 ])
759
760 await service.deleteReservationItemsByLineItem("line-item-id")
761
762 const reservationsPostDeleted = await service.listReservationItems({
763 line_item_id: "line-item-id",
764 })
765
766 expect(reservationsPostDeleted).toEqual([])
767
768 await service.restoreReservationItemsByLineItem("line-item-id")
769
770 const reservationsPostRestored = await service.listReservationItems({
771 line_item_id: "line-item-id",
772 })
773
774 expect(reservationsPostRestored).toEqual([
775 expect.objectContaining({
776 location_id: "location-1",
777 quantity: 2,
778 line_item_id: "line-item-id",
779 }),
780 expect.objectContaining({
781 location_id: "location-1",
782 quantity: 2,
783 line_item_id: "line-item-id",
784 }),
785 ])
786 })
787
788 it("should adjust inventory levels accordingly when removing reservations by line item", async () => {
789 const inventoryLevelBefore =
790 await service.retrieveInventoryLevelByItemAndLocation(
791 inventoryItem.id,
792 "location-1"
793 )
794
795 expect(inventoryLevelBefore).toEqual(
796 expect.objectContaining({ reserved_quantity: 6 })
797 )
798
799 await service.deleteReservationItemsByLineItem("line-item-id")
800
801 const inventoryLevel =
802 await service.retrieveInventoryLevelByItemAndLocation(
803 inventoryItem.id,
804 "location-1"
805 )
806
807 expect(inventoryLevel).toEqual(
808 expect.objectContaining({ reserved_quantity: 2 })
809 )
810 })
811 })
812
813 describe("deleteReservationItemByLocationId", () => {
814 let inventoryItem: InventoryItemDTO
815 beforeEach(async () => {
816 inventoryItem = await service.createInventoryItems({
817 sku: "test-sku",
818 origin_country: "test-country",
819 })
820
821 await service.createInventoryLevels([
822 {
823 inventory_item_id: inventoryItem.id,
824 location_id: "location-1",
825 stocked_quantity: 2,
826 },
827 {
828 inventory_item_id: inventoryItem.id,
829 location_id: "location-2",
830 stocked_quantity: 2,
831 },
832 ])
833
834 await service.createReservationItems([
835 {
836 inventory_item_id: inventoryItem.id,
837 location_id: "location-1",
838 quantity: 2,
839 },
840 {
841 inventory_item_id: inventoryItem.id,
842 location_id: "location-1",
843 quantity: 2,
844 line_item_id: "line-item-id",
845 },
846 {
847 inventory_item_id: inventoryItem.id,
848 location_id: "location-2",
849 quantity: 2,
850 line_item_id: "line-item-id",
851 },
852 ])
853 })
854
855 it("deleted reservation items by line item", async () => {
856 const reservationsPreDeleted = await service.listReservationItems({
857 location_id: "location-1",
858 })
859
860 expect(reservationsPreDeleted).toEqual([
861 expect.objectContaining({
862 location_id: "location-1",
863 quantity: 2,
864 }),
865 expect.objectContaining({
866 location_id: "location-1",
867 quantity: 2,
868 }),
869 ])
870
871 await service.deleteReservationItemByLocationId("location-1")
872
873 const reservationsPostDeleted = await service.listReservationItems({
874 location_id: "location-1",
875 })
876
877 expect(reservationsPostDeleted).toEqual([])
878 })
879
880 it("should adjust inventory levels accordingly when removing reservations by line item", async () => {
881 const inventoryLevelPreDelete =
882 await service.retrieveInventoryLevelByItemAndLocation(
883 inventoryItem.id,
884 "location-1"
885 )
886
887 expect(inventoryLevelPreDelete).toEqual(
888 expect.objectContaining({ reserved_quantity: 4 })
889 )
890
891 await service.deleteReservationItemByLocationId("location-1")
892
893 const inventoryLevel =
894 await service.retrieveInventoryLevelByItemAndLocation(
895 inventoryItem.id,
896 "location-1"
897 )
898
899 expect(inventoryLevel).toEqual(
900 expect.objectContaining({ reserved_quantity: 0 })
901 )
902 })
903 })
904
905 describe("deleteInventoryItemLevelByLocationId", () => {
906 let inventoryItem: InventoryItemDTO
907
908 beforeEach(async () => {
909 inventoryItem = await service.createInventoryItems({
910 sku: "test-sku",
911 origin_country: "test-country",
912 })
913
914 await service.createInventoryLevels([
915 {
916 inventory_item_id: inventoryItem.id,
917 location_id: "location-1",
918 stocked_quantity: 2,
919 },
920 {
921 inventory_item_id: inventoryItem.id,
922 location_id: "location-2",
923 stocked_quantity: 2,
924 },
925 ])
926
927 await service.createReservationItems({
928 inventory_item_id: inventoryItem.id,
929 location_id: "location-1",
930 quantity: 1,
931 })
932 })
933
934 it("should remove inventory levels with given location id", async () => {
935 const inventoryLevelsPreDeletion = await service.listInventoryLevels(
936 {}
937 )
938
939 expect(inventoryLevelsPreDeletion).toEqual(
940 expect.arrayContaining([
941 expect.objectContaining({
942 stocked_quantity: 2,
943 location_id: "location-1",
944 reserved_quantity: 1,
945 }),
946 expect.objectContaining({
947 stocked_quantity: 2,
948 location_id: "location-2",
949 }),
950 ])
951 )
952
953 await service.deleteInventoryItemLevelByLocationId("location-1")
954
955 const inventoryLevelsPostDeletion = await service.listInventoryLevels(
956 {}
957 )
958
959 expect(inventoryLevelsPostDeletion).toEqual([
960 expect.objectContaining({
961 stocked_quantity: 2,
962 location_id: "location-2",
963 }),
964 ])
965 })
966 })
967
968 describe("deleteInventoryLevel", () => {
969 let inventoryItem: InventoryItemDTO
970 beforeEach(async () => {
971 inventoryItem = await service.createInventoryItems({
972 sku: "test-sku",
973 origin_country: "test-country",
974 })
975
976 await service.createInventoryLevels([
977 {
978 inventory_item_id: inventoryItem.id,
979 location_id: "location-1",
980 stocked_quantity: 2,
981 },
982 ])
983 })
984
985 it("should remove inventory levels with given location id", async () => {
986 const inventoryLevelsPreDeletion = await service.listInventoryLevels(
987 {}
988 )
989
990 expect(inventoryLevelsPreDeletion).toEqual([
991 expect.objectContaining({
992 stocked_quantity: 2,
993 location_id: "location-1",
994 }),
995 ])
996
997 await service.deleteInventoryLevel(inventoryItem.id, "location-1")
998
999 const inventoryLevelsPostDeletion = await service.listInventoryLevels(
1000 {}
1001 )
1002
1003 expect(inventoryLevelsPostDeletion).toEqual([])
1004 })
1005 })
1006
1007 describe("adjustInventory", () => {
1008 let inventoryItem: InventoryItemDTO
1009 beforeEach(async () => {
1010 inventoryItem = await service.createInventoryItems({
1011 sku: "test-sku",
1012 origin_country: "test-country",
1013 })
1014
1015 await service.createInventoryLevels([
1016 {
1017 inventory_item_id: inventoryItem.id,
1018 location_id: "location-1",
1019 stocked_quantity: 2,
1020 },
1021 ])
1022 })
1023
1024 it("should updated inventory level stocked_quantity by quantity", async () => {
1025 const updatedLevel = await service.adjustInventory(
1026 inventoryItem.id,
1027 "location-1",
1028 2
1029 )
1030 expect(updatedLevel.stocked_quantity).toEqual(4)
1031 })
1032
1033 it("should updated inventory level stocked_quantity by negative quantity", async () => {
1034 const updatedLevel = await service.adjustInventory(
1035 inventoryItem.id,
1036 "location-1",
1037 -1
1038 )
1039 expect(updatedLevel.stocked_quantity).toEqual(1!)
1040 })
1041 })
1042
1043 describe("retrieveInventoryLevelByItemAndLocation", () => {
1044 let inventoryItem: InventoryItemDTO
1045 beforeEach(async () => {
1046 inventoryItem = await service.createInventoryItems({
1047 sku: "test-sku",
1048 origin_country: "test-country",
1049 })
1050 const inventoryItem1 = await service.createInventoryItems({
1051 sku: "test-sku-1",
1052 origin_country: "test-country",
1053 })
1054
1055 await service.createInventoryLevels([
1056 {
1057 inventory_item_id: inventoryItem.id,
1058 location_id: "location-1",
1059 stocked_quantity: 2,
1060 },
1061 {
1062 inventory_item_id: inventoryItem.id,
1063 location_id: "location-2",
1064 stocked_quantity: 3,
1065 },
1066 {
1067 inventory_item_id: inventoryItem1.id,
1068 location_id: "location-1",
1069 stocked_quantity: 3,
1070 },
1071 ])
1072 })
1073
1074 it("should retrieve inventory level with provided location_id and inventory_item", async () => {
1075 const level = await service.retrieveInventoryLevelByItemAndLocation(
1076 inventoryItem.id,
1077 "location-1"
1078 )
1079 expect(level.stocked_quantity).toEqual(2)
1080 })
1081 })
1082
1083 describe("retrieveAvailableQuantity", () => {
1084 let inventoryItem: InventoryItemDTO
1085
1086 beforeEach(async () => {
1087 inventoryItem = await service.createInventoryItems({
1088 sku: "test-sku",
1089 origin_country: "test-country",
1090 })
1091 const inventoryItem1 = await service.createInventoryItems({
1092 sku: "test-sku-1",
1093 origin_country: "test-country",
1094 })
1095
1096 await service.createInventoryLevels([
1097 {
1098 inventory_item_id: inventoryItem.id,
1099 location_id: "location-1",
1100 stocked_quantity: 4,
1101 },
1102 {
1103 inventory_item_id: inventoryItem.id,
1104 location_id: "location-2",
1105 stocked_quantity: 4,
1106 },
1107 {
1108 inventory_item_id: inventoryItem1.id,
1109 location_id: "location-1",
1110 stocked_quantity: 3,
1111 },
1112 {
1113 inventory_item_id: inventoryItem.id,
1114 location_id: "location-3",
1115 stocked_quantity: 3,
1116 },
1117 ])
1118
1119 await service.createReservationItems({
1120 line_item_id: "test",
1121 inventory_item_id: inventoryItem.id,
1122 location_id: "location-2",
1123 quantity: 2,
1124 })
1125 })
1126
1127 it("should calculate current stocked quantity across locations", async () => {
1128 const level = await service.retrieveAvailableQuantity(
1129 inventoryItem.id,
1130 ["location-1", "location-2"]
1131 )
1132
1133 expect(level).toEqual(new BigNumber(6))
1134 })
1135 })
1136
1137 describe("retrieveStockedQuantity", () => {
1138 let inventoryItem: InventoryItemDTO
1139
1140 beforeEach(async () => {
1141 inventoryItem = await service.createInventoryItems({
1142 sku: "test-sku",
1143 origin_country: "test-country",
1144 })
1145
1146 const inventoryItem1 = await service.createInventoryItems({
1147 sku: "test-sku-1",
1148 origin_country: "test-country",
1149 })
1150
1151 await service.createInventoryLevels([
1152 {
1153 inventory_item_id: inventoryItem.id,
1154 location_id: "location-1",
1155 stocked_quantity: 4,
1156 },
1157 {
1158 inventory_item_id: inventoryItem.id,
1159 location_id: "location-2",
1160 stocked_quantity: 4,
1161 },
1162 {
1163 inventory_item_id: inventoryItem1.id,
1164 location_id: "location-1",
1165 stocked_quantity: 3,
1166 },
1167 {
1168 inventory_item_id: inventoryItem.id,
1169 location_id: "location-3",
1170 stocked_quantity: 3,
1171 },
1172 ])
1173 })
1174
1175 it("should retrieve stocked location", async () => {
1176 const stockedQuantity = await service.retrieveStockedQuantity(
1177 inventoryItem.id,
1178 ["location-1", "location-2"]
1179 )
1180
1181 expect(stockedQuantity.valueOf()).toEqual(8)
1182 })
1183 })
1184
1185 describe("retrieveReservedQuantity", () => {
1186 let inventoryItem: InventoryItemDTO
1187
1188 beforeEach(async () => {
1189 inventoryItem = await service.createInventoryItems({
1190 sku: "test-sku",
1191 origin_country: "test-country",
1192 })
1193
1194 const inventoryItem1 = await service.createInventoryItems({
1195 sku: "test-sku-1",
1196 origin_country: "test-country",
1197 })
1198
1199 await service.createInventoryLevels([
1200 {
1201 inventory_item_id: inventoryItem.id,
1202 location_id: "location-1",
1203 stocked_quantity: 4,
1204 },
1205 {
1206 inventory_item_id: inventoryItem.id,
1207 location_id: "location-2",
1208 stocked_quantity: 4,
1209 },
1210 {
1211 inventory_item_id: inventoryItem1.id,
1212 location_id: "location-1",
1213 stocked_quantity: 3,
1214 },
1215 {
1216 inventory_item_id: inventoryItem.id,
1217 location_id: "location-3",
1218 stocked_quantity: 3,
1219 },
1220 ])
1221
1222 await service.createReservationItems([
1223 {
1224 inventory_item_id: inventoryItem.id,
1225 location_id: "location-2",
1226 quantity: 2,
1227 },
1228 {
1229 inventory_item_id: inventoryItem1.id,
1230 location_id: "location-1",
1231 quantity: 2,
1232 },
1233 {
1234 inventory_item_id: inventoryItem.id,
1235 location_id: "location-3",
1236 quantity: 2,
1237 },
1238 ])
1239 })
1240
1241 it("retrieves reserved quantity", async () => {
1242 const reservedQuantity = (await service.retrieveReservedQuantity(
1243 inventoryItem.id,
1244 ["location-1", "location-2"]
1245 )) as any
1246
1247 expect(reservedQuantity + 0).toEqual(2)
1248 })
1249 })
1250
1251 describe("confirmInventory", () => {
1252 let inventoryItem: InventoryItemDTO
1253
1254 beforeEach(async () => {
1255 inventoryItem = await service.createInventoryItems({
1256 sku: "test-sku",
1257 origin_country: "test-country",
1258 })
1259
1260 await service.createInventoryLevels({
1261 inventory_item_id: inventoryItem.id,
1262 location_id: "location-1",
1263 stocked_quantity: 4,
1264 })
1265
1266 await service.createReservationItems({
1267 inventory_item_id: inventoryItem.id,
1268 location_id: "location-1",
1269 quantity: 2,
1270 })
1271 })
1272
1273 it("should return true if quantity is less than or equal to available quantity", async () => {
1274 const reservedQuantity = await service.confirmInventory(
1275 inventoryItem.id,
1276 ["location-1"],
1277 2
1278 )
1279
1280 expect(reservedQuantity).toBeTruthy()
1281 })
1282
1283 it("should return false if quantity is more than available quantity", async () => {
1284 const reservedQuantity = await service.confirmInventory(
1285 inventoryItem.id,
1286 ["location-1"],
1287 3
1288 )
1289
1290 expect(reservedQuantity).toBeFalsy()
1291 })
1292 })
1293 })
1294 },
1295 })