From ed121922b04e2e2af672fa6ab5ad69cd3cf17040 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 24 Nov 2022 19:31:45 +0100 Subject: [PATCH] fix(medusa): Order service legacy decorate totals should add totals to items (#2667) For some reason, probably a conflict, the legacy decorate totals does not attach the totals to the line items https://github.com/medusajs/medusa/pull/2546/files FIXES CORE-832 Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> --- .changeset/moody-ravens-wait.md | 5 +++ .../__snapshots__/index.js.snap | 32 ++++++++++++++++++- packages/medusa/src/services/order.ts | 23 ++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 .changeset/moody-ravens-wait.md diff --git a/.changeset/moody-ravens-wait.md b/.changeset/moody-ravens-wait.md new file mode 100644 index 0000000000..61616511b7 --- /dev/null +++ b/.changeset/moody-ravens-wait.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Order service legacy decorate totals should still apply the totals to the items diff --git a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap index a34bbefd16..b12a9dbfee 100644 --- a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap +++ b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap @@ -432,6 +432,7 @@ Object { "claim_order_id": null, "created_at": Any, "description": "", + "discount_total": 0, "fulfilled_quantity": 2, "has_shipping": null, "id": Any, @@ -439,10 +440,13 @@ Object { "is_return": false, "metadata": null, "order_id": Any, + "original_tax_total": 400, + "original_total": 2400, "quantity": 2, "returned_quantity": 1, "shipped_quantity": 2, "should_merge": true, + "subtotal": 2000, "swap_id": null, "tax_lines": Array [ Object { @@ -456,8 +460,10 @@ Object { "updated_at": Any, }, ], + "tax_total": 400, "thumbnail": "", "title": "Intelligent Plastic Chips", + "total": 2400, "unit_price": 1000, "updated_at": Any, "variant": Object { @@ -768,6 +774,7 @@ Object { "claim_order_id": null, "created_at": Any, "description": "", + "discount_total": 0, "fulfilled_quantity": null, "has_shipping": null, "id": "test-item", @@ -775,11 +782,14 @@ Object { "is_return": false, "metadata": null, "order_id": Any, + "original_tax_total": 400, + "original_total": 2400, "price": "10.00 USD", "quantity": 2, "returned_quantity": null, "shipped_quantity": null, "should_merge": true, + "subtotal": 2000, "swap_id": null, "tax_lines": Array [ Object { @@ -793,8 +803,10 @@ Object { "updated_at": Any, }, ], + "tax_total": 400, "thumbnail": null, "title": "Intelligent Plastic Chips", + "total": 2400, "unit_price": 1000, "updated_at": Any, "variant": Object { @@ -991,6 +1003,7 @@ Object { "claim_order_id": null, "created_at": Any, "description": "", + "discount_total": 0, "discounted_price": "12.00 USD", "fulfilled_quantity": 2, "has_shipping": null, @@ -999,11 +1012,14 @@ Object { "is_return": false, "metadata": null, "order_id": Any, + "original_tax_total": 400, + "original_total": 2400, "price": "12.00 USD", "quantity": 2, "returned_quantity": null, "shipped_quantity": 2, "should_merge": true, + "subtotal": 2000, "swap_id": null, "tax_lines": Array [ Object { @@ -1017,8 +1033,10 @@ Object { "updated_at": Any, }, ], + "tax_total": 400, "thumbnail": null, "title": "Intelligent Plastic Chips", + "total": 2400, "totals": Object { "discount_total": 0, "original_tax_total": 400, @@ -1258,6 +1276,7 @@ Object { "claim_order_id": null, "created_at": Any, "description": "", + "discount_total": 0, "fulfilled_quantity": 2, "has_shipping": null, "id": "test-item", @@ -1265,10 +1284,13 @@ Object { "is_return": false, "metadata": null, "order_id": Any, + "original_tax_total": 400, + "original_total": 2400, "quantity": 2, "returned_quantity": null, "shipped_quantity": 2, "should_merge": true, + "subtotal": 2000, "swap_id": null, "tax_lines": Array [ Object { @@ -1282,8 +1304,10 @@ Object { "updated_at": Any, }, ], + "tax_total": 400, "thumbnail": "", "title": "Intelligent Plastic Chips", + "total": 2400, "unit_price": 1000, "updated_at": Any, "variant": Object { @@ -1579,6 +1603,7 @@ Object { "claim_order_id": null, "created_at": Any, "description": "", + "discount_total": 0, "fulfilled_quantity": 2, "has_shipping": null, "id": Any, @@ -1586,10 +1611,13 @@ Object { "is_return": false, "metadata": null, "order_id": Any, + "original_tax_total": 400, + "original_total": 2400, "quantity": 2, "returned_quantity": null, "shipped_quantity": 2, "should_merge": true, + "subtotal": 2000, "swap_id": null, "tax_lines": Array [ Object { @@ -1603,8 +1631,10 @@ Object { "updated_at": Any, }, ], + "tax_total": 400, "thumbnail": "", "title": "Intelligent Plastic Chips", + "total": 2400, "unit_price": 1000, "updated_at": Any, "variant": Object { @@ -2446,4 +2476,4 @@ Object { "tracking_links": Array [], "tracking_number": "", } -`; +`; \ No newline at end of file diff --git a/packages/medusa/src/services/order.ts b/packages/medusa/src/services/order.ts index 1b5c5960cd..f207a6a241 100644 --- a/packages/medusa/src/services/order.ts +++ b/packages/medusa/src/services/order.ts @@ -1498,6 +1498,27 @@ class OrderService extends TransactionBaseService { order: Order, totalsFields: string[] = [] ): Promise { + if (totalsFields.some((field) => ["subtotal", "total"].includes(field))) { + const calculationContext = + await this.totalsService_.getCalculationContext(order, { + exclude_shipping: true, + }) + order.items = await Promise.all( + (order.items || []).map(async (item) => { + const itemTotals = await this.totalsService_.getLineItemTotals( + item, + order, + { + include_tax: true, + calculation_context: calculationContext, + } + ) + + return Object.assign(item, itemTotals) + }) + ) + } + for (const totalField of totalsFields) { switch (totalField) { case "shipping_total": { @@ -1603,9 +1624,9 @@ class OrderService extends TransactionBaseService { } /** + * Calculate and attach the different total fields on the object * @param order * @param totalsFieldsOrConfig - * @protected */ async decorateTotals( order: Order,