Skip to content

Commit

Permalink
chore: move to hard delete + safely traverse invalid ID
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Jan 10, 2023
1 parent dfb16b0 commit 2a32017
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
19 changes: 9 additions & 10 deletions integration-tests/api/__tests__/admin/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,18 @@ describe("/admin/product-categories", () => {
return await db.teardown()
})

it("throws a not found error with an invalid ID", async () => {
it("returns successfully with an invalid ID", async () => {
const api = useApi()

const error = await api.delete(
const response = await api.delete(
`/admin/product-categories/invalid-id`,
adminHeaders
).catch(e => e)

expect(error.response.status).toEqual(404)
expect(error.response.data.type).toEqual("not_found")
expect(error.response.data.message).toEqual(
"ProductCategory with id: invalid-id was not found"
)

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 () => {
Expand All @@ -285,13 +284,13 @@ describe("/admin/product-categories", () => {
)
})

it("soft deletes a product category with no children successfully", async () => {
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)
Expand Down
17 changes: 4 additions & 13 deletions packages/medusa/src/services/__tests__/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,6 @@ describe("ProductCategoryService", () => {
findDescendantsTree: (productCategory) => {
return Promise.resolve(productCategory)
},
softRemove: (productCategory) => {
return Promise.resolve(productCategory)
}
})

const productCategoryService = new ProductCategoryService({
Expand All @@ -138,21 +135,15 @@ describe("ProductCategoryService", () => {
IdMap.getId("jeans")
)

expect(productCategoryRepository.softRemove).toBeCalledTimes(1)
expect(productCategoryRepository.softRemove).toBeCalledWith({
id: IdMap.getId("jeans"),
category_children: [],
})
expect(productCategoryRepository.delete).toBeCalledTimes(1)
expect(productCategoryRepository.delete).toBeCalledWith(IdMap.getId("jeans"))
})

it("fails on not-found product category id", async () => {
it("returns without failure on not-found product category id", async () => {
const categoryResponse = await productCategoryService
.delete("not-found")
.catch((e) => e)

expect(categoryResponse.message).toBe(
`ProductCategory with id: not-found was not found`
)
expect(categoryResponse).toBe(undefined)
})

it("fails on product category with children", async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/services/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ProductCategoryService extends TransactionBaseService {

const productCategory = await this.retrieve(productCategoryId, {
relations: ["category_children"],
})
}).catch((err) => void 0)

if (!productCategory) {
return Promise.resolve()
Expand All @@ -125,7 +125,7 @@ class ProductCategoryService extends TransactionBaseService {
)
}

await productCategoryRepository.softRemove(productCategory)
await productCategoryRepository.delete(productCategory.id)

return Promise.resolve()
})
Expand Down

0 comments on commit 2a32017

Please sign in to comment.