From 7dbec10b3bdde8a9f869080d5ef2c3c97fca1077 Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Tue, 4 Mar 2025 08:01:12 -0300 Subject: [PATCH] fix(order): summary raw totals (#11712) --- .changeset/thick-readers-add.md | 6 ++++ .../src/payment/workflows/refund-payment.ts | 33 +++++++++---------- .../order/src/utils/apply-order-changes.ts | 7 ++-- 3 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 .changeset/thick-readers-add.md diff --git a/.changeset/thick-readers-add.md b/.changeset/thick-readers-add.md new file mode 100644 index 0000000000..25f9204575 --- /dev/null +++ b/.changeset/thick-readers-add.md @@ -0,0 +1,6 @@ +--- +"@medusajs/order": patch +"@medusajs/core-flows": patch +--- + +fix(order): summary raw totals diff --git a/packages/core/core-flows/src/payment/workflows/refund-payment.ts b/packages/core/core-flows/src/payment/workflows/refund-payment.ts index 6a733822e2..9d8b1c9bee 100644 --- a/packages/core/core-flows/src/payment/workflows/refund-payment.ts +++ b/packages/core/core-flows/src/payment/workflows/refund-payment.ts @@ -33,14 +33,14 @@ export type ValidateRefundStepInput = { /** * This step validates that the refund is valid for the order. * If the order does not have an outstanding balance to refund, the step throws an error. - * + * * :::note - * + * * You can retrieve an order or payment's details using [Query](https://docs.medusajs.com/learn/fundamentals/module-links/query), * or [useQueryGraphStep](https://docs.medusajs.com/resources/references/medusa-workflows/steps/useQueryGraphStep). - * + * * ::: - * + * * @example * const data = validateRefundStep({ * order: { @@ -56,12 +56,11 @@ export type ValidateRefundStepInput = { */ export const validateRefundStep = createStep( "validate-refund-step", - async function ({ - order, - payment, - amount, - }: ValidateRefundStepInput) { - const pendingDifference = order.summary?.raw_pending_difference! + async function ({ order, payment, amount }: ValidateRefundStepInput) { + const pendingDifference = + order.summary?.raw_pending_difference! ?? + order.summary?.pending_difference! ?? + 0 if (MathBN.gte(pendingDifference, 0)) { throw new MedusaError( @@ -102,12 +101,12 @@ export type RefundPaymentWorkflowInput = { export const refundPaymentWorkflowId = "refund-payment-workflow" /** - * This workflow refunds a payment. It's used by the + * This workflow refunds a payment. It's used by the * [Refund Payment Admin API Route](https://docs.medusajs.com/api/admin#payments_postpaymentsidrefund). - * + * * You can use this workflow within your own customizations or custom workflows, allowing you * to refund a payment in your custom flows. - * + * * @example * const { result } = await refundPaymentWorkflow(container) * .run({ @@ -115,16 +114,14 @@ export const refundPaymentWorkflowId = "refund-payment-workflow" * payment_id: "payment_123", * } * }) - * + * * @summary - * + * * Refund a payment. */ export const refundPaymentWorkflow = createWorkflow( refundPaymentWorkflowId, - ( - input: WorkflowData - ) => { + (input: WorkflowData) => { const payment = useRemoteQueryStep({ entry_point: "payment", fields: [ diff --git a/packages/modules/order/src/utils/apply-order-changes.ts b/packages/modules/order/src/utils/apply-order-changes.ts index 488d672095..74f90c93d8 100644 --- a/packages/modules/order/src/utils/apply-order-changes.ts +++ b/packages/modules/order/src/utils/apply-order-changes.ts @@ -144,14 +144,17 @@ export async function applyChangesToOrder( } const orderSummary = order.summary - summariesToUpsert.push({ + const upsertSummary = { id: orderSummary?.version === version ? orderSummary.id : undefined, order_id: order.id, version, totals: calculated.getSummaryFromOrder( calculated.order as unknown as OrderDTO ), - }) + } + + createRawPropertiesFromBigNumber(upsertSummary) + summariesToUpsert.push(upsertSummary) if (Object.keys(orderAttributes).length > 0) { orderToUpdate.push({