diff --git a/.changeset/spotty-queens-look.md b/.changeset/spotty-queens-look.md new file mode 100644 index 0000000000..0c8c2ec070 --- /dev/null +++ b/.changeset/spotty-queens-look.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix: Custom repository take/skip wrongly applied when there is no relations in the findWihtRelationAndCount diff --git a/packages/medusa/src/repositories/customer-group.ts b/packages/medusa/src/repositories/customer-group.ts index f9971a8536..3605696660 100644 --- a/packages/medusa/src/repositories/customer-group.ts +++ b/packages/medusa/src/repositories/customer-group.ts @@ -114,10 +114,14 @@ export class CustomerGroupRepository extends Repository { } if (relations.length === 0) { - const toReturn = await this.findByIds( - entitiesIds, - idsOrOptionsWithoutRelations - ) + const options = { ...idsOrOptionsWithoutRelations } + + // Since we are finding by the ids that have been retrieved above and those ids are already + // applying skip/take. Remove those options to avoid getting no results + delete options.skip + delete options.take + + const toReturn = await this.findByIds(entitiesIds, options) return [toReturn, toReturn.length] } diff --git a/packages/medusa/src/repositories/product-variant.ts b/packages/medusa/src/repositories/product-variant.ts index 41814b7041..5f4fe82e08 100644 --- a/packages/medusa/src/repositories/product-variant.ts +++ b/packages/medusa/src/repositories/product-variant.ts @@ -150,9 +150,18 @@ export class ProductVariantRepository extends Repository { } if (relations.length === 0) { + const options = { ...idsOrOptionsWithoutRelations } + + // Since we are finding by the ids that have been retrieved above and those ids are already + // applying skip/take. Remove those options to avoid getting no results + if (typeof options === "object") { + delete (options as FindWithRelationsOptions).skip + delete (options as FindWithRelationsOptions).take + } + const toReturn = await this.findByIds( entitiesIds, - idsOrOptionsWithoutRelations as FindConditions + options as FindConditions ) return [toReturn, count] } @@ -198,9 +207,18 @@ export class ProductVariantRepository extends Repository { } if (relations.length === 0) { + const options = { ...idsOrOptionsWithoutRelations } + + // Since we are finding by the ids that have been retrieved above and those ids are already + // applying skip/take. Remove those options to avoid getting no results + if (typeof options === "object") { + delete (options as FindWithRelationsOptions).skip + delete (options as FindWithRelationsOptions).take + } + return await this.findByIds( entitiesIds, - idsOrOptionsWithoutRelations as FindConditions + options as FindConditions ) } diff --git a/packages/medusa/src/repositories/product.ts b/packages/medusa/src/repositories/product.ts index e3c1b70b0c..22f7cc7136 100644 --- a/packages/medusa/src/repositories/product.ts +++ b/packages/medusa/src/repositories/product.ts @@ -1,7 +1,17 @@ import { flatten, groupBy, map, merge } from "lodash" -import { Brackets, EntityRepository, FindOperator, In, Repository, } from "typeorm" +import { + Brackets, + EntityRepository, + FindOperator, + In, + Repository, +} from "typeorm" import { PriceList, Product, SalesChannel } from "../models" -import { ExtendedFindConfig, Selector, WithRequiredProperty, } from "../types/common" +import { + ExtendedFindConfig, + Selector, + WithRequiredProperty, +} from "../types/common" import { applyOrdering } from "../utils/repository" export type ProductSelector = Omit, "tags"> & { @@ -228,10 +238,14 @@ export class ProductRepository extends Repository { } if (relations.length === 0) { - const toReturn = await this.findByIds( - entitiesIds, - idsOrOptionsWithoutRelations - ) + const options = { ...idsOrOptionsWithoutRelations } + + // Since we are finding by the ids that have been retrieved above and those ids are already + // applying skip/take. Remove those options to avoid getting no results + delete options.skip + delete options.take + + const toReturn = await this.findByIds(entitiesIds, options) return [toReturn, toReturn.length] }