diff --git a/integration-tests/api/__tests__/taxes/cart.js b/integration-tests/api/__tests__/taxes/cart.js new file mode 100644 index 0000000000..ea8c3765b7 --- /dev/null +++ b/integration-tests/api/__tests__/taxes/cart.js @@ -0,0 +1,237 @@ +const path = require("path") + +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { useDb, initDb } = require("../../../helpers/use-db") +const { + simpleDiscountFactory, + simpleRegionFactory, + simpleProductFactory, + simpleProductTaxRateFactory, +} = require("../../factories") + +const adminSeeder = require("../../helpers/admin-seeder") + +jest.setTimeout(30000) + +describe("Cart Totals Calculations", () => { + let medusaProcess + let dbConnection + + beforeAll(async () => { + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) + + afterAll(async () => { + const db = useDb() + await db.shutdown() + + medusaProcess.kill() + }) + + beforeEach(async () => { + try { + await adminSeeder(dbConnection) + } catch (err) { + console.log(err) + throw err + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("sets correct line item totals for a cart with item of price 100 and tax rate 10", async () => { + const api = useApi() + + const region = await simpleRegionFactory(dbConnection) + const product = await simpleProductFactory(dbConnection) + await simpleProductTaxRateFactory(dbConnection, { + product_id: product.id, + rate: { + region_id: region.id, + rate: 10, + }, + }) + + // create cart + const { cart } = await api + .post("/store/carts", { + region_id: region.id, + }) + .then((res) => res.data) + + // add product to cart + const res = await api.post(`/store/carts/${cart.id}/line-items`, { + variant_id: product.variants[0].id, + quantity: 1, + }) + + expect(res.data.cart.items[0].unit_price).toEqual(100) + expect(res.data.cart.items[0].quantity).toEqual(1) + expect(res.data.cart.items[0].subtotal).toEqual(100) + expect(res.data.cart.items[0].tax_total).toEqual(10) + expect(res.data.cart.items[0].total).toEqual(110) + expect(res.data.cart.items[0].original_total).toEqual(110) + expect(res.data.cart.items[0].original_tax_total).toEqual(10) + expect(res.data.cart.items[0].discount_total).toEqual(0) + expect(res.data.cart.items[0].gift_card_total).toEqual(0) + }) + + it("sets correct line item totals for a cart with item of price 100; tax rate 10; discount 10", async () => { + const api = useApi() + + const region = await simpleRegionFactory(dbConnection) + const product = await simpleProductFactory(dbConnection) + const discount = await simpleDiscountFactory(dbConnection, { + regions: [region.id], + type: "percentage", + value: 10, + }) + + await simpleProductTaxRateFactory(dbConnection, { + product_id: product.id, + rate: { + region_id: region.id, + rate: 10, + }, + }) + + const { cart } = await api + .post("/store/carts", { + region_id: region.id, + }) + .then((res) => res.data) + + await api.post(`/store/carts/${cart.id}/line-items`, { + variant_id: product.variants[0].id, + quantity: 1, + }) + + const res = await api.post(`/store/carts/${cart.id}`, { + discounts: [ + { + code: discount.code, + }, + ], + }) + + expect(res.data.cart.items[0].unit_price).toEqual(100) + expect(res.data.cart.items[0].quantity).toEqual(1) + expect(res.data.cart.items[0].subtotal).toEqual(100) + expect(res.data.cart.items[0].tax_total).toEqual(9) + expect(res.data.cart.items[0].total).toEqual(99) + expect(res.data.cart.items[0].original_total).toEqual(110) + expect(res.data.cart.items[0].original_tax_total).toEqual(10) + expect(res.data.cart.items[0].discount_total).toEqual(10) + expect(res.data.cart.items[0].gift_card_total).toEqual(0) + }) + + it("doesn't include taxes in !automatic_taxes regions", async () => { + const api = useApi() + + const region = await simpleRegionFactory(dbConnection, { + automatic_taxes: false, + }) + const product = await simpleProductFactory(dbConnection) + const discount = await simpleDiscountFactory(dbConnection, { + regions: [region.id], + type: "percentage", + value: 10, + }) + + await simpleProductTaxRateFactory(dbConnection, { + product_id: product.id, + rate: { + region_id: region.id, + rate: 10, + }, + }) + + const { cart } = await api + .post("/store/carts", { + region_id: region.id, + }) + .then((res) => res.data) + + await api.post(`/store/carts/${cart.id}/line-items`, { + variant_id: product.variants[0].id, + quantity: 1, + }) + + const res = await api.post(`/store/carts/${cart.id}`, { + discounts: [ + { + code: discount.code, + }, + ], + }) + + expect(res.data.cart.items[0].unit_price).toEqual(100) + expect(res.data.cart.items[0].quantity).toEqual(1) + expect(res.data.cart.items[0].subtotal).toEqual(100) + expect(res.data.cart.items[0].tax_total).toEqual(0) + expect(res.data.cart.items[0].total).toEqual(90) + expect(res.data.cart.items[0].original_total).toEqual(100) + expect(res.data.cart.items[0].original_tax_total).toEqual(0) + expect(res.data.cart.items[0].discount_total).toEqual(10) + expect(res.data.cart.items[0].gift_card_total).toEqual(0) + }) + + it("includes taxes in !automatic_taxes regions when forced", async () => { + const api = useApi() + + const region = await simpleRegionFactory(dbConnection, { + automatic_taxes: false, + }) + const product = await simpleProductFactory(dbConnection) + const discount = await simpleDiscountFactory(dbConnection, { + regions: [region.id], + type: "percentage", + value: 10, + }) + + await simpleProductTaxRateFactory(dbConnection, { + product_id: product.id, + rate: { + region_id: region.id, + rate: 10, + }, + }) + + const { cart } = await api + .post("/store/carts", { + region_id: region.id, + }) + .then((res) => res.data) + + await api.post(`/store/carts/${cart.id}/line-items`, { + variant_id: product.variants[0].id, + quantity: 1, + }) + + await api.post(`/store/carts/${cart.id}`, { + discounts: [ + { + code: discount.code, + }, + ], + }) + + const res = await api.post(`/store/carts/${cart.id}/taxes`) + + expect(res.data.cart.items[0].unit_price).toEqual(100) + expect(res.data.cart.items[0].quantity).toEqual(1) + expect(res.data.cart.items[0].subtotal).toEqual(100) + expect(res.data.cart.items[0].tax_total).toEqual(9) + expect(res.data.cart.items[0].total).toEqual(99) + expect(res.data.cart.items[0].original_total).toEqual(110) + expect(res.data.cart.items[0].original_tax_total).toEqual(10) + expect(res.data.cart.items[0].discount_total).toEqual(10) + expect(res.data.cart.items[0].gift_card_total).toEqual(0) + }) +}) diff --git a/integration-tests/api/factories/index.ts b/integration-tests/api/factories/index.ts index d77949c266..56b0298a4f 100644 --- a/integration-tests/api/factories/index.ts +++ b/integration-tests/api/factories/index.ts @@ -1,5 +1,6 @@ export * from "./simple-payment-factory" export * from "./simple-batch-job-factory" +export * from "./simple-discount-factory" export * from "./simple-order-factory" export * from "./simple-cart-factory" export * from "./simple-region-factory" @@ -14,4 +15,3 @@ export * from "./simple-shipping-method-factory" export * from "./simple-product-type-tax-rate-factory" export * from "./simple-price-list-factory" export * from "./simple-batch-job-factory" - diff --git a/packages/medusa/src/api/routes/store/carts/add-shipping-method.ts b/packages/medusa/src/api/routes/store/carts/add-shipping-method.ts index 7a73dd1ab4..f891fcc8e7 100644 --- a/packages/medusa/src/api/routes/store/carts/add-shipping-method.ts +++ b/packages/medusa/src/api/routes/store/carts/add-shipping-method.ts @@ -3,6 +3,7 @@ import { EntityManager } from "typeorm" import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" import { validator } from "../../../../utils/validator" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts/{id}/shipping-methods @@ -59,7 +60,9 @@ export default async (req, res) => { relations: defaultStoreCartRelations, }) - res.status(200).json({ cart: updatedCart }) + const data = await decorateLineItemsWithTotals(updatedCart, req) + + res.status(200).json({ cart: data }) } export class StorePostCartsCartShippingMethodReq { diff --git a/packages/medusa/src/api/routes/store/carts/calculate-taxes.ts b/packages/medusa/src/api/routes/store/carts/calculate-taxes.ts index ed055e1648..d13d5af8da 100644 --- a/packages/medusa/src/api/routes/store/carts/calculate-taxes.ts +++ b/packages/medusa/src/api/routes/store/carts/calculate-taxes.ts @@ -1,6 +1,7 @@ import { EntityManager } from "typeorm" import { IdempotencyKey } from "../../../../models/idempotency-key" import { CartService, IdempotencyKeyService } from "../../../../services" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts/{id}/taxes @@ -64,6 +65,7 @@ export default async (req, res) => { const cart = await cartService.withTransaction(manager).retrieve( id, { + relations: ["items", "items.adjustments"], select: [ "total", "subtotal", @@ -76,9 +78,13 @@ export default async (req, res) => { { force_taxes: true } ) + const data = await decorateLineItemsWithTotals(cart, req, { + force_taxes: true, + }) + return { response_code: 200, - response_body: { cart }, + response_body: { cart: data }, } } ) diff --git a/packages/medusa/src/api/routes/store/carts/create-cart.ts b/packages/medusa/src/api/routes/store/carts/create-cart.ts index 02f8dde4bf..4fdaa9d9c8 100644 --- a/packages/medusa/src/api/routes/store/carts/create-cart.ts +++ b/packages/medusa/src/api/routes/store/carts/create-cart.ts @@ -15,6 +15,7 @@ import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService, LineItemService } from "../../../../services" import { validator } from "../../../../utils/validator" import { AddressPayload } from "../../../../types/common" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts @@ -144,7 +145,9 @@ export default async (req, res) => { relations: defaultStoreCartRelations, }) - res.status(200).json({ cart }) + const data = await decorateLineItemsWithTotals(cart, req) + + res.status(200).json({ cart: data }) }) } diff --git a/packages/medusa/src/api/routes/store/carts/create-line-item.ts b/packages/medusa/src/api/routes/store/carts/create-line-item.ts index 8153eef1a2..eaa069a431 100644 --- a/packages/medusa/src/api/routes/store/carts/create-line-item.ts +++ b/packages/medusa/src/api/routes/store/carts/create-line-item.ts @@ -3,6 +3,7 @@ import { EntityManager } from "typeorm" import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService, LineItemService } from "../../../../services" import { validator } from "../../../../utils/validator" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts/{id}/line-items @@ -63,7 +64,9 @@ export default async (req, res) => { relations: defaultStoreCartRelations, }) - res.status(200).json({ cart }) + const data = await decorateLineItemsWithTotals(cart, req) + + res.status(200).json({ cart: data }) } export class StorePostCartsCartLineItemsReq { diff --git a/packages/medusa/src/api/routes/store/carts/create-payment-sessions.ts b/packages/medusa/src/api/routes/store/carts/create-payment-sessions.ts index 14e7ef0e66..3c61a72791 100644 --- a/packages/medusa/src/api/routes/store/carts/create-payment-sessions.ts +++ b/packages/medusa/src/api/routes/store/carts/create-payment-sessions.ts @@ -1,5 +1,6 @@ import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts/{id}/payment-sessions @@ -32,5 +33,6 @@ export default async (req, res) => { relations: defaultStoreCartRelations, }) - res.status(200).json({ cart }) + const data = await decorateLineItemsWithTotals(cart, req) + res.status(200).json({ cart: data }) } diff --git a/packages/medusa/src/api/routes/store/carts/decorate-line-items-with-totals.ts b/packages/medusa/src/api/routes/store/carts/decorate-line-items-with-totals.ts new file mode 100644 index 0000000000..167e3ed36e --- /dev/null +++ b/packages/medusa/src/api/routes/store/carts/decorate-line-items-with-totals.ts @@ -0,0 +1,27 @@ +import { Request } from "express" +import { TotalsService } from "../../../../services" +import { Cart, LineItem } from "../../../../models" + +export const decorateLineItemsWithTotals = async ( + cart: Cart, + req: Request, + options: { force_taxes: boolean } = { force_taxes: false } +): Promise => { + const totalsService: TotalsService = req.scope.resolve("totalsService") + + if (cart.items && cart.region) { + const items = await Promise.all( + cart.items.map(async (item: LineItem) => { + const itemTotals = await totalsService.getLineItemTotals(item, cart, { + include_tax: options.force_taxes || cart.region.automatic_taxes, + }) + + return Object.assign(item, itemTotals) + }) + ) + + return Object.assign(cart, { items }) + } + + return cart +} diff --git a/packages/medusa/src/api/routes/store/carts/delete-discount.ts b/packages/medusa/src/api/routes/store/carts/delete-discount.ts index 668d0218c8..cdca0cdcc9 100644 --- a/packages/medusa/src/api/routes/store/carts/delete-discount.ts +++ b/packages/medusa/src/api/routes/store/carts/delete-discount.ts @@ -1,6 +1,7 @@ import { EntityManager } from "typeorm" import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [delete] /carts/{id}/discounts/{code} @@ -46,6 +47,7 @@ export default async (req, res) => { select: defaultStoreCartFields, relations: defaultStoreCartRelations, }) + const data = await decorateLineItemsWithTotals(cart, req) - res.status(200).json({ cart }) + res.status(200).json({ cart: data }) } diff --git a/packages/medusa/src/api/routes/store/carts/delete-line-item.ts b/packages/medusa/src/api/routes/store/carts/delete-line-item.ts index 51d4959322..e667ad26c7 100644 --- a/packages/medusa/src/api/routes/store/carts/delete-line-item.ts +++ b/packages/medusa/src/api/routes/store/carts/delete-line-item.ts @@ -1,6 +1,7 @@ import { EntityManager } from "typeorm" import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [delete] /carts/{id}/line-items/{line_id} @@ -46,6 +47,7 @@ export default async (req, res) => { select: defaultStoreCartFields, relations: defaultStoreCartRelations, }) + const data = await decorateLineItemsWithTotals(cart, req) - res.status(200).json({ cart }) + res.status(200).json({ cart: data }) } diff --git a/packages/medusa/src/api/routes/store/carts/delete-payment-session.ts b/packages/medusa/src/api/routes/store/carts/delete-payment-session.ts index efe488656e..a14b5ce1d5 100644 --- a/packages/medusa/src/api/routes/store/carts/delete-payment-session.ts +++ b/packages/medusa/src/api/routes/store/carts/delete-payment-session.ts @@ -1,5 +1,6 @@ import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [delete] /carts/{id}/payment-sessions/{provider_id} @@ -32,5 +33,6 @@ export default async (req, res) => { relations: defaultStoreCartRelations, }) - res.status(200).json({ cart }) + const data = await decorateLineItemsWithTotals(cart, req) + res.status(200).json({ cart: data }) } diff --git a/packages/medusa/src/api/routes/store/carts/get-cart.ts b/packages/medusa/src/api/routes/store/carts/get-cart.ts index 599716c85f..99c043f75b 100644 --- a/packages/medusa/src/api/routes/store/carts/get-cart.ts +++ b/packages/medusa/src/api/routes/store/carts/get-cart.ts @@ -1,5 +1,6 @@ import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [get] /carts/{id} @@ -47,5 +48,6 @@ export default async (req, res) => { relations: defaultStoreCartRelations, }) - res.json({ cart }) + const data = await decorateLineItemsWithTotals(cart, req) + res.json({ cart: data }) } diff --git a/packages/medusa/src/api/routes/store/carts/refresh-payment-session.ts b/packages/medusa/src/api/routes/store/carts/refresh-payment-session.ts index 80eb42a4e1..d120072f5b 100644 --- a/packages/medusa/src/api/routes/store/carts/refresh-payment-session.ts +++ b/packages/medusa/src/api/routes/store/carts/refresh-payment-session.ts @@ -1,4 +1,5 @@ import { CartService } from "../../../../services" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts/{id}/payment-sessions/{provider_id}/refresh @@ -44,5 +45,6 @@ export default async (req, res) => { ], }) - res.status(200).json({ cart }) + const data = await decorateLineItemsWithTotals(cart, req) + res.status(200).json({ cart: data }) } diff --git a/packages/medusa/src/api/routes/store/carts/set-payment-session.ts b/packages/medusa/src/api/routes/store/carts/set-payment-session.ts index 98da5677f7..b3c1b1d2ee 100644 --- a/packages/medusa/src/api/routes/store/carts/set-payment-session.ts +++ b/packages/medusa/src/api/routes/store/carts/set-payment-session.ts @@ -2,6 +2,7 @@ import { IsString } from "class-validator" import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" import { validator } from "../../../../utils/validator" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts/{id}/payment-session @@ -39,7 +40,8 @@ export default async (req, res) => { relations: defaultStoreCartRelations, }) - res.status(200).json({ cart }) + const data = await decorateLineItemsWithTotals(cart, req) + res.status(200).json({ cart: data }) } export class StorePostCartsCartPaymentSessionReq { diff --git a/packages/medusa/src/api/routes/store/carts/update-cart.ts b/packages/medusa/src/api/routes/store/carts/update-cart.ts index 1874c6b398..9459d712da 100644 --- a/packages/medusa/src/api/routes/store/carts/update-cart.ts +++ b/packages/medusa/src/api/routes/store/carts/update-cart.ts @@ -12,6 +12,7 @@ import { CartUpdateProps } from "../../../../types/cart" import { AddressPayload } from "../../../../types/common" import { validator } from "../../../../utils/validator" import { IsType } from "../../../../utils/validators/is-type" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /store/carts/{id} @@ -118,8 +119,9 @@ export default async (req, res) => { select: defaultStoreCartFields, relations: defaultStoreCartRelations, }) + const data = await decorateLineItemsWithTotals(cart, req) - res.json({ cart }) + res.json({ cart: data }) } class GiftCard { diff --git a/packages/medusa/src/api/routes/store/carts/update-line-item.ts b/packages/medusa/src/api/routes/store/carts/update-line-item.ts index cea8c8f3a0..7e107f5aa8 100644 --- a/packages/medusa/src/api/routes/store/carts/update-line-item.ts +++ b/packages/medusa/src/api/routes/store/carts/update-line-item.ts @@ -4,6 +4,7 @@ import { EntityManager } from "typeorm" import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" import { validator } from "../../../../utils/validator" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts/{id}/line-items/{line_id} @@ -78,8 +79,9 @@ export default async (req, res) => { select: defaultStoreCartFields, relations: defaultStoreCartRelations, }) + const data = await decorateLineItemsWithTotals(cart, req) - res.status(200).json({ cart }) + res.status(200).json({ cart: data }) } export class StorePostCartsCartLineItemsItemReq { diff --git a/packages/medusa/src/api/routes/store/carts/update-payment-session.ts b/packages/medusa/src/api/routes/store/carts/update-payment-session.ts index 803f4811b6..378876e87c 100644 --- a/packages/medusa/src/api/routes/store/carts/update-payment-session.ts +++ b/packages/medusa/src/api/routes/store/carts/update-payment-session.ts @@ -2,6 +2,7 @@ import { IsObject } from "class-validator" import { defaultStoreCartFields, defaultStoreCartRelations } from "." import { CartService } from "../../../../services" import { validator } from "../../../../utils/validator" +import { decorateLineItemsWithTotals } from "./decorate-line-items-with-totals" /** * @oas [post] /carts/{id}/payment-sessions/{provider_id} @@ -41,8 +42,9 @@ export default async (req, res) => { select: defaultStoreCartFields, relations: defaultStoreCartRelations, }) + const data = await decorateLineItemsWithTotals(cart, req) - res.status(200).json({ cart }) + res.status(200).json({ cart: data }) } export class StorePostCartsCartPaymentSessionUpdateReq { diff --git a/packages/medusa/src/models/line-item.ts b/packages/medusa/src/models/line-item.ts index abad1cd5ad..2f92981cec 100644 --- a/packages/medusa/src/models/line-item.ts +++ b/packages/medusa/src/models/line-item.ts @@ -30,10 +30,7 @@ export class LineItem extends BaseEntity { @Column({ nullable: true }) cart_id: string - @ManyToOne( - () => Cart, - (cart) => cart.items - ) + @ManyToOne(() => Cart, (cart) => cart.items) @JoinColumn({ name: "cart_id" }) cart: Cart @@ -41,10 +38,7 @@ export class LineItem extends BaseEntity { @Column({ nullable: true }) order_id: string - @ManyToOne( - () => Order, - (order) => order.items - ) + @ManyToOne(() => Order, (order) => order.items) @JoinColumn({ name: "order_id" }) order: Order @@ -52,10 +46,7 @@ export class LineItem extends BaseEntity { @Column({ nullable: true }) swap_id: string - @ManyToOne( - () => Swap, - (swap) => swap.additional_items - ) + @ManyToOne(() => Swap, (swap) => swap.additional_items) @JoinColumn({ name: "swap_id" }) swap: Swap @@ -63,27 +54,16 @@ export class LineItem extends BaseEntity { @Column({ nullable: true }) claim_order_id: string - @ManyToOne( - () => ClaimOrder, - (co) => co.additional_items - ) + @ManyToOne(() => ClaimOrder, (co) => co.additional_items) @JoinColumn({ name: "claim_order_id" }) claim_order: ClaimOrder - @OneToMany( - () => LineItemTaxLine, - (tl) => tl.item, - { cascade: ["insert"] } - ) + @OneToMany(() => LineItemTaxLine, (tl) => tl.item, { cascade: ["insert"] }) tax_lines: LineItemTaxLine[] - @OneToMany( - () => LineItemAdjustment, - (lia) => lia.item, - { - cascade: ["insert"], - } - ) + @OneToMany(() => LineItemAdjustment, (lia) => lia.item, { + cascade: ["insert"], + }) adjustments: LineItemAdjustment[] @Column() @@ -136,7 +116,14 @@ export class LineItem extends BaseEntity { @DbAwareColumn({ type: "jsonb", nullable: true }) metadata: Record - refundable: number | null + refundable?: number | null + subtotal?: number | null + tax_total?: number | null + total?: number | null + original_total?: number | null + original_tax_total?: number | null + discount_total?: number | null + gift_card_total?: number | null @BeforeInsert() private beforeInsert(): void { diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index bcdf70e698..82e16e301b 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -1,4 +1,4 @@ -import _ from "lodash" +import { isEmpty, isEqual } from "lodash" import { MedusaError, Validator } from "medusa-core-utils" import { DeepPartial, EntityManager, In } from "typeorm" import { TransactionBaseService } from "../interfaces" @@ -546,7 +546,7 @@ class CartService extends TransactionBaseService { if (lineItem.should_merge) { currentItem = cart.items.find((item) => { if (item.should_merge && item.variant_id === lineItem.variant_id) { - return _.isEqual(item.metadata, lineItem.metadata) + return isEqual(item.metadata, lineItem.metadata) } return false }) @@ -1786,7 +1786,7 @@ class CartService extends TransactionBaseService { let updated = { ...shippingAddress } // If the country code of a shipping address is set we need to clear it - if (!_.isEmpty(shippingAddress) && shippingAddress.country_code) { + if (!isEmpty(shippingAddress) && shippingAddress.country_code) { updated = { ...updated, country_code: null, @@ -1938,6 +1938,7 @@ class CartService extends TransactionBaseService { "region.tax_rates", ], }) + const calculationContext = this.totalsService_ .withTransaction(transactionManager) .getCalculationContext(cart) diff --git a/packages/medusa/src/services/totals.ts b/packages/medusa/src/services/totals.ts index 53322f14bc..ad2a327bd0 100644 --- a/packages/medusa/src/services/totals.ts +++ b/packages/medusa/src/services/totals.ts @@ -689,7 +689,6 @@ class TotalsService extends BaseService { const calculationContext = this.getCalculationContext(cartOrOrder, { exclude_shipping: true, }) - const lineItemAllocation = calculationContext.allocation_map[lineItem.id] || {}