From 0b6bcdd538de366c4e45b4186b9f278ed538f8b5 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Wed, 7 Aug 2024 15:15:17 -0300 Subject: [PATCH] chore(core-flows): util to format inventory input (#8483) --- .../utils/prepare-confirm-inventory-input.ts | 95 ++++++++++++++++++- .../cart/workflows/complete-cart.ts | 18 ++-- .../workflows/confirm-variant-inventory.ts | 95 +------------------ .../workflows/claim/confirm-claim-request.ts | 18 ++-- .../exchange/confirm-exchange-request.ts | 18 ++-- packages/core/types/src/cart/workflows.ts | 1 - 6 files changed, 127 insertions(+), 118 deletions(-) diff --git a/packages/core/core-flows/src/definition/cart/utils/prepare-confirm-inventory-input.ts b/packages/core/core-flows/src/definition/cart/utils/prepare-confirm-inventory-input.ts index 0a9d885aab..541c6a6195 100644 --- a/packages/core/core-flows/src/definition/cart/utils/prepare-confirm-inventory-input.ts +++ b/packages/core/core-flows/src/definition/cart/utils/prepare-confirm-inventory-input.ts @@ -1,5 +1,8 @@ -import { BigNumberInput } from "@medusajs/types" -import { MedusaError } from "@medusajs/utils" +import { + BigNumberInput, + ConfirmVariantInventoryWorkflowInputDTO, +} from "@medusajs/types" +import { MedusaError, deepFlatMap } from "@medusajs/utils" interface ConfirmInventoryPreparationInput { product_variant_inventory_items: { @@ -29,7 +32,93 @@ interface ConfirmInventoryItem { location_ids: string[] } -export const prepareConfirmInventoryInput = ({ +export const prepareConfirmInventoryInput = (data: { + input: ConfirmVariantInventoryWorkflowInputDTO +}) => { + const productVariantInventoryItems = new Map() + const stockLocationIds = new Set() + const allVariants = new Map() + let hasSalesChannelStockLocation = false + let hasManagedInventory = false + + const salesChannelId = data.input.sales_channel_id + + for (const updateItem of data.input.itemsToUpdate ?? []) { + const item = data.input.items.find( + (item) => item.variant_id === updateItem.data.variant_id + ) + if (item && updateItem.data.quantity) { + item.quantity = updateItem.data.quantity! + } + } + + deepFlatMap( + data.input, + "variants.inventory_items.inventory.location_levels.stock_locations.sales_channels", + ({ variants, inventory_items, stock_locations, sales_channels }) => { + if (!variants) { + return + } + + if ( + !hasSalesChannelStockLocation && + sales_channels?.id === salesChannelId + ) { + hasSalesChannelStockLocation = true + } + + if (stock_locations) { + stockLocationIds.add(stock_locations.id) + } + + if (inventory_items) { + const inventoryItemId = inventory_items.inventory_item_id + if (!productVariantInventoryItems.has(inventoryItemId)) { + productVariantInventoryItems.set(inventoryItemId, { + variant_id: inventory_items.variant_id, + inventory_item_id: inventoryItemId, + required_quantity: inventory_items.required_quantity, + }) + } + } + + if (!allVariants.has(variants.id)) { + if (!hasManagedInventory && variants.manage_inventory) { + hasManagedInventory = true + } + + allVariants.set(variants.id, { + id: variants.id, + manage_inventory: variants.manage_inventory, + }) + } + } + ) + + if (!hasManagedInventory) { + return { items: [] } + } + + if (salesChannelId && !hasSalesChannelStockLocation) { + throw new MedusaError( + MedusaError.Types.INVALID_DATA, + `Sales channel ${salesChannelId} is not associated with any stock location.` + ) + } + + const items = formatInventoryInput({ + product_variant_inventory_items: Array.from( + productVariantInventoryItems.values() + ), + location_ids: Array.from(stockLocationIds), + items: data.input.items, + variants: Array.from(allVariants.values()), + }) + + return { items } +} + +const formatInventoryInput = ({ product_variant_inventory_items, location_ids, items, diff --git a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts index 92844118bd..7f4c283a87 100644 --- a/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts +++ b/packages/core/core-flows/src/definition/cart/workflows/complete-cart.ts @@ -17,12 +17,12 @@ import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-pa import { validateCartPaymentsStep } from "../steps" import { reserveInventoryStep } from "../steps/reserve-inventory" import { completeCartFields } from "../utils/fields" +import { prepareConfirmInventoryInput } from "../utils/prepare-confirm-inventory-input" import { prepareAdjustmentsData, prepareLineItemData, prepareTaxLinesData, } from "../utils/prepare-line-item-data" -import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory" export const completeCartWorkflowId = "complete-cart" export const completeCartWorkflow = createWorkflow( @@ -66,14 +66,16 @@ export const completeCartWorkflow = createWorkflow( } ) - const formatedInventoryItems = confirmVariantInventoryWorkflow.runAsStep({ - input: { - skipInventoryCheck: true, - sales_channel_id, - variants, - items, + const formatedInventoryItems = transform( + { + input: { + sales_channel_id, + variants, + items, + }, }, - }) + prepareConfirmInventoryInput + ) const [, finalCart] = parallelize( reserveInventoryStep(formatedInventoryItems), diff --git a/packages/core/core-flows/src/definition/cart/workflows/confirm-variant-inventory.ts b/packages/core/core-flows/src/definition/cart/workflows/confirm-variant-inventory.ts index 26680ff9eb..afd82cb59f 100644 --- a/packages/core/core-flows/src/definition/cart/workflows/confirm-variant-inventory.ts +++ b/packages/core/core-flows/src/definition/cart/workflows/confirm-variant-inventory.ts @@ -1,11 +1,9 @@ import { ConfirmVariantInventoryWorkflowInputDTO } from "@medusajs/types" -import { MedusaError, deepFlatMap } from "@medusajs/utils" import { WorkflowData, WorkflowResponse, createWorkflow, transform, - when, } from "@medusajs/workflows-sdk" import { confirmInventoryStep } from "../steps" import { prepareConfirmInventoryInput } from "../utils/prepare-confirm-inventory-input" @@ -27,95 +25,12 @@ export const confirmVariantInventoryWorkflow = createWorkflow( ( input: WorkflowData ): WorkflowResponse => { - const confirmInventoryInput = transform({ input }, (data) => { - const productVariantInventoryItems = new Map() - const stockLocationIds = new Set() - const allVariants = new Map() - let hasSalesChannelStockLocation = false - let hasManagedInventory = false + const confirmInventoryInput = transform( + { input }, + prepareConfirmInventoryInput + ) - const salesChannelId = data.input.sales_channel_id - - for (const updateItem of data.input.itemsToUpdate ?? []) { - const item = data.input.items.find( - (item) => item.variant_id === updateItem.data.variant_id - ) - if (item && updateItem.data.quantity) { - item.quantity = updateItem.data.quantity! - } - } - - deepFlatMap( - data.input, - "variants.inventory_items.inventory.location_levels.stock_locations.sales_channels", - ({ variants, inventory_items, stock_locations, sales_channels }) => { - if (!variants) { - return - } - - if ( - !hasSalesChannelStockLocation && - sales_channels?.id === salesChannelId - ) { - hasSalesChannelStockLocation = true - } - - if (stock_locations) { - stockLocationIds.add(stock_locations.id) - } - - if (inventory_items) { - const inventoryItemId = inventory_items.inventory_item_id - if (!productVariantInventoryItems.has(inventoryItemId)) { - productVariantInventoryItems.set(inventoryItemId, { - variant_id: inventory_items.variant_id, - inventory_item_id: inventoryItemId, - required_quantity: inventory_items.required_quantity, - }) - } - } - - if (!allVariants.has(variants.id)) { - if (!hasManagedInventory && variants.manage_inventory) { - hasManagedInventory = true - } - - allVariants.set(variants.id, { - id: variants.id, - manage_inventory: variants.manage_inventory, - }) - } - } - ) - - if (!hasManagedInventory) { - return { items: [] } - } - - if (salesChannelId && !hasSalesChannelStockLocation) { - throw new MedusaError( - MedusaError.Types.INVALID_DATA, - `Sales channel ${salesChannelId} is not associated with any stock location.` - ) - } - - const items = prepareConfirmInventoryInput({ - product_variant_inventory_items: Array.from( - productVariantInventoryItems.values() - ), - location_ids: Array.from(stockLocationIds), - items: data.input.items, - variants: Array.from(allVariants.values()), - }) - - return { items } - }) - - when({ input }, ({ input }) => { - return !input.skipInventoryCheck - }).then(() => { - confirmInventoryStep(confirmInventoryInput) - }) + confirmInventoryStep(confirmInventoryInput) return new WorkflowResponse(confirmInventoryInput) } diff --git a/packages/core/core-flows/src/order/workflows/claim/confirm-claim-request.ts b/packages/core/core-flows/src/order/workflows/claim/confirm-claim-request.ts index 50296d2677..70c14820fc 100644 --- a/packages/core/core-flows/src/order/workflows/claim/confirm-claim-request.ts +++ b/packages/core/core-flows/src/order/workflows/claim/confirm-claim-request.ts @@ -21,7 +21,7 @@ import { } from "@medusajs/workflows-sdk" import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common" import { reserveInventoryStep } from "../../../definition/cart/steps/reserve-inventory" -import { confirmVariantInventoryWorkflow } from "../../../definition/cart/workflows/confirm-variant-inventory" +import { prepareConfirmInventoryInput } from "../../../definition/cart/utils/prepare-confirm-inventory-input" import { createReturnFulfillmentWorkflow } from "../../../fulfillment/workflows/create-return-fulfillment" import { previewOrderChangeStep, updateReturnsStep } from "../../steps" import { createOrderClaimItemsFromActionsStep } from "../../steps/claim/create-claim-items-from-actions" @@ -310,14 +310,16 @@ export const confirmClaimRequestWorkflow = createWorkflow( } }) - const formatedInventoryItems = confirmVariantInventoryWorkflow.runAsStep({ - input: { - skipInventoryCheck: true, - sales_channel_id: (claim as any).order.sales_channel_id, - variants, - items, + const formatedInventoryItems = transform( + { + input: { + sales_channel_id: (claim as any).order.sales_channel_id, + variants, + items, + }, }, - }) + prepareConfirmInventoryInput + ) reserveInventoryStep(formatedInventoryItems) }) diff --git a/packages/core/core-flows/src/order/workflows/exchange/confirm-exchange-request.ts b/packages/core/core-flows/src/order/workflows/exchange/confirm-exchange-request.ts index fe74e3f7a7..42b6d2f37e 100644 --- a/packages/core/core-flows/src/order/workflows/exchange/confirm-exchange-request.ts +++ b/packages/core/core-flows/src/order/workflows/exchange/confirm-exchange-request.ts @@ -16,7 +16,7 @@ import { } from "@medusajs/workflows-sdk" import { createRemoteLinkStep, useRemoteQueryStep } from "../../../common" import { reserveInventoryStep } from "../../../definition/cart/steps/reserve-inventory" -import { confirmVariantInventoryWorkflow } from "../../../definition/cart/workflows/confirm-variant-inventory" +import { prepareConfirmInventoryInput } from "../../../definition/cart/utils/prepare-confirm-inventory-input" import { createReturnFulfillmentWorkflow } from "../../../fulfillment/workflows/create-return-fulfillment" import { previewOrderChangeStep } from "../../steps" import { confirmOrderChanges } from "../../steps/confirm-order-changes" @@ -297,14 +297,16 @@ export const confirmExchangeRequestWorkflow = createWorkflow( } }) - const formatedInventoryItems = confirmVariantInventoryWorkflow.runAsStep({ - input: { - skipInventoryCheck: true, - sales_channel_id: (exchange as any).order.sales_channel_id, - variants, - items, + const formatedInventoryItems = transform( + { + input: { + sales_channel_id: (exchange as any).order.sales_channel_id, + variants, + items, + }, }, - }) + prepareConfirmInventoryInput + ) reserveInventoryStep(formatedInventoryItems) }) diff --git a/packages/core/types/src/cart/workflows.ts b/packages/core/types/src/cart/workflows.ts index 0fcbb17f65..0f6f496aa4 100644 --- a/packages/core/types/src/cart/workflows.ts +++ b/packages/core/types/src/cart/workflows.ts @@ -122,7 +122,6 @@ export interface CompleteCartWorkflowInputDTO { } export interface ConfirmVariantInventoryWorkflowInputDTO { - skipInventoryCheck?: boolean sales_channel_id: string variants: { id: string