Skip to content

Commit

Permalink
Merge branch 'develop' into fix-typo-in-documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hadicph authored Nov 12, 2024
2 parents 7549ba3 + fc5d2b5 commit e1bad34
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-chefs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/core-flows": patch
---

Order edit quantity diff
27 changes: 26 additions & 1 deletion integration-tests/http/__tests__/order-edits/order-edits.spec.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
}
})
}
)

Expand Down

0 comments on commit e1bad34

Please sign in to comment.