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
This commit is contained in:
Adrien de Peretti
2023-01-09 09:54:25 +00:00
committed by GitHub
parent b280e53bd3
commit e4af968531
4 changed files with 53 additions and 12 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
fix: Custom repository take/skip wrongly applied when there is no relations in the findWihtRelationAndCount
@@ -114,10 +114,14 @@ export class CustomerGroupRepository extends Repository<CustomerGroup> {
} }
if (relations.length === 0) { if (relations.length === 0) {
const toReturn = await this.findByIds( const options = { ...idsOrOptionsWithoutRelations }
entitiesIds,
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] return [toReturn, toReturn.length]
} }
@@ -150,9 +150,18 @@ export class ProductVariantRepository extends Repository<ProductVariant> {
} }
if (relations.length === 0) { 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( const toReturn = await this.findByIds(
entitiesIds, entitiesIds,
idsOrOptionsWithoutRelations as FindConditions<ProductVariant> options as FindConditions<ProductVariant>
) )
return [toReturn, count] return [toReturn, count]
} }
@@ -198,9 +207,18 @@ export class ProductVariantRepository extends Repository<ProductVariant> {
} }
if (relations.length === 0) { 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( return await this.findByIds(
entitiesIds, entitiesIds,
idsOrOptionsWithoutRelations as FindConditions<ProductVariant> options as FindConditions<ProductVariant>
) )
} }
+20 -6
View File
@@ -1,7 +1,17 @@
import { flatten, groupBy, map, merge } from "lodash" 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 { PriceList, Product, SalesChannel } from "../models"
import { ExtendedFindConfig, Selector, WithRequiredProperty, } from "../types/common" import {
ExtendedFindConfig,
Selector,
WithRequiredProperty,
} from "../types/common"
import { applyOrdering } from "../utils/repository" import { applyOrdering } from "../utils/repository"
export type ProductSelector = Omit<Selector<Product>, "tags"> & { export type ProductSelector = Omit<Selector<Product>, "tags"> & {
@@ -228,10 +238,14 @@ export class ProductRepository extends Repository<Product> {
} }
if (relations.length === 0) { if (relations.length === 0) {
const toReturn = await this.findByIds( const options = { ...idsOrOptionsWithoutRelations }
entitiesIds,
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] return [toReturn, toReturn.length]
} }