chore(core-flows): order update item quantity (#8659)
This commit is contained in:
committed by
GitHub
parent
dd82a56ec5
commit
1be9373290
@@ -45,9 +45,11 @@ export * from "./order-edit/cancel-begin-order-edit"
|
||||
export * from "./order-edit/confirm-order-edit-request"
|
||||
export * from "./order-edit/create-order-edit-shipping-method"
|
||||
export * from "./order-edit/order-edit-add-new-item"
|
||||
export * from "./order-edit/order-edit-update-item-quantity"
|
||||
export * from "./order-edit/remove-order-edit-item-action"
|
||||
export * from "./order-edit/remove-order-edit-shipping-method"
|
||||
export * from "./order-edit/update-order-edit-add-item"
|
||||
export * from "./order-edit/update-order-edit-item-quantity"
|
||||
export * from "./order-edit/update-order-edit-shipping-method"
|
||||
export * from "./return/begin-receive-return"
|
||||
export * from "./return/begin-return"
|
||||
|
||||
@@ -102,51 +102,51 @@ export const confirmOrderEditRequestWorkflow = createWorkflow(
|
||||
"version",
|
||||
"canceled_at",
|
||||
"sales_channel_id",
|
||||
"items.quantity",
|
||||
"items.raw_quantity",
|
||||
"items.item.id",
|
||||
"items.item.variant.manage_inventory",
|
||||
"items.item.variant.allow_backorder",
|
||||
"items.item.variant.inventory_items.inventory_item_id",
|
||||
"items.item.variant.inventory_items.required_quantity",
|
||||
"items.item.variant.inventory_items.inventory.location_levels.stock_locations.id",
|
||||
"items.item.variant.inventory_items.inventory.location_levels.stock_locations.name",
|
||||
"items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
|
||||
"items.item.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
|
||||
"items.*",
|
||||
"items.variant.manage_inventory",
|
||||
"items.variant.allow_backorder",
|
||||
"items.variant.inventory_items.inventory_item_id",
|
||||
"items.variant.inventory_items.required_quantity",
|
||||
"items.variant.inventory_items.inventory.location_levels.stock_locations.id",
|
||||
"items.variant.inventory_items.inventory.location_levels.stock_locations.name",
|
||||
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
|
||||
"items.variant.inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
|
||||
],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "order-items-query" })
|
||||
|
||||
const { variants, items } = transform({ orderItems }, ({ orderItems }) => {
|
||||
const allItems: any[] = []
|
||||
const allVariants: any[] = []
|
||||
orderItems.items.forEach((ordItem) => {
|
||||
const itemAction = orderPreview.items?.find(
|
||||
(item) =>
|
||||
item.id === ordItem.id &&
|
||||
item.actions?.find((a) => a.action === ChangeActionType.ITEM_ADD)
|
||||
)
|
||||
const { variants, items } = transform(
|
||||
{ orderItems, orderPreview },
|
||||
({ orderItems, orderPreview }) => {
|
||||
const allItems: any[] = []
|
||||
const allVariants: any[] = []
|
||||
orderItems.items.forEach((ordItem) => {
|
||||
const itemAction = orderPreview.items?.find(
|
||||
(item) =>
|
||||
item.id === ordItem.id &&
|
||||
item.actions?.find((a) => a.action === ChangeActionType.ITEM_ADD)
|
||||
)
|
||||
|
||||
if (!itemAction) {
|
||||
return
|
||||
}
|
||||
if (!itemAction) {
|
||||
return
|
||||
}
|
||||
|
||||
const item = ordItem.item
|
||||
allItems.push({
|
||||
id: item.id,
|
||||
variant_id: item.variant_id,
|
||||
quantity: itemAction.raw_quantity ?? itemAction.quantity,
|
||||
allItems.push({
|
||||
id: ordItem.id,
|
||||
variant_id: ordItem.variant_id,
|
||||
quantity: itemAction.raw_quantity ?? itemAction.quantity,
|
||||
})
|
||||
allVariants.push(ordItem.variant)
|
||||
})
|
||||
allVariants.push(item.variant)
|
||||
})
|
||||
|
||||
return {
|
||||
variants: allVariants,
|
||||
items: allItems,
|
||||
return {
|
||||
variants: allVariants,
|
||||
items: allItems,
|
||||
}
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
const formatedInventoryItems = transform(
|
||||
{
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import {
|
||||
OrderChangeDTO,
|
||||
OrderDTO,
|
||||
OrderPreviewDTO,
|
||||
OrderWorkflow,
|
||||
} from "@medusajs/types"
|
||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common"
|
||||
import { previewOrderChangeStep } from "../../steps/preview-order-change"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
throwIfOrderChangeIsNotActive,
|
||||
} from "../../utils/order-validation"
|
||||
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
|
||||
|
||||
/**
|
||||
* This step validates that item quantity updated can be added to an order edit.
|
||||
*/
|
||||
export const orderEditUpdateItemQuantityValidationStep = createStep(
|
||||
"order-edit-update-item-quantity-validation",
|
||||
async function ({
|
||||
order,
|
||||
orderChange,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
orderChange: OrderChangeDTO
|
||||
}) {
|
||||
throwIfIsCancelled(order, "Order")
|
||||
throwIfOrderChangeIsNotActive({ orderChange })
|
||||
}
|
||||
)
|
||||
|
||||
export const orderEditUpdateItemQuantityWorkflowId =
|
||||
"order-edit-update-item-quantity"
|
||||
/**
|
||||
* This workflow update item's quantity of an order.
|
||||
*/
|
||||
export const orderEditUpdateItemQuantityWorkflow = createWorkflow(
|
||||
orderEditUpdateItemQuantityWorkflowId,
|
||||
function (
|
||||
input: WorkflowData<OrderWorkflow.OrderEditUpdateItemQuantityWorkflowInput>
|
||||
): WorkflowResponse<OrderPreviewDTO> {
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: ["id", "status", "canceled_at", "items.*"],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "order-query" })
|
||||
|
||||
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
||||
entry_point: "order_change",
|
||||
fields: ["id", "status"],
|
||||
variables: {
|
||||
filters: {
|
||||
order_id: input.order_id,
|
||||
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
|
||||
},
|
||||
},
|
||||
list: false,
|
||||
}).config({ name: "order-change-query" })
|
||||
|
||||
orderEditUpdateItemQuantityValidationStep({
|
||||
order,
|
||||
orderChange,
|
||||
})
|
||||
|
||||
const orderChangeActionInput = transform(
|
||||
{ order, orderChange, items: input.items },
|
||||
({ order, orderChange, items }) => {
|
||||
return items.map((item) => ({
|
||||
order_change_id: orderChange.id,
|
||||
order_id: order.id,
|
||||
version: orderChange.version,
|
||||
action: ChangeActionType.ITEM_UPDATE,
|
||||
internal_note: item.internal_note,
|
||||
details: {
|
||||
reference_id: item.id,
|
||||
quantity: item.quantity,
|
||||
},
|
||||
}))
|
||||
}
|
||||
)
|
||||
|
||||
createOrderChangeActionsWorkflow.runAsStep({
|
||||
input: orderChangeActionInput,
|
||||
})
|
||||
|
||||
return new WorkflowResponse(previewOrderChangeStep(input.order_id))
|
||||
}
|
||||
)
|
||||
@@ -24,7 +24,7 @@ import {
|
||||
} from "../../utils/order-validation"
|
||||
|
||||
/**
|
||||
* This step validates that a new item can be removed from an order edit.
|
||||
* This step validates that a new item can be updated from an order edit.
|
||||
*/
|
||||
export const updateOrderEditAddItemValidationStep = createStep(
|
||||
"update-order-edit-add-item-validation",
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
import {
|
||||
OrderChangeActionDTO,
|
||||
OrderChangeDTO,
|
||||
OrderDTO,
|
||||
OrderPreviewDTO,
|
||||
OrderWorkflow,
|
||||
} from "@medusajs/types"
|
||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/utils"
|
||||
import {
|
||||
WorkflowData,
|
||||
WorkflowResponse,
|
||||
createStep,
|
||||
createWorkflow,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import { useRemoteQueryStep } from "../../../common"
|
||||
import {
|
||||
previewOrderChangeStep,
|
||||
updateOrderChangeActionsStep,
|
||||
} from "../../steps"
|
||||
import {
|
||||
throwIfIsCancelled,
|
||||
throwIfOrderChangeIsNotActive,
|
||||
} from "../../utils/order-validation"
|
||||
|
||||
/**
|
||||
* This step validates that an item can be updated from an order edit.
|
||||
*/
|
||||
export const updateOrderEditItemQuantityValidationStep = createStep(
|
||||
"update-order-edit-update-quantity-validation",
|
||||
async function (
|
||||
{
|
||||
order,
|
||||
orderChange,
|
||||
input,
|
||||
}: {
|
||||
order: OrderDTO
|
||||
orderChange: OrderChangeDTO
|
||||
input: OrderWorkflow.UpdateOrderEditItemQuantityWorkflowInput
|
||||
},
|
||||
context
|
||||
) {
|
||||
throwIfIsCancelled(order, "Order")
|
||||
throwIfOrderChangeIsNotActive({ orderChange })
|
||||
|
||||
const associatedAction = (orderChange.actions ?? []).find(
|
||||
(a) => a.id === input.action_id
|
||||
) as OrderChangeActionDTO
|
||||
|
||||
if (!associatedAction) {
|
||||
throw new Error(
|
||||
`No request to update item quantity for order ${input.order_id} in order change ${orderChange.id}`
|
||||
)
|
||||
} else if (associatedAction.action !== ChangeActionType.ITEM_UPDATE) {
|
||||
throw new Error(`Action ${associatedAction.id} is not updating an item`)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const updateOrderEditItemQuantityWorkflowId =
|
||||
"update-order-edit-update-quantity"
|
||||
/**
|
||||
* This workflow updates a new item in the order edit.
|
||||
*/
|
||||
export const updateOrderEditItemQuantityWorkflow = createWorkflow(
|
||||
updateOrderEditItemQuantityWorkflowId,
|
||||
function (
|
||||
input: WorkflowData<OrderWorkflow.UpdateOrderEditItemQuantityWorkflowInput>
|
||||
): WorkflowResponse<OrderPreviewDTO> {
|
||||
const order: OrderDTO = useRemoteQueryStep({
|
||||
entry_point: "orders",
|
||||
fields: ["id", "status", "canceled_at", "items.*"],
|
||||
variables: { id: input.order_id },
|
||||
list: false,
|
||||
throw_if_key_not_found: true,
|
||||
}).config({ name: "order-query" })
|
||||
|
||||
const orderChange: OrderChangeDTO = useRemoteQueryStep({
|
||||
entry_point: "order_change",
|
||||
fields: ["id", "status", "version", "actions.*"],
|
||||
variables: {
|
||||
filters: {
|
||||
order_id: input.order_id,
|
||||
status: [OrderChangeStatus.PENDING, OrderChangeStatus.REQUESTED],
|
||||
},
|
||||
},
|
||||
list: false,
|
||||
}).config({ name: "order-change-query" })
|
||||
|
||||
updateOrderEditItemQuantityValidationStep({
|
||||
order,
|
||||
input,
|
||||
orderChange,
|
||||
})
|
||||
|
||||
const updateData = transform(
|
||||
{ orderChange, input },
|
||||
({ input, orderChange }) => {
|
||||
const originalAction = (orderChange.actions ?? []).find(
|
||||
(a) => a.id === input.action_id
|
||||
) as OrderChangeActionDTO
|
||||
|
||||
const data = input.data
|
||||
return {
|
||||
id: input.action_id,
|
||||
details: {
|
||||
quantity: data.quantity ?? originalAction.details?.quantity,
|
||||
},
|
||||
internal_note: data.internal_note,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
updateOrderChangeActionsStep([updateData])
|
||||
|
||||
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user