fix(medusa): Reduce joins in cart retrieval (#3909)

* fix(medusa): Remove discounts.regions relation from cart retrieval

* Create violet-poets-rule.md

* fix service test

* update snapshot

---------

Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
Oliver Windall Juhl
2023-04-25 12:51:33 +02:00
committed by GitHub
parent 935abeae68
commit 3b3236cc01
8 changed files with 69 additions and 77 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix(medusa): Remove discounts.regions relation from cart retrieval

View File

@@ -547,17 +547,10 @@ Object {
"created_at": Any<Date>,
"currency_code": "usd",
"deleted_at": null,
"fulfillment_providers": Array [],
"gift_cards_taxable": true,
"id": Any<String>,
"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<Date>,
"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<Date>,
"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<Date>,
"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<Date>,
"currency_code": "usd",
"deleted_at": null,
"fulfillment_providers": Array [],
"gift_cards_taxable": true,
"id": Any<String>,
"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<Date>,
"currency_code": "usd",
"deleted_at": null,
"fulfillment_providers": Array [],
"gift_cards_taxable": true,
"id": Any<String>,
"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,

View File

@@ -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({

View File

@@ -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

View File

@@ -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: [],

View File

@@ -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)
})
}

View File

@@ -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,

View File

@@ -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))