diff --git a/.changeset/hungry-papayas-raise.md b/.changeset/hungry-papayas-raise.md new file mode 100644 index 0000000000..fbd0c74652 --- /dev/null +++ b/.changeset/hungry-papayas-raise.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): fix rank order changing on category update diff --git a/integration-tests/api/__tests__/admin/product-category.ts b/integration-tests/api/__tests__/admin/product-category.ts index 321f3175ca..8717a33fba 100644 --- a/integration-tests/api/__tests__/admin/product-category.ts +++ b/integration-tests/api/__tests__/admin/product-category.ts @@ -626,6 +626,35 @@ describe("/admin/product-categories", () => { expect(errorFetchingDeleted.response.status).toEqual(404) }) + + it("deleting a product category reorders siblings accurately", async () => { + const api = useApi() + + const deleteResponse = await api.delete( + `/admin/product-categories/${productCategory.id}`, + adminHeaders + ).catch(e => e) + + expect(deleteResponse.status).toEqual(200) + + const siblingsResponse = await api.get( + `/admin/product-categories?parent_category_id=${productCategoryParent.id}`, + adminHeaders + ).catch(e => e) + + expect(siblingsResponse.data.product_categories).toEqual( + [ + expect.objectContaining({ + id: productCategory1.id, + rank: 0 + }), + expect.objectContaining({ + id: productCategory2.id, + rank: 1 + }), + ] + ) + }) }) describe("POST /admin/product-categories/:id", () => { @@ -779,6 +808,22 @@ describe("/admin/product-categories", () => { ) }) + it("updating properties other than rank should not change its rank", async () => { + const api = useApi() + + expect(productCategory.rank).toEqual(0) + + const response = await api.post( + `/admin/product-categories/${productCategory.id}`, + { + name: "different-name", + }, + adminHeaders + ) + expect(response.status).toEqual(200) + expect(response.data.product_category.rank).toEqual(productCategory.rank) + }) + it("root parent returns children correctly on updating new category", async () => { const api = useApi() diff --git a/packages/medusa/src/services/product-category.ts b/packages/medusa/src/services/product-category.ts index 297fcbb3f3..a17234bc9c 100644 --- a/packages/medusa/src/services/product-category.ts +++ b/packages/medusa/src/services/product-category.ts @@ -310,7 +310,9 @@ class ProductCategoryService extends TransactionBaseService { const targetRank = input.rank const shouldChangeParent = targetParentId !== undefined && targetParentId !== originalParentId - const shouldChangeRank = shouldChangeParent || originalRank !== targetRank + const shouldChangeRank = + shouldChangeParent || + (isDefined(targetRank) && originalRank !== targetRank) return { targetCategoryId: productCategory.id,