From 05898e8ef778aaaebd90a3119231e2c93d837752 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" Date: Tue, 12 Nov 2024 13:47:30 -0300 Subject: [PATCH] feat(core-flows): order add quantity diff --- .changeset/honest-chefs-attend.md | 5 +++ .../__tests__/order-edits/order-edits.spec.ts | 27 +++++++++++- .../order-edit-update-item-quantity.ts | 44 +++++++++++++------ 3 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 .changeset/honest-chefs-attend.md diff --git a/.changeset/honest-chefs-attend.md b/.changeset/honest-chefs-attend.md new file mode 100644 index 0000000000000..ee5c391feb7bd --- /dev/null +++ b/.changeset/honest-chefs-attend.md @@ -0,0 +1,5 @@ +--- +"@medusajs/core-flows": patch +--- + +Order edit quantity diff diff --git a/integration-tests/http/__tests__/order-edits/order-edits.spec.ts b/integration-tests/http/__tests__/order-edits/order-edits.spec.ts index 9790780b1215c..80f4fa4d7bd6e 100644 --- a/integration-tests/http/__tests__/order-edits/order-edits.spec.ts +++ b/integration-tests/http/__tests__/order-edits/order-edits.spec.ts @@ -1,10 +1,10 @@ +import { medusaIntegrationTestRunner } from "@medusajs/test-utils" import { ContainerRegistrationKeys, Modules, OrderChangeStatus, RuleOperator, } from "@medusajs/utils" -import { medusaIntegrationTestRunner } from "@medusajs/test-utils" import { adminHeaders, createAdminUser, @@ -418,6 +418,31 @@ medusaIntegrationTestRunner({ expect(result.summary.current_order_total).toEqual(124) 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 result = ( await api.post( diff --git a/packages/core/core-flows/src/order/workflows/order-edit/order-edit-update-item-quantity.ts b/packages/core/core-flows/src/order/workflows/order-edit/order-edit-update-item-quantity.ts index a70e674f6b65a..176ac2d345a1f 100644 --- a/packages/core/core-flows/src/order/workflows/order-edit/order-edit-update-item-quantity.ts +++ b/packages/core/core-flows/src/order/workflows/order-edit/order-edit-update-item-quantity.ts @@ -4,7 +4,12 @@ import { OrderPreviewDTO, OrderWorkflow, } from "@medusajs/framework/types" -import { ChangeActionType, OrderChangeStatus } from "@medusajs/framework/utils" +import { + BigNumber, + ChangeActionType, + MathBN, + OrderChangeStatus, +} from "@medusajs/framework/utils" import { WorkflowData, WorkflowResponse, @@ -75,19 +80,30 @@ export const orderEditUpdateItemQuantityWorkflow = createWorkflow( 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, - unit_price: item.unit_price, - compare_at_unit_price: item.compare_at_unit_price, - }, - })) + return items.map((item) => { + const existing = order?.items?.find( + (exItem) => exItem.id === item.id + )! + + const quantityDiff = new BigNumber( + MathBN.sub(item.quantity, existing.quantity) + ) + + return { + 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, + }, + } + }) } )