fix(medusa): category list api bug where limit skews results (#3914)

* chore: fix category list api bug where limit skews results

* chore: add limits to integration test
This commit is contained in:
Riqwan Thamir authored and GitHub committed 2023-04-24 19:16:58 +02:00
1 parent 88a224e42a
commit 935abeae68
4 files changed
+60 -40

No files matched your search

@@ -51,7 +51,9 @@ export const ProductCategoryRepository = dataSource
const modelColumns = this.metadata.ownColumns.map(
(column) => column.propertyName
)
const selectColumns = columnsSelected.length ? columnsSelected : modelColumns
const selectColumns = columnsSelected.length
? columnsSelected
: modelColumns
return selectColumns.map((column) => {
return `${relationName}.${column}`
@@ -69,13 +71,16 @@ export const ProductCategoryRepository = dataSource
delete options_.where?.name
delete options_.where?.handle
options_.where = [{
...options_.where,
name: ILike(`%${q}%`)
}, {
...options_.where,
handle: ILike(`%${q}%`)
}]
options_.where = [
{
...options_.where,
name: ILike(`%${q}%`),
},
{
...options_.where,
handle: ILike(`%${q}%`),
},
]
}
queryBuilder.where(options_.where)
@@ -97,8 +102,6 @@ export const ProductCategoryRepository = dataSource
treeScope
)
.addSelect(fetchSelectColumns(treeRelation))
.addOrderBy(`${treeRelation}.rank`, "ASC")
.addOrderBy(`${treeRelation}.handle`, "ASC")
})
const nonTreeRelations: string[] = relationsSelected.filter(
@@ -111,15 +114,15 @@ export const ProductCategoryRepository = dataSource
let [categories, count] = await queryBuilder.getManyAndCount()
if (includeTree) {
categories = await Promise.all(
categories.map(async (productCategory) => {
categories = await Promise.all(
categories.map(async (productCategory) => {
if (includeTree) {
productCategory = await this.findDescendantsTree(productCategory)
}
return sortChildren(productCategory, treeScope)
})
)
}
return sortChildren(productCategory, treeScope)
})
)
return [categories, count]
},
@@ -166,13 +169,15 @@ const scopeChildren = (
return category
}
category.category_children = category.category_children.filter(
(categoryChild) => {
return !Object.entries(treeScope).some(
([attribute, value]) => categoryChild[attribute] !== value
)
}
)
if (category.category_children) {
category.category_children = category.category_children.filter(
(categoryChild) => {
return !Object.entries(treeScope).some(
([attribute, value]) => categoryChild[attribute] !== value
)
}
)
}
return category
}
@@ -182,13 +187,23 @@ const sortChildren = (
treeScope: QuerySelector<ProductCategory> = {}
): ProductCategory => {
if (category.category_children) {
category.category_children = category?.category_children
category.category_children = category.category_children
.map(
// Before we sort the children, we need scope the children
// to conform to treeScope conditions
(child) => sortChildren(scopeChildren(child, treeScope), treeScope)
)
.sort((a, b) => a.rank - b.rank)
.sort((a, b) => {
// Sort by rank first
const rankDiff = a.rank - b.rank
// If the ranks are the same, sort by handle in ascending order
if (rankDiff === 0) {
return a.handle.localeCompare(b.handle)
}
return rankDiff
})
}
return category