fix(product): category tree breaks due to pagination limit (#7947)

what:

- when children scope beyond the pagination limit, we fail to load all descendants and ancestors
- we fix this by fetching all categories in the tree scoped by the where clause
This commit is contained in:
Riqwan Thamir
2024-07-04 14:58:21 +00:00
committed by GitHub
parent f49139b20f
commit 0d534d4f70
2 changed files with 132 additions and 23 deletions
@@ -133,6 +133,118 @@ moduleIntegrationTestRunner<IProductModuleService>({
}),
])
})
describe("with tree inclusion", () => {
let root, child1, child2, child1a, child2a, child2a1
beforeEach(async () => {
root = await service.createProductCategories({
name: "Root",
})
child1 = await service.createProductCategories({
name: "Child 1",
parent_category_id: root.id,
})
child1a = await service.createProductCategories({
name: "Child 1 a",
parent_category_id: child1.id,
})
child2 = await service.createProductCategories({
name: "Child 2",
parent_category_id: root.id,
})
child2a = await service.createProductCategories({
name: "Child 2 a",
parent_category_id: child2.id,
is_internal: true,
})
child2a1 = await service.createProductCategories({
name: "Child 2 a 1",
parent_category_id: child2a.id,
})
})
it("should return all descendants of a category", async () => {
const results = await service.listProductCategories(
{
id: root.id,
include_descendants_tree: true,
is_internal: false,
},
{
select: ["id"],
take: 1,
}
)
expect(results).toEqual([
expect.objectContaining({
id: root.id,
category_children: [
expect.objectContaining({
id: child1.id,
category_children: [
expect.objectContaining({ id: child1a.id }),
],
}),
expect.objectContaining({
id: child2.id,
// child2a & child2a1 should not show up as we're scoping by internal
category_children: [],
}),
],
}),
])
})
it("should return all ancestors of a category", async () => {
const results = await service.listProductCategories(
{
id: child1a.id,
include_ancestors_tree: true,
is_internal: false,
},
{
select: ["id"],
take: 1,
}
)
expect(results).toEqual([
expect.objectContaining({
id: child1a.id,
parent_category: expect.objectContaining({
id: child1.id,
parent_category: expect.objectContaining({ id: root.id }),
}),
}),
])
const results2 = await service.listProductCategories(
{
id: child2a1.id,
include_ancestors_tree: true,
is_internal: false,
},
{
select: ["id"],
take: 1,
}
)
// If the where query includes scoped categories, we hide from the tree
expect(results2).toEqual([
expect.objectContaining({
id: child2a1.id,
parent_category: undefined,
}),
])
})
})
})
describe("listAndCountCategories", () => {