feat(core-flows): order add quantity diff (#10065)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/core-flows": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Order edit quantity diff
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
Modules,
|
Modules,
|
||||||
OrderChangeStatus,
|
OrderChangeStatus,
|
||||||
RuleOperator,
|
RuleOperator,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
|
||||||
import {
|
import {
|
||||||
adminHeaders,
|
adminHeaders,
|
||||||
createAdminUser,
|
createAdminUser,
|
||||||
@@ -418,6 +418,31 @@ medusaIntegrationTestRunner({
|
|||||||
expect(result.summary.current_order_total).toEqual(124)
|
expect(result.summary.current_order_total).toEqual(124)
|
||||||
expect(result.summary.original_order_total).toEqual(60)
|
expect(result.summary.original_order_total).toEqual(60)
|
||||||
|
|
||||||
|
const updatedItem = result.items.find((i) => i.id === item.id)
|
||||||
|
expect(updatedItem.actions).toEqual([
|
||||||
|
expect.objectContaining({
|
||||||
|
details: expect.objectContaining({
|
||||||
|
quantity: 2,
|
||||||
|
unit_price: 25,
|
||||||
|
quantity_diff: 0,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
details: expect.objectContaining({
|
||||||
|
quantity: 3,
|
||||||
|
unit_price: 25,
|
||||||
|
quantity_diff: 1,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
expect.objectContaining({
|
||||||
|
details: expect.objectContaining({
|
||||||
|
quantity: 3,
|
||||||
|
unit_price: 30,
|
||||||
|
quantity_diff: 1,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
])
|
||||||
|
|
||||||
// Remove the item by setting the quantity to 0
|
// Remove the item by setting the quantity to 0
|
||||||
result = (
|
result = (
|
||||||
await api.post(
|
await api.post(
|
||||||
|
|||||||
+30
-14
@@ -4,7 +4,12 @@ import {
|
|||||||
OrderPreviewDTO,
|
OrderPreviewDTO,
|
||||||
OrderWorkflow,
|
OrderWorkflow,
|
||||||
} from "@medusajs/framework/types"
|
} from "@medusajs/framework/types"
|
||||||
import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils"
|
import {
|
||||||
|
BigNumber,
|
||||||
|
ChangeActionType,
|
||||||
|
MathBN,
|
||||||
|
OrderChangeStatus,
|
||||||
|
} from "@medusajs/framework/utils"
|
||||||
import {
|
import {
|
||||||
WorkflowData,
|
WorkflowData,
|
||||||
WorkflowResponse,
|
WorkflowResponse,
|
||||||
@@ -75,19 +80,30 @@ export const orderEditUpdateItemQuantityWorkflow = createWorkflow(
|
|||||||
const orderChangeActionInput = transform(
|
const orderChangeActionInput = transform(
|
||||||
{ order, orderChange, items: input.items },
|
{ order, orderChange, items: input.items },
|
||||||
({ order, orderChange, items }) => {
|
({ order, orderChange, items }) => {
|
||||||
return items.map((item) => ({
|
return items.map((item) => {
|
||||||
order_change_id: orderChange.id,
|
const existing = order?.items?.find(
|
||||||
order_id: order.id,
|
(exItem) => exItem.id === item.id
|
||||||
version: orderChange.version,
|
)!
|
||||||
action: ChangeActionType.ITEM_UPDATE,
|
|
||||||
internal_note: item.internal_note,
|
const quantityDiff = new BigNumber(
|
||||||
details: {
|
MathBN.sub(item.quantity, existing.quantity)
|
||||||
reference_id: item.id,
|
)
|
||||||
quantity: item.quantity,
|
|
||||||
unit_price: item.unit_price,
|
return {
|
||||||
compare_at_unit_price: item.compare_at_unit_price,
|
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,
|
||||||
|
unit_price: item.unit_price,
|
||||||
|
compare_at_unit_price: item.compare_at_unit_price,
|
||||||
|
quantity_diff: quantityDiff,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user