fix(medusa): fix rank order changing on category update (#3486)
what: - fixes issue where ranking changes when only properties other than rank are updated on categories FIXES CORE-1253
This commit is contained in:
5
.changeset/hungry-papayas-raise.md
Normal file
5
.changeset/hungry-papayas-raise.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix(medusa): fix rank order changing on category update
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user