diff --git a/.changeset/violet-poets-rule.md b/.changeset/violet-poets-rule.md new file mode 100644 index 0000000000..e80eee436c --- /dev/null +++ b/.changeset/violet-poets-rule.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Remove discounts.regions relation from cart retrieval 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 28206c6257..35e26ceb1e 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 @@ -547,17 +547,10 @@ Object { "created_at": Any, "currency_code": "usd", "deleted_at": null, - "fulfillment_providers": Array [], "gift_cards_taxable": true, "id": Any, "metadata": null, "name": "Test region", - "payment_providers": Array [ - PaymentProvider { - "id": "test-pay", - "is_installed": true, - }, - ], "tax_code": null, "tax_provider_id": null, "tax_rate": 12.5, @@ -903,17 +896,10 @@ Object { "created_at": Any, "currency_code": "usd", "deleted_at": null, - "fulfillment_providers": Array [], "gift_cards_taxable": true, "id": "test-region", "metadata": null, "name": "Test region", - "payment_providers": Array [ - PaymentProvider { - "id": "test-pay", - "is_installed": true, - }, - ], "tax_code": null, "tax_provider_id": null, "tax_rate": 12.5, @@ -1161,17 +1147,10 @@ Object { "created_at": Any, "currency_code": "usd", "deleted_at": null, - "fulfillment_providers": Array [], "gift_cards_taxable": true, "id": "test-region", "metadata": null, "name": "Test region", - "payment_providers": Array [ - PaymentProvider { - "id": "test-pay", - "is_installed": true, - }, - ], "tax_code": null, "tax_provider_id": null, "tax_rate": 12.5, @@ -1417,17 +1396,10 @@ Object { "created_at": Any, "currency_code": "usd", "deleted_at": null, - "fulfillment_providers": Array [], "gift_cards_taxable": true, "id": "test-region", "metadata": null, "name": "Test region", - "payment_providers": Array [ - PaymentProvider { - "id": "test-pay", - "is_installed": true, - }, - ], "tax_code": null, "tax_provider_id": null, "tax_rate": 12.5, @@ -1750,17 +1722,10 @@ Object { "created_at": Any, "currency_code": "usd", "deleted_at": null, - "fulfillment_providers": Array [], "gift_cards_taxable": true, "id": Any, "metadata": null, "name": "Test region", - "payment_providers": Array [ - PaymentProvider { - "id": "test-pay", - "is_installed": true, - }, - ], "tax_code": null, "tax_provider_id": null, "tax_rate": 12.5, @@ -2205,17 +2170,10 @@ Object { "created_at": Any, "currency_code": "usd", "deleted_at": null, - "fulfillment_providers": Array [], "gift_cards_taxable": true, "id": Any, "metadata": null, "name": "Test region", - "payment_providers": Array [ - PaymentProvider { - "id": "test-pay", - "is_installed": true, - }, - ], "tax_code": null, "tax_provider_id": null, "tax_rate": 12.5, diff --git a/packages/medusa/src/models/region.ts b/packages/medusa/src/models/region.ts index 90ff7daae6..9b3f0380bb 100644 --- a/packages/medusa/src/models/region.ts +++ b/packages/medusa/src/models/region.ts @@ -10,17 +10,17 @@ import { OneToMany, } from "typeorm" +import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity" +import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing" +import { DbAwareColumn } from "../utils/db-aware-column" +import { FeatureFlagColumn } from "../utils/feature-flag-decorators" +import { generateEntityId } from "../utils/generate-entity-id" import { Country } from "./country" import { Currency } from "./currency" -import { DbAwareColumn } from "../utils/db-aware-column" import { FulfillmentProvider } from "./fulfillment-provider" import { PaymentProvider } from "./payment-provider" -import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity" import { TaxProvider } from "./tax-provider" import { TaxRate } from "./tax-rate" -import { generateEntityId } from "../utils/generate-entity-id" -import { FeatureFlagColumn } from "../utils/feature-flag-decorators" -import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing" @Entity() export class Region extends SoftDeletableEntity { @@ -61,7 +61,6 @@ export class Region extends SoftDeletableEntity { tax_provider: TaxProvider @ManyToMany(() => PaymentProvider, { - eager: true, cascade: ["insert", "update"], }) @JoinTable({ @@ -78,7 +77,6 @@ export class Region extends SoftDeletableEntity { payment_providers: PaymentProvider[] @ManyToMany(() => FulfillmentProvider, { - eager: true, cascade: ["insert", "update"], }) @JoinTable({ diff --git a/packages/medusa/src/repositories/discount.ts b/packages/medusa/src/repositories/discount.ts index 4b1ffc7782..e703b53dfd 100644 --- a/packages/medusa/src/repositories/discount.ts +++ b/packages/medusa/src/repositories/discount.ts @@ -1,5 +1,5 @@ -import { Discount } from "../models" import { dataSource } from "../loaders/database" +import { Discount } from "../models" export const DiscountRepository = dataSource.getRepository(Discount) export default DiscountRepository diff --git a/packages/medusa/src/services/__tests__/cart.js b/packages/medusa/src/services/__tests__/cart.js index c2d83132f2..20f249f061 100644 --- a/packages/medusa/src/services/__tests__/cart.js +++ b/packages/medusa/src/services/__tests__/cart.js @@ -65,8 +65,7 @@ describe("CartService", () => { describe("retrieve", () => { let result const cartRepository = MockRepository({ - findOne: () => - Promise.resolve({ id: IdMap.getId("emptyCart") }), + findOne: () => Promise.resolve({ id: IdMap.getId("emptyCart") }), }) beforeAll(async () => { jest.clearAllMocks() @@ -83,13 +82,11 @@ describe("CartService", () => { it("calls cart model functions", () => { expect(cartRepository.findOne).toHaveBeenCalledTimes(1) - expect(cartRepository.findOne).toHaveBeenCalledWith( - { - where: { id: IdMap.getId("emptyCart") }, - select: undefined, - relations: undefined, - } - ) + expect(cartRepository.findOne).toHaveBeenCalledWith({ + where: { id: IdMap.getId("emptyCart") }, + select: undefined, + relations: undefined, + }) }) }) @@ -852,25 +849,24 @@ describe("CartService", () => { billing_address: true, customer: true, discounts: { - regions: true, - rule: true + rule: true, }, gift_cards: true, items: { variant: { - product: true - } + product: true, + }, }, payment_sessions: true, region: { countries: true }, shipping_address: true, - shipping_methods: true + shipping_methods: true, }, select: undefined, where: { - id: "withpays" - } - }), + id: "withpays", + }, + }) ) }) }) @@ -1271,6 +1267,25 @@ describe("CartService", () => { return this }, } + + const discountService = { + list: jest.fn().mockReturnValue( + Promise.resolve([ + { + id: IdMap.getId("stays"), + regions: [{ id: IdMap.getId("region-us") }], + }, + { + id: IdMap.getId("removes"), + regions: [], + }, + ]) + ), + withTransaction: function () { + return this + }, + } + const cartRepository = MockRepository({ findOne: () => Promise.resolve({ @@ -1325,6 +1340,7 @@ describe("CartService", () => { manager: MockManager, paymentProviderService, addressRepository, + discountService, totalsService, cartRepository, newTotalsService: newTotalsServiceMock, @@ -1377,9 +1393,11 @@ describe("CartService", () => { shipping_address: { country_code: "us", }, - items: [{ - id: IdMap.getId("testitem") - }], + items: [ + { + id: IdMap.getId("testitem"), + }, + ], payment_session: null, payment_sessions: [], gift_cards: [], diff --git a/packages/medusa/src/services/cart.ts b/packages/medusa/src/services/cart.ts index de1174cea9..d0ad660249 100644 --- a/packages/medusa/src/services/cart.ts +++ b/packages/medusa/src/services/cart.ts @@ -1,9 +1,9 @@ import { isEmpty, isEqual } from "lodash" -import { isDefined, MedusaError } from "medusa-core-utils" +import { MedusaError, isDefined } from "medusa-core-utils" import { DeepPartial, EntityManager, In, IsNull, Not } from "typeorm" import { - CustomerService, CustomShippingOptionService, + CustomerService, DiscountService, EventBusService, GiftCardService, @@ -26,8 +26,8 @@ import SalesChannelFeatureFlag from "../loaders/feature-flags/sales-channels" import { Address, Cart, - Customer, CustomShippingOption, + Customer, Discount, DiscountRule, DiscountRuleType, @@ -46,9 +46,9 @@ import { CartCreateProps, CartUpdateProps, FilterableCartProps, - isCart, LineItemUpdate, LineItemValidateData, + isCart, } from "../types/cart" import { AddressPayload, @@ -1064,7 +1064,6 @@ class CartService extends TransactionBaseService { "region.countries", "discounts", "discounts.rule", - "discounts.regions", ] if ( @@ -2382,7 +2381,16 @@ class CartService extends TransactionBaseService { } if (cart.discounts && cart.discounts.length) { - cart.discounts = cart.discounts.filter((discount) => { + const discounts = await this.discountService_ + .withTransaction(this.activeManager_) + .list( + { + id: [...cart.discounts.map(({ id }) => id)], + }, + { relations: ["rule", "regions"] } + ) + + cart.discounts = discounts.filter((discount) => { return discount.regions.find(({ id }) => id === regionId) }) } diff --git a/packages/medusa/src/services/discount.ts b/packages/medusa/src/services/discount.ts index 0f27454b9c..a2a17a2288 100644 --- a/packages/medusa/src/services/discount.ts +++ b/packages/medusa/src/services/discount.ts @@ -1,6 +1,6 @@ import { parse, toSeconds } from "iso8601-duration" import { isEmpty, omit } from "lodash" -import { isDefined, MedusaError } from "medusa-core-utils" +import { MedusaError, isDefined } from "medusa-core-utils" import { DeepPartial, EntityManager, diff --git a/packages/medusa/src/types/discount.ts b/packages/medusa/src/types/discount.ts index 83e8553b3b..24374975c0 100644 --- a/packages/medusa/src/types/discount.ts +++ b/packages/medusa/src/types/discount.ts @@ -16,6 +16,7 @@ import { Region, } from "../models" import { optionalBooleanMapper } from "../utils/validators/is-boolean" +import { IsType } from "../utils/validators/is-type" import { ExactlyOne } from "./validators/exactly-one" export type QuerySelector = { @@ -27,6 +28,10 @@ export class FilterableDiscountProps { @IsOptional() q?: string + @IsOptional() + @IsType([String, [String]]) + id?: string | string[] + @IsBoolean() @IsOptional() @Transform(({ value }) => optionalBooleanMapper.get(value))