From 91208c8f2e5429d87ccd90e9e2518fda45e08398 Mon Sep 17 00:00:00 2001
From: "Carlos R. L. Rodrigues" <rodrigolr@gmail.com>
Date: Mon, 18 Nov 2024 09:33:18 -0300
Subject: [PATCH] fix(inventory): update reservation quantity

---
 .changeset/curvy-spies-design.md                      |  5 +++++
 .../__tests__/inventory-module-service.spec.ts        | 11 ++++++++++-
 .../inventory/src/services/inventory-module.ts        | 10 +++++++++-
 3 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 .changeset/curvy-spies-design.md

diff --git a/.changeset/curvy-spies-design.md b/.changeset/curvy-spies-design.md
new file mode 100644
index 0000000000000..a0818d3b29b2a
--- /dev/null
+++ b/.changeset/curvy-spies-design.md
@@ -0,0 +1,5 @@
+---
+"@medusajs/inventory": patch
+---
+
+Update reservation quantity
diff --git a/packages/modules/inventory/integration-tests/__tests__/inventory-module-service.spec.ts b/packages/modules/inventory/integration-tests/__tests__/inventory-module-service.spec.ts
index 5034380a39280..89eb399d1ef46 100644
--- a/packages/modules/inventory/integration-tests/__tests__/inventory-module-service.spec.ts
+++ b/packages/modules/inventory/integration-tests/__tests__/inventory-module-service.spec.ts
@@ -399,6 +399,15 @@ moduleIntegrationTestRunner<IInventoryService>({
           const updated = await service.updateReservationItems(update)
 
           expect(updated).toEqual(expect.objectContaining(update))
+
+          const update2 = {
+            id: reservationItem.id,
+            quantity: 10,
+          }
+
+          const updated2 = await service.updateReservationItems(update2)
+
+          expect(updated2).toEqual(expect.objectContaining(update2))
         })
 
         it("should adjust reserved_quantity of inventory level after updates increasing reserved quantity", async () => {
@@ -438,7 +447,7 @@ moduleIntegrationTestRunner<IInventoryService>({
         it("should throw error when increasing reserved quantity beyond availability", async () => {
           const update = {
             id: reservationItem.id,
-            quantity: 10,
+            quantity: 11,
           }
 
           const error = await service
diff --git a/packages/modules/inventory/src/services/inventory-module.ts b/packages/modules/inventory/src/services/inventory-module.ts
index 688b745ada724..e3ac99cf439f6 100644
--- a/packages/modules/inventory/src/services/inventory-module.ts
+++ b/packages/modules/inventory/src/services/inventory-module.ts
@@ -746,10 +746,18 @@ export default class InventoryModuleService
     const availabilityData = input.map((data) => {
       const reservation = reservationMap.get(data.id)!
 
+      let adjustment = data.quantity
+        ? MathBN.sub(data.quantity, reservation.quantity)
+        : 0
+
+      if (MathBN.lt(adjustment, 0)) {
+        adjustment = 0
+      }
+
       return {
         inventory_item_id: reservation.inventory_item_id,
         location_id: data.location_id ?? reservation.location_id,
-        quantity: data.quantity ?? reservation.quantity,
+        quantity: adjustment,
         allow_backorder:
           data.allow_backorder || reservation.allow_backorder || false,
       }