Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(medusa): Order/Cart decorate totals should assign the items totals #2546

Merged
merged 13 commits into from
Nov 7, 2022
34 changes: 34 additions & 0 deletions integration-tests/api/__tests__/admin/draft-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions integration-tests/api/__tests__/admin/order/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,17 +432,21 @@ Object {
"claim_order_id": null,
"created_at": Any<Date>,
"description": "",
"discount_total": 0,
"fulfilled_quantity": 2,
"has_shipping": null,
"id": Any<String>,
"is_giftcard": false,
"is_return": false,
"metadata": null,
"order_id": Any<String>,
"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 {
Expand All @@ -456,8 +460,10 @@ Object {
"updated_at": Any<Date>,
},
],
"tax_total": 400,
"thumbnail": "",
"title": "Intelligent Plastic Chips",
"total": 2400,
"unit_price": 1000,
"updated_at": Any<Date>,
"variant": Object {
Expand Down Expand Up @@ -768,18 +774,22 @@ Object {
"claim_order_id": null,
"created_at": Any<Date>,
"description": "",
"discount_total": 0,
"fulfilled_quantity": null,
"has_shipping": null,
"id": "test-item",
"is_giftcard": false,
"is_return": false,
"metadata": null,
"order_id": Any<String>,
"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 {
Expand All @@ -793,8 +803,10 @@ Object {
"updated_at": Any<Date>,
},
],
"tax_total": 400,
"thumbnail": null,
"title": "Intelligent Plastic Chips",
"total": 2400,
"unit_price": 1000,
"updated_at": Any<Date>,
"variant": Object {
Expand Down Expand Up @@ -991,6 +1003,7 @@ Object {
"claim_order_id": null,
"created_at": Any<Date>,
"description": "",
"discount_total": 0,
"discounted_price": "12.00 USD",
"fulfilled_quantity": 2,
"has_shipping": null,
Expand All @@ -999,11 +1012,14 @@ Object {
"is_return": false,
"metadata": null,
"order_id": Any<String>,
"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 {
Expand All @@ -1017,8 +1033,10 @@ Object {
"updated_at": Any<Date>,
},
],
"tax_total": 400,
"thumbnail": null,
"title": "Intelligent Plastic Chips",
"total": 2400,
"totals": Object {
"discount_total": 0,
"original_tax_total": 400,
Expand Down Expand Up @@ -1258,17 +1276,21 @@ Object {
"claim_order_id": null,
"created_at": Any<Date>,
"description": "",
"discount_total": 0,
"fulfilled_quantity": 2,
"has_shipping": null,
"id": "test-item",
"is_giftcard": false,
"is_return": false,
"metadata": null,
"order_id": Any<String>,
"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 {
Expand All @@ -1282,8 +1304,10 @@ Object {
"updated_at": Any<Date>,
},
],
"tax_total": 400,
"thumbnail": "",
"title": "Intelligent Plastic Chips",
"total": 2400,
"unit_price": 1000,
"updated_at": Any<Date>,
"variant": Object {
Expand Down Expand Up @@ -1579,17 +1603,21 @@ Object {
"claim_order_id": null,
"created_at": Any<Date>,
"description": "",
"discount_total": 0,
"fulfilled_quantity": 2,
"has_shipping": null,
"id": Any<String>,
"is_giftcard": false,
"is_return": false,
"metadata": null,
"order_id": Any<String>,
"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 {
Expand All @@ -1603,8 +1631,10 @@ Object {
"updated_at": Any<Date>,
},
],
"tax_total": 400,
"thumbnail": "",
"title": "Intelligent Plastic Chips",
"total": 2400,
"unit_price": 1000,
"updated_at": Any<Date>,
"variant": Object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { CartService, DraftOrderService } from "../../../../services"
import {
defaultAdminDraftOrdersCartFields,
defaultAdminDraftOrdersCartRelations,
defaultAdminDraftOrdersFields,
defaultAdminDraftOrdersRelations,
Expand Down Expand Up @@ -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 })
Expand Down
3 changes: 3 additions & 0 deletions packages/medusa/src/services/__mocks__/totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
Loading