feat(medusa) allow querying category descendants with a param in list endpoint (#3321)

What:

Allowing the list endpoint to return a full tree when requested. 

Why:

When scoped with parent_category_id=null and include_descendant_tree=true, the query cost is fairly low. This allows for fast querying and prevent FE from building out the entire tree from a flat list repeatedly. By default, it is set to false, so this should be an intentional change knowing the costs of doing it for the entire result set. 

How:

When include_descendants_tree is included in the request parameter or the service parameter, we do a loop on results of product categories and do a call to fetch the descendants of that product category. 

RESOLVES CORE-1128
This commit is contained in:
Riqwan Thamir
2023-02-24 09:46:52 +01:00
committed by GitHub
parent ad7f56506f
commit 6323868f65
6 changed files with 102 additions and 11 deletions
@@ -124,21 +124,23 @@ describe("/admin/product-categories", () => {
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
name: "Mens",
handle: "mens",
})
productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "sweater",
handle: "sweater",
parent_category: productCategoryParent,
is_internal: true,
})
productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
name: "cashmere",
handle: "cashmere",
parent_category: productCategory,
})
productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
name: "specific cashmere",
parent_category: productCategoryChild,
})
})
afterEach(async () => {
@@ -155,7 +157,7 @@ describe("/admin/product-categories", () => {
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(3)
expect(response.data.count).toEqual(4)
expect(response.data.offset).toEqual(0)
expect(response.data.limit).toEqual(100)
expect(response.data.product_categories).toEqual(
@@ -185,6 +187,17 @@ describe("/admin/product-categories", () => {
parent_category: expect.objectContaining({
id: productCategory.id,
}),
category_children: [
expect.objectContaining({
id: productCategoryChild2.id,
})
],
}),
expect.objectContaining({
id: productCategoryChild2.id,
parent_category: expect.objectContaining({
id: productCategoryChild.id,
}),
category_children: [],
}),
])
@@ -238,6 +251,41 @@ describe("/admin/product-categories", () => {
expect(nullCategoryResponse.data.count).toEqual(1)
expect(nullCategoryResponse.data.product_categories[0].id).toEqual(productCategoryParent.id)
})
it("adds all descendants to categories in a nested way", async () => {
const api = useApi()
const response = await api.get(
`/admin/product-categories?parent_category_id=null&include_descendants_tree=true`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.product_categories).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: productCategoryParent.id,
category_children: [
expect.objectContaining({
id: productCategory.id,
category_children: [
expect.objectContaining({
id: productCategoryChild.id,
category_children: [
expect.objectContaining({
id: productCategoryChild2.id,
category_children: []
})
],
})
]
})
],
}),
])
)
})
})
describe("POST /admin/product-categories", () => {