feat(medusa): Nested Categories Admin Delete Endpoint (#2975)

**What:**

Introduces an admin endpoint that allows a user to delete a product category, given an ID.

Why:

This is part of a greater goal of allowing products to be added to multiple categories.

How:

- Creates a route on the admin scope to delete category
- Creates a method in product category services to delete a category

RESOLVES CORE-957
This commit is contained in:
Riqwan Thamir
2023-01-10 15:28:46 +01:00
committed by GitHub
parent f6c81dab9e
commit 71fa60892c
6 changed files with 263 additions and 1 deletions
@@ -217,7 +217,7 @@ describe("/admin/product-categories", () => {
const response = await api.get(
`/admin/product-categories?parent_category_id=${productCategoryParent.id}`,
adminHeaders,
).catch(e => e)
)
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
@@ -233,4 +233,76 @@ describe("/admin/product-categories", () => {
expect(nullCategoryResponse.data.product_categories[0].id).toEqual(productCategoryParent.id)
})
})
describe("DELETE /admin/product-categories/:id", () => {
beforeEach(async () => {
await adminSeeder(dbConnection)
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
name: "category parent",
handle: "category-parent",
})
productCategory = await simpleProductCategoryFactory(dbConnection, {
name: "category",
handle: "category",
parent_category: productCategoryParent,
})
})
afterEach(async () => {
const db = useDb()
return await db.teardown()
})
it("returns successfully with an invalid ID", async () => {
const api = useApi()
const response = await api.delete(
`/admin/product-categories/invalid-id`,
adminHeaders
)
expect(response.status).toEqual(200)
expect(response.data.id).toEqual("invalid-id")
expect(response.data.deleted).toBeTruthy()
expect(response.data.object).toEqual("product_category")
})
it("throws a not allowed error for a category with children", async () => {
const api = useApi()
const error = await api.delete(
`/admin/product-categories/${productCategoryParent.id}`,
adminHeaders
).catch(e => e)
expect(error.response.status).toEqual(400)
expect(error.response.data.type).toEqual("not_allowed")
expect(error.response.data.message).toEqual(
`Deleting ProductCategory (${productCategoryParent.id}) with category children is not allowed`
)
})
it("deletes a product category with no children successfully", async () => {
const api = useApi()
const deleteResponse = await api.delete(
`/admin/product-categories/${productCategory.id}`,
adminHeaders
).catch(e => e)
expect(deleteResponse.status).toEqual(200)
expect(deleteResponse.data.id).toEqual(productCategory.id)
expect(deleteResponse.data.deleted).toBeTruthy()
expect(deleteResponse.data.object).toEqual("product_category")
const errorFetchingDeleted = await api.get(
`/admin/product-categories/${productCategory.id}`,
adminHeaders
).catch(e => e)
expect(errorFetchingDeleted.response.status).toEqual(404)
})
})
})