diff --git a/integration-tests/api/__tests__/admin/draft-order.js b/integration-tests/api/__tests__/admin/draft-order.js index e495d6d78aecd..4ecb7ac721298 100644 --- a/integration-tests/api/__tests__/admin/draft-order.js +++ b/integration-tests/api/__tests__/admin/draft-order.js @@ -785,6 +785,40 @@ describe("/admin/draft-orders", () => { }) }) + describe("GET /admin/draft-orders/:id", () => { + beforeEach(async () => { + await adminSeeder(dbConnection) + await draftOrderSeeder(dbConnection) + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("retrieves a draft-order should include the items totals", async () => { + const api = useApi() + + const order = await api.get("/admin/draft-orders/test-draft-order", { + headers: { + authorization: "Bearer test_token", + }, + }) + + expect(order.status).toEqual(200) + expect(order.data.draft_order).toEqual( + expect.objectContaining({ + id: "test-draft-order", + }) + ) + + order.data.draft_order.cart.items.forEach((item) => { + expect(item.total).toBeDefined() + expect(item.subtotal).toBeDefined() + }) + }) + }) + describe("DELETE /admin/draft-orders/:id", () => { beforeEach(async () => { await adminSeeder(dbConnection) diff --git a/integration-tests/api/__tests__/admin/order/order.js b/integration-tests/api/__tests__/admin/order/order.js index ca71fcde125b0..b54a776238bd3 100644 --- a/integration-tests/api/__tests__/admin/order/order.js +++ b/integration-tests/api/__tests__/admin/order/order.js @@ -2284,6 +2284,28 @@ describe("/admin/orders", () => { ) }) + it("retrieves an order should include the items totals", async () => { + const api = useApi() + + const order = await api.get("/admin/orders/test-order", { + headers: { + authorization: "Bearer test_token", + }, + }) + + expect(order.status).toEqual(200) + expect(order.data.order).toEqual( + expect.objectContaining({ + id: "test-order", + }) + ) + + order.data.order.items.forEach((item) => { + expect(item.total).toBeDefined() + expect(item.subtotal).toBeDefined() + }) + }) + it("throws on invalid relation", async () => { const api = useApi() 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 a34bbefd16f0e..cd593db1b8bbb 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 { diff --git a/packages/medusa/src/api/routes/admin/draft-orders/get-draft-order.ts b/packages/medusa/src/api/routes/admin/draft-orders/get-draft-order.ts index 863d739582eb2..c6e8d8bd001bd 100644 --- a/packages/medusa/src/api/routes/admin/draft-orders/get-draft-order.ts +++ b/packages/medusa/src/api/routes/admin/draft-orders/get-draft-order.ts @@ -1,6 +1,5 @@ import { CartService, DraftOrderService } from "../../../../services" import { - defaultAdminDraftOrdersCartFields, defaultAdminDraftOrdersCartRelations, defaultAdminDraftOrdersFields, defaultAdminDraftOrdersRelations, @@ -72,9 +71,8 @@ export default async (req, res) => { relations: defaultAdminDraftOrdersRelations, }) - draftOrder.cart = await cartService.retrieve(draftOrder.cart_id, { + draftOrder.cart = await cartService.retrieveWithTotals(draftOrder.cart_id, { relations: defaultAdminDraftOrdersCartRelations, - select: defaultAdminDraftOrdersCartFields, }) res.json({ draft_order: draftOrder }) diff --git a/packages/medusa/src/services/__mocks__/totals.js b/packages/medusa/src/services/__mocks__/totals.js index c72a21f6d268f..9fd2a265d6a4c 100644 --- a/packages/medusa/src/services/__mocks__/totals.js +++ b/packages/medusa/src/services/__mocks__/totals.js @@ -37,6 +37,9 @@ export const TotalsServiceMock = { getCalculationContext: jest.fn().mockImplementation((order, lineItems) => { return Promise.resolve({}) }), + getLineItemTotals: jest.fn().mockImplementation(() => { + return Promise.resolve({}) + }), } const mock = jest.fn().mockImplementation(() => { diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index 64b37389afd8c..fa00b3e7309b2 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -5,6 +5,7 @@ import CartService from "../cart" import { InventoryServiceMock } from "../__mocks__/inventory" import { LineItemAdjustmentServiceMock } from "../__mocks__/line-item-adjustment" import { FlagRouter } from "../../utils/flag-router" +import { taxProviderServiceMock } from "../__mocks__/tax-provider" const eventBusService = { emit: jest.fn(), @@ -57,6 +58,7 @@ describe("CartService", () => { manager: MockManager, totalsService, cartRepository, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) result = await cartService.retrieve(IdMap.getId("emptyCart")) @@ -90,6 +92,7 @@ describe("CartService", () => { totalsService, cartRepository, eventBusService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -179,6 +182,7 @@ describe("CartService", () => { customerService, regionService, eventBusService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -352,6 +356,7 @@ describe("CartService", () => { inventoryService, productVariantService, lineItemAdjustmentService: LineItemAdjustmentServiceMock, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -585,6 +590,7 @@ describe("CartService", () => { inventoryService, productVariantService, lineItemAdjustmentService: LineItemAdjustmentServiceMock, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({ sales_channels: true }), }) @@ -680,6 +686,7 @@ describe("CartService", () => { shippingOptionService, eventBusService, lineItemAdjustmentService: LineItemAdjustmentServiceMock, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -786,6 +793,7 @@ describe("CartService", () => { cartRepository, totalsService, eventBusService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -798,7 +806,7 @@ describe("CartService", () => { await cartService.update("withpays", {}) expect(cartRepository.findOneWithRelations).toHaveBeenCalledWith( - [ + expect.arrayContaining([ "items", "shipping_methods", "shipping_address", @@ -811,9 +819,7 @@ describe("CartService", () => { "discounts", "discounts.rule", "discounts.regions", - "items.tax_lines", - "region.tax_rates", - ], + ]), { where: { id: "withpays" }, } @@ -876,6 +882,7 @@ describe("CartService", () => { eventBusService, inventoryService, lineItemAdjustmentService: LineItemAdjustmentServiceMock, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -958,6 +965,7 @@ describe("CartService", () => { cartRepository, eventBusService, customerService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -977,19 +985,16 @@ describe("CartService", () => { ) expect(cartRepository.save).toHaveBeenCalledTimes(1) - expect(cartRepository.save).toHaveBeenCalledWith({ - customer_id: IdMap.getId("existing"), - customer: { - id: IdMap.getId("existing"), + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + customer_id: IdMap.getId("existing"), + customer: { + id: IdMap.getId("existing"), + email: "test@testdom.com", + }, email: "test@testdom.com", - }, - email: "test@testdom.com", - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - }) + }) + ) }) it("creates a new customer", async () => { @@ -1004,16 +1009,13 @@ describe("CartService", () => { ) expect(cartRepository.save).toHaveBeenCalledTimes(1) - expect(cartRepository.save).toHaveBeenCalledWith({ - customer_id: IdMap.getId("newCus"), - customer: { id: IdMap.getId("newCus"), email: "no@mail.com" }, - email: "no@mail.com", - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - }) + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + customer_id: IdMap.getId("newCus"), + customer: { id: IdMap.getId("newCus"), email: "no@mail.com" }, + email: "no@mail.com", + }) + ) }) it("throws on invalid email", async () => { @@ -1039,6 +1041,7 @@ describe("CartService", () => { cartRepository, addressRepository, eventBusService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -1074,15 +1077,12 @@ describe("CartService", () => { country_code: "us", }) - expect(cartRepository.save).toHaveBeenCalledWith({ - region: { countries: [{ iso_2: "us" }] }, - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - billing_address: address, - }) + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + region: { countries: [{ iso_2: "us" }] }, + billing_address: address, + }) + ) }) }) @@ -1101,6 +1101,7 @@ describe("CartService", () => { totalsService, cartRepository, eventBusService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -1135,15 +1136,12 @@ describe("CartService", () => { ...address, }) - expect(cartRepository.save).toHaveBeenCalledWith({ - region: { countries: [{ iso_2: "us" }] }, - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - shipping_address: address, - }) + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + region: { countries: [{ iso_2: "us" }] }, + shipping_address: address, + }) + ) }) it("throws if country not in region", async () => { @@ -1256,6 +1254,7 @@ describe("CartService", () => { eventBusService, paymentSessionRepository: MockRepository(), priceSelectionStrategy: priceSelectionStrat, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -1287,31 +1286,28 @@ describe("CartService", () => { ) expect(cartRepository.save).toHaveBeenCalledTimes(1) - expect(cartRepository.save).toHaveBeenCalledWith({ - region_id: "region", - region: { - id: "region", - countries: [{ iso_2: "us" }], - }, - shipping_address: { - country_code: "us", - }, - items: [IdMap.getId("testitem")], - payment_session: null, - payment_sessions: [], - gift_cards: [], - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - discounts: [ - { - id: IdMap.getId("stays"), - regions: [{ id: IdMap.getId("region-us") }], + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + region_id: "region", + region: { + id: "region", + countries: [{ iso_2: "us" }], }, - ], - }) + shipping_address: { + country_code: "us", + }, + items: [IdMap.getId("testitem")], + payment_session: null, + payment_sessions: [], + gift_cards: [], + discounts: [ + { + id: IdMap.getId("stays"), + regions: [{ id: IdMap.getId("region-us") }], + }, + ], + }) + ) }) }) @@ -1326,6 +1322,8 @@ describe("CartService", () => { }, ], }, + items: [], + shipping_methods: [], payment_sessions: [ { id: IdMap.getId("test-session"), @@ -1344,6 +1342,7 @@ describe("CartService", () => { totalsService, cartRepository, eventBusService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -1380,6 +1379,7 @@ describe("CartService", () => { const cart1 = { total: 100, items: [{ subtotal: 100 }], + shipping_methods: [], payment_sessions: [], region: { payment_providers: [{ id: "provider_1" }, { id: "provider_2" }], @@ -1388,6 +1388,8 @@ describe("CartService", () => { const cart2 = { total: 100, + items: [], + shipping_methods: [], payment_sessions: [{ provider_id: "provider_1" }], region: { payment_providers: [{ id: "provider_1" }, { id: "provider_2" }], @@ -1464,6 +1466,7 @@ describe("CartService", () => { cartRepository, paymentProviderService, eventBusService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -1655,6 +1658,7 @@ describe("CartService", () => { lineItemService, eventBusService, customShippingOptionService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -2010,6 +2014,7 @@ describe("CartService", () => { discountService, eventBusService, lineItemAdjustmentService: LineItemAdjustmentServiceMock, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) @@ -2032,37 +2037,34 @@ describe("CartService", () => { ) expect(cartRepository.save).toHaveBeenCalledTimes(1) - expect(cartRepository.save).toHaveBeenCalledWith({ - id: IdMap.getId("cart"), - region_id: IdMap.getId("good"), - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - items: [ - { - id: "li1", - quantity: 2, - unit_price: 1000, - }, - { - id: "li2", - quantity: 1, - unit_price: 500, - }, - ], - discounts: [ - { - id: IdMap.getId("10off"), - code: "10%OFF", - regions: [{ id: IdMap.getId("good") }], - rule: { - type: "percentage", + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + id: IdMap.getId("cart"), + region_id: IdMap.getId("good"), + items: [ + { + id: "li1", + quantity: 2, + unit_price: 1000, }, - }, - ], - }) + { + id: "li2", + quantity: 1, + unit_price: 500, + }, + ], + discounts: [ + { + id: IdMap.getId("10off"), + code: "10%OFF", + regions: [{ id: IdMap.getId("good") }], + rule: { + type: "percentage", + }, + }, + ], + }) + ) expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledTimes(1) expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledWith({ @@ -2085,37 +2087,34 @@ describe("CartService", () => { }) expect(cartRepository.save).toHaveBeenCalledTimes(1) - expect(cartRepository.save).toHaveBeenCalledWith({ - id: IdMap.getId("cart"), - region_id: IdMap.getId("good"), - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - items: [ - { - id: "li1", - quantity: 2, - unit_price: 1000, - }, - { - id: "li2", - quantity: 1, - unit_price: 500, - }, - ], - discounts: [ - { - id: IdMap.getId("10off"), - code: "10%OFF", - regions: [{ id: IdMap.getId("good") }], - rule: { - type: "percentage", + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + id: IdMap.getId("cart"), + region_id: IdMap.getId("good"), + items: [ + { + id: "li1", + quantity: 2, + unit_price: 1000, }, - }, - ], - }) + { + id: "li2", + quantity: 1, + unit_price: 500, + }, + ], + discounts: [ + { + id: IdMap.getId("10off"), + code: "10%OFF", + regions: [{ id: IdMap.getId("good") }], + rule: { + type: "percentage", + }, + }, + ], + }) + ) expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledTimes(1) expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledWith({ @@ -2139,45 +2138,42 @@ describe("CartService", () => { expect(discountService.retrieveByCode).toHaveBeenCalledTimes(2) expect(cartRepository.save).toHaveBeenCalledTimes(1) - expect(cartRepository.save).toHaveBeenCalledWith({ - id: IdMap.getId("cart"), - discounts: [ - { - id: IdMap.getId("10off"), - code: "10%OFF", - regions: [{ id: IdMap.getId("good") }], - rule: { - type: "percentage", + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + id: IdMap.getId("cart"), + discounts: [ + { + id: IdMap.getId("10off"), + code: "10%OFF", + regions: [{ id: IdMap.getId("good") }], + rule: { + type: "percentage", + }, }, - }, - { - id: IdMap.getId("freeship"), - code: "FREESHIPPING", - regions: [{ id: IdMap.getId("good") }], - rule: { - type: "free_shipping", + { + id: IdMap.getId("freeship"), + code: "FREESHIPPING", + regions: [{ id: IdMap.getId("good") }], + rule: { + type: "free_shipping", + }, }, - }, - ], - items: [ - { - id: "li1", - quantity: 2, - unit_price: 1000, - }, - { - id: "li2", - quantity: 1, - unit_price: 500, - }, - ], - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - region_id: IdMap.getId("good"), - }) + ], + items: [ + { + id: "li1", + quantity: 2, + unit_price: 1000, + }, + { + id: "li2", + quantity: 1, + unit_price: 500, + }, + ], + region_id: IdMap.getId("good"), + }) + ) expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledTimes(1) expect(LineItemAdjustmentServiceMock.delete).toHaveBeenCalledWith({ @@ -2209,28 +2205,25 @@ describe("CartService", () => { ) expect(cartRepository.save).toHaveBeenCalledTimes(1) - expect(cartRepository.save).toHaveBeenCalledWith({ - id: "with-d-and-customer", - region_id: IdMap.getId("good"), - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - discounts: [ - { - id: "ApplicableForCustomer", - code: "ApplicableForCustomer", - regions: [{ id: IdMap.getId("good") }], - rule: { - id: "test-rule", - type: "percentage", + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + id: "with-d-and-customer", + region_id: IdMap.getId("good"), + discounts: [ + { + id: "ApplicableForCustomer", + code: "ApplicableForCustomer", + regions: [{ id: IdMap.getId("good") }], + rule: { + id: "test-rule", + type: "percentage", + }, + starts_at: expect.any(Date), + ends_at: expect.any(Date), }, - starts_at: expect.any(Date), - ends_at: expect.any(Date), - }, - ], - }) + ], + }) + ) }) it("successfully remove all discounts that have been applied", async () => { @@ -2244,28 +2237,25 @@ describe("CartService", () => { ) expect(cartRepository.save).toHaveBeenCalledTimes(1) - expect(cartRepository.save).toHaveBeenCalledWith({ - id: IdMap.getId("cart"), - region_id: IdMap.getId("good"), - discount_total: 0, - shipping_total: 0, - subtotal: 0, - tax_total: 0, - total: 0, - items: [ - { - id: "li1", - quantity: 2, - unit_price: 1000, - }, - { - id: "li2", - quantity: 1, - unit_price: 500, - }, - ], - discounts: [], - }) + expect(cartRepository.save).toHaveBeenCalledWith( + expect.objectContaining({ + id: IdMap.getId("cart"), + region_id: IdMap.getId("good"), + items: [ + { + id: "li1", + quantity: 2, + unit_price: 1000, + }, + { + id: "li2", + quantity: 1, + unit_price: 500, + }, + ], + discounts: [], + }) + ) }) }) @@ -2298,6 +2288,7 @@ describe("CartService", () => { totalsService, cartRepository, eventBusService, + taxProviderService: taxProviderServiceMock, featureFlagRouter: new FlagRouter({}), }) diff --git a/packages/medusa/src/services/__tests__/order.js b/packages/medusa/src/services/__tests__/order.js index a867a80ac2a0c..cbd5c8915f3e1 100644 --- a/packages/medusa/src/services/__tests__/order.js +++ b/packages/medusa/src/services/__tests__/order.js @@ -8,6 +8,12 @@ describe("OrderService", () => { withTransaction: function () { return this }, + getCalculationContext: jest.fn().mockImplementation((order, lineItems) => { + return Promise.resolve({}) + }), + getLineItemTotals: jest.fn().mockImplementation(() => { + return Promise.resolve({}) + }), getLineItemRefund: () => {}, getTotal: (o) => { return o.total || 0 @@ -1249,6 +1255,7 @@ describe("OrderService", () => { { order: { id: IdMap.getId("order"), + items: [], shipping_methods: [ { shipping_option: { @@ -1278,6 +1285,7 @@ describe("OrderService", () => { { order: { id: IdMap.getId("order"), + items: [], shipping_methods: [ { shipping_option: { diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index 1e64f21771b00..fc82417f0f2d6 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -920,13 +920,6 @@ class CartService extends TransactionBaseService { } const cart = await this.retrieve(cartId, { - select: [ - "subtotal", - "tax_total", - "shipping_total", - "discount_total", - "total", - ], relations, }) diff --git a/packages/medusa/src/services/order.ts b/packages/medusa/src/services/order.ts index 5f64dc02be91a..b1cafd7ca304d 100644 --- a/packages/medusa/src/services/order.ts +++ b/packages/medusa/src/services/order.ts @@ -1412,6 +1412,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": {