From 2a320179756e53993a0be683ba766fb4f45ec6ac Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Tue, 10 Jan 2023 17:40:33 +0530 Subject: [PATCH] chore: move to hard delete + safely traverse invalid ID --- .../api/__tests__/admin/product-category.ts | 19 +++++++++---------- .../services/__tests__/product-category.ts | 17 ++++------------- .../medusa/src/services/product-category.ts | 4 ++-- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/integration-tests/api/__tests__/admin/product-category.ts b/integration-tests/api/__tests__/admin/product-category.ts index fc9b0e796fa88..31ad71fc5ccab 100644 --- a/integration-tests/api/__tests__/admin/product-category.ts +++ b/integration-tests/api/__tests__/admin/product-category.ts @@ -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 () => { @@ -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) diff --git a/packages/medusa/src/services/__tests__/product-category.ts b/packages/medusa/src/services/__tests__/product-category.ts index 4ca56d953819e..6a97426557c80 100644 --- a/packages/medusa/src/services/__tests__/product-category.ts +++ b/packages/medusa/src/services/__tests__/product-category.ts @@ -121,9 +121,6 @@ describe("ProductCategoryService", () => { findDescendantsTree: (productCategory) => { return Promise.resolve(productCategory) }, - softRemove: (productCategory) => { - return Promise.resolve(productCategory) - } }) const productCategoryService = new ProductCategoryService({ @@ -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 () => { diff --git a/packages/medusa/src/services/product-category.ts b/packages/medusa/src/services/product-category.ts index 3a0d6f918d5c7..92d564763c759 100644 --- a/packages/medusa/src/services/product-category.ts +++ b/packages/medusa/src/services/product-category.ts @@ -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() @@ -125,7 +125,7 @@ class ProductCategoryService extends TransactionBaseService { ) } - await productCategoryRepository.softRemove(productCategory) + await productCategoryRepository.delete(productCategory.id) return Promise.resolve() })