Skip to content

Commit

Permalink
fix(core-flows, dashboard): inventory kit reservations (medusajs#9502)
Browse files Browse the repository at this point in the history
**What**
- fix `prepareConfirmInventory` to account for inventory kit items
  - _note: this step is reused in the complete cart and all RMA flows_ 
- properly remove reservations for items that are removed from the order edit
- invalidate inventory/reservations cache when order edit is confirmed

---

https://github.com/user-attachments/assets/f12e9198-0718-4c08-bd81-efc536eca146

---

FIXES CC-565
  • Loading branch information
fPolic authored Oct 9, 2024
1 parent 7f1f075 commit 1b9379b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
15 changes: 15 additions & 0 deletions packages/admin/dashboard/src/hooks/api/order-edits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { sdk } from "../../lib/client"
import { queryClient } from "../../lib/query-client"
import { ordersQueryKeys } from "./orders"
import { FetchError } from "@medusajs/js-sdk"
import { reservationItemsQueryKeys } from "./reservations"
import { inventoryItemsQueryKeys } from "./inventory.tsx"

export const useCreateOrderEdit = (
orderId: string,
Expand Down Expand Up @@ -82,6 +84,19 @@ export const useConfirmOrderEdit = (
queryClient.invalidateQueries({
queryKey: ordersQueryKeys.changes(id),
})

queryClient.invalidateQueries({
queryKey: reservationItemsQueryKeys.lists(),
})

queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.lists(),
})

queryClient.invalidateQueries({
queryKey: inventoryItemsQueryKeys.details(),
})

options?.onSuccess?.(data, variables, context)
},
...options,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core-flows/src/cart/steps/confirm-inventory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IInventoryService } from "@medusajs/framework/types"
import { BigNumberInput, IInventoryService } from "@medusajs/framework/types"
import {
MathBN,
MedusaError,
Expand All @@ -12,7 +12,7 @@ export interface ConfirmVariantInventoryStepInput {
inventory_item_id: string
required_quantity: number
allow_backorder: boolean
quantity: number
quantity: BigNumberInput
location_ids: string[]
}[]
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/core-flows/src/cart/steps/reserve-inventory.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { IInventoryService } from "@medusajs/framework/types"
import { MathBN, Modules } from "@medusajs/framework/utils"
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
import { BigNumberInput } from "@medusajs/types"

export interface ReserveVariantInventoryStepInput {
items: {
id?: string
inventory_item_id: string
required_quantity: number
allow_backorder: boolean
quantity: number
quantity: BigNumberInput
location_ids: string[]
}[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface ConfirmInventoryItem {
inventory_item_id: string
required_quantity: number
allow_backorder: boolean
quantity: number
quantity: BigNumberInput
location_ids: string[]
}

Expand Down Expand Up @@ -143,25 +143,27 @@ const formatInventoryInput = ({
return
}

const variantInventoryItem = product_variant_inventory_items.find(
const variantInventoryItems = product_variant_inventory_items.filter(
(i) => i.variant_id === item.variant_id
)

if (!variantInventoryItem) {
if (!variantInventoryItems.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Variant ${item.variant_id} does not have any inventory items associated with it.`
)
}

itemsToConfirm.push({
id: item.id,
inventory_item_id: variantInventoryItem.inventory_item_id,
required_quantity: variantInventoryItem.required_quantity,
allow_backorder: !!variant.allow_backorder,
quantity: item.quantity as number, // TODO: update type to BigNumberInput
location_ids: location_ids,
})
variantInventoryItems.forEach((variantInventoryItem) =>
itemsToConfirm.push({
id: item.id,
inventory_item_id: variantInventoryItem.inventory_item_id,
required_quantity: variantInventoryItem.required_quantity,
allow_backorder: !!variant.allow_backorder,
quantity: item.quantity,
location_ids: location_ids,
})
)
})

return itemsToConfirm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createWorkflow,
transform,
} from "@medusajs/framework/workflows-sdk"
import { BigNumberInput } from "@medusajs/types"
import { confirmInventoryStep } from "../steps"
import { prepareConfirmInventoryInput } from "../utils/prepare-confirm-inventory-input"

Expand All @@ -14,7 +15,7 @@ export interface ConfirmVariantInventoryWorkflowOutput {
inventory_item_id: string
required_quantity: number
allow_backorder: boolean
quantity: number
quantity: BigNumberInput
location_ids: string[]
}[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,16 @@ export const confirmOrderEditRequestWorkflow = createWorkflow(
throw_if_key_not_found: true,
}).config({ name: "order-items-query" })

const lineItemIds = transform({ orderItems }, (data) =>
data.orderItems.items.map(({ id }) => id)
const lineItemIds = transform(
{ orderItems, previousOrderItems: order.items },

(data) => {
const previousItemIds = (data.previousOrderItems || []).map(
({ id }) => id
) // items that have been removed with the change
const newItemIds = data.orderItems.items.map(({ id }) => id)
return [...new Set([...previousItemIds, newItemIds])]
}
)

deleteReservationsByLineItemsStep(lineItemIds)
Expand Down

0 comments on commit 1b9379b

Please sign in to comment.