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

feat(medusa): Cart and totals improvements #2475

Merged
merged 31 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3c0b4b9
feat(medusa): Cart and totals improvements
adrien2p Oct 20, 2022
f093e03
test(medusa): cart service unit test
adrien2p Oct 20, 2022
f7ec936
cleanup
adrien2p Oct 20, 2022
011e9f9
cleanup
adrien2p Oct 20, 2022
1b69422
fix(medusa): cart service
adrien2p Oct 20, 2022
59d9ea4
feat(medusa): Move totals improvements to a new totals service
adrien2p Oct 21, 2022
c088396
some more refactoring around the order as well
adrien2p Oct 21, 2022
894c6e7
use isDefined
adrien2p Oct 21, 2022
8f518a4
fix swap service test mock
adrien2p Oct 21, 2022
d714bfd
some order impr
adrien2p Oct 21, 2022
0b404a0
fix cart upadte
adrien2p Oct 21, 2022
8c2c4e3
test(medusa): Fix unit tests
adrien2p Oct 31, 2022
c9ec63e
refactor cart/order totals and fix integration
adrien2p Oct 31, 2022
ea76fc4
cleanup and fix wrong manager in cart completion
adrien2p Oct 31, 2022
cef7559
cleanup and improve
adrien2p Nov 2, 2022
73be837
test(medusa): fix unit and integration
adrien2p Nov 2, 2022
eb4f2d3
cleanup
adrien2p Nov 2, 2022
fc19e38
fix cart/order decorate total for return item
adrien2p Nov 2, 2022
72b74c2
partial feedback
adrien2p Nov 3, 2022
491183a
partial feedback
adrien2p Nov 3, 2022
ee86e70
feedback
adrien2p Nov 4, 2022
256d281
test(medusa): Start implementing new totals service tests
adrien2p Nov 7, 2022
207f314
improvements and tests
adrien2p Nov 8, 2022
4c3cdd3
fix tests
adrien2p Nov 8, 2022
333cdb9
fix integration
adrien2p Nov 9, 2022
cd4bddc
fix wronf prop name
adrien2p Nov 9, 2022
7c211a0
fix order decorate totals
adrien2p Nov 9, 2022
4194cb1
remove comment
adrien2p Nov 10, 2022
50585f8
Address feedback
adrien2p Nov 14, 2022
5718407
Address feedback
adrien2p Nov 14, 2022
3a0f313
fix test
adrien2p Nov 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions integration-tests/api/__tests__/admin/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -1866,25 +1866,28 @@ describe("/admin/products", () => {

expect(res.status).toEqual(200)

expect(insertedVariant.prices).toEqual([
expect.objectContaining({
currency_code: "usd",
amount: 100,
min_quantity: null,
max_quantity: null,
variant_id: insertedVariant.id,
region_id: null,
}),
expect.objectContaining({
currency_code: "usd",
amount: 200,
min_quantity: null,
max_quantity: null,
price_list_id: null,
variant_id: insertedVariant.id,
region_id: "test-region",
}),
])
expect(insertedVariant.prices).toHaveLength(2)
expect(insertedVariant.prices).toEqual(
expect.arrayContaining([
expect.objectContaining({
currency_code: "usd",
amount: 100,
min_quantity: null,
max_quantity: null,
variant_id: insertedVariant.id,
region_id: null,
}),
expect.objectContaining({
currency_code: "usd",
amount: 200,
min_quantity: null,
max_quantity: null,
price_list_id: null,
variant_id: insertedVariant.id,
region_id: "test-region",
}),
])
)
})
})

Expand Down
18 changes: 13 additions & 5 deletions integration-tests/api/__tests__/store/cart/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ describe("/store/carts", () => {
tax_rate: 0,
})
await manager.query(
`UPDATE "country" SET region_id='region' WHERE iso_2 = 'us'`
`UPDATE "country"
SET region_id='region'
WHERE iso_2 = 'us'`
)
})

Expand All @@ -88,9 +90,12 @@ describe("/store/carts", () => {
const api = useApi()

await dbConnection.manager.query(
`UPDATE "country" SET region_id=null WHERE iso_2 = 'us'`
`UPDATE "country"
SET region_id=null
WHERE iso_2 = 'us'`
)
await dbConnection.manager.query(`DELETE from region`)
await dbConnection.manager.query(`DELETE
from region`)

try {
await api.post("/store/carts")
Expand Down Expand Up @@ -1679,7 +1684,9 @@ describe("/store/carts", () => {
const manager = dbConnection.manager
const api = useApi()
await manager.query(
`UPDATE "cart" SET completed_at=current_timestamp WHERE id = 'test-cart-2'`
`UPDATE "cart"
SET completed_at=current_timestamp
WHERE id = 'test-cart-2'`
)
try {
await api.post(`/store/carts/test-cart-2/complete-cart`)
Expand Down Expand Up @@ -1982,7 +1989,8 @@ describe("/store/carts", () => {
try {
await cartSeeder(dbConnection)
await dbConnection.manager.query(
`INSERT INTO "cart_discounts" (cart_id, discount_id) VALUES ('test-cart', 'free-shipping')`
`INSERT INTO "cart_discounts" (cart_id, discount_id)
VALUES ('test-cart', 'free-shipping')`
)
} catch (err) {
console.log(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,17 @@ 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 @@ -460,10 +456,8 @@ 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 @@ -774,22 +768,18 @@ 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 @@ -803,10 +793,8 @@ 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 @@ -1003,7 +991,6 @@ 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 @@ -1012,14 +999,11 @@ 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 @@ -1033,10 +1017,8 @@ 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 @@ -1276,21 +1258,17 @@ 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 @@ -1304,10 +1282,8 @@ 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 @@ -1603,21 +1579,17 @@ 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 @@ -1631,10 +1603,8 @@ 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
Expand Up @@ -71,9 +71,15 @@ export default async (req, res) => {
relations: defaultAdminDraftOrdersRelations,
})

draftOrder.cart = await cartService.retrieveWithTotals(draftOrder.cart_id, {
relations: defaultAdminDraftOrdersCartRelations,
})
draftOrder.cart = await cartService.retrieveWithTotals(
draftOrder.cart_id,
{
relations: defaultAdminDraftOrdersCartRelations,
},
{
force_taxes: true,
}
)

res.json({ draft_order: draftOrder })
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,7 @@ export default async (req, res) => {

const cart = await cartService
.withTransaction(manager)
.retrieve(draftOrder.cart_id, {
select: ["total"],
relations: [
"discounts",
"discounts.rule",
"shipping_methods",
"region",
"items",
],
})
.retrieveWithTotals(draftOrder.cart_id)

await paymentProviderService
.withTransaction(manager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,23 @@ describe("GET /admin/orders", () => {
})

it("calls orderService retrieve", () => {
expect(OrderServiceMock.retrieve).toHaveBeenCalledTimes(1)
expect(OrderServiceMock.retrieve).toHaveBeenCalledWith(
expect(OrderServiceMock.retrieveWithTotals).toHaveBeenCalledTimes(1)
expect(OrderServiceMock.retrieveWithTotals).toHaveBeenCalledWith(
IdMap.getId("test-order"),
{
select: defaultAdminOrdersFields,
select: defaultAdminOrdersFields.filter((field) => {
return ![
"shipping_total",
"discount_total",
"tax_total",
"refunded_total",
"total",
"subtotal",
"refundable_amount",
"gift_card_total",
"gift_card_tax_total",
].includes(field)
}),
relations: defaultAdminOrdersRelations,
}
)
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/api/routes/admin/orders/get-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default async (req, res) => {

const orderService: OrderService = req.scope.resolve("orderService")

const order = await orderService.retrieve(id, req.retrieveConfig)
const order = await orderService.retrieveWithTotals(id, req.retrieveConfig)

res.json({ order })
}
14 changes: 13 additions & 1 deletion packages/medusa/src/api/routes/admin/orders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@ export default (app, featureFlagRouter: FlagRouter) => {
"/:id",
transformQuery(FindParams, {
defaultRelations: relations,
defaultFields: defaultAdminOrdersFields,
defaultFields: defaultAdminOrdersFields.filter((field) => {
return ![
"shipping_total",
"discount_total",
"tax_total",
"refunded_total",
"total",
"subtotal",
"refundable_amount",
"gift_card_total",
"gift_card_tax_total",
].includes(field)
}),
allowedFields: allowedAdminOrdersFields,
allowedRelations: allowedAdminOrdersRelations,
isList: false,
Expand Down
Loading