From e4af9685313077ece7e3fb7bd27053108cd9d5f8 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Mon, 9 Jan 2023 10:54:25 +0100 Subject: [PATCH] fix(medusa): Custom repository take/skip when called without relations (#2962) **What** The actual behavior is that the skip/take is applied in a first query returning the correct number of results. When there is no relations, we are calling findByIds with the previously retrieved ids, but we also pass skip/take which means that no result are returned after the first increment of the skip. The skip/take should not be applied in that case as it has already been applied. FIXES CORE-970 --- .changeset/spotty-queens-look.md | 5 ++++ .../medusa/src/repositories/customer-group.ts | 12 ++++++--- .../src/repositories/product-variant.ts | 22 ++++++++++++++-- packages/medusa/src/repositories/product.ts | 26 ++++++++++++++----- 4 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 .changeset/spotty-queens-look.md 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] }