From a501364b2d26a3ca0670eb345d9933b83964abc1 Mon Sep 17 00:00:00 2001 From: Hakim Saoudi Date: Tue, 23 Sep 2025 16:40:31 +0200 Subject: [PATCH] fix(product): Correctly fetch category descendants by handle (#13579) This PR fixes the issue #13518 where product category descendants were not retrieved when filtering by handle with the include_descendants_tree flag. The handle filter was not being correctly removed before the descendant tree query was executed. --- .changeset/cuddly-kiwis-roll.md | 5 ++++ .../admin/product-category.spec.ts | 27 +++++++++++++++++++ .../src/repositories/product-category.ts | 1 + 3 files changed, 33 insertions(+) create mode 100644 .changeset/cuddly-kiwis-roll.md diff --git a/.changeset/cuddly-kiwis-roll.md b/.changeset/cuddly-kiwis-roll.md new file mode 100644 index 0000000000..6f0db6c86c --- /dev/null +++ b/.changeset/cuddly-kiwis-roll.md @@ -0,0 +1,5 @@ +--- +"@medusajs/product": patch +--- + +fix(product): Correctly fetch category descendants by handle \ No newline at end of file diff --git a/integration-tests/http/__tests__/product-category/admin/product-category.spec.ts b/integration-tests/http/__tests__/product-category/admin/product-category.spec.ts index 8dd486f063..93a5dbfc75 100644 --- a/integration-tests/http/__tests__/product-category/admin/product-category.spec.ts +++ b/integration-tests/http/__tests__/product-category/admin/product-category.spec.ts @@ -598,6 +598,33 @@ medusaIntegrationTestRunner({ ) }) + it("should filter by handle and retrieve descendants tree", async () => { + const response = await api.get( + `/admin/product-categories?handle=${productCategoryParent.handle}&include_descendants_tree=true`, + adminHeaders + ) + + expect(response.status).toEqual(200) + expect(response.data.product_categories).toEqual([ + expect.objectContaining({ + id: productCategoryParent.id, + handle: productCategoryParent.handle, + category_children: [ + expect.objectContaining({ + id: productCategory.id, + handle: productCategory.handle, + category_children: [ + expect.objectContaining({ + id: productCategoryChild.id, + handle: productCategoryChild.handle, + }), + ], + }), + ], + }), + ]) + }) + it("filters based on free text on name and handle columns", async () => { const response = await api.get( `/admin/product-categories?q=men&limit=1`, diff --git a/packages/modules/product/src/repositories/product-category.ts b/packages/modules/product/src/repositories/product-category.ts index c5ce205272..1e62a7dde1 100644 --- a/packages/modules/product/src/repositories/product-category.ts +++ b/packages/modules/product/src/repositories/product-category.ts @@ -182,6 +182,7 @@ export class ProductCategoryRepository extends DALUtils.MikroOrmBaseTreeReposito } as MikroOptions delete where.id + delete where.handle delete where.mpath delete where.parent_category_id