From 939af44094346bd231840253ddc405c593e3d4cd Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 13 Oct 2022 13:00:44 +0200 Subject: [PATCH] revert --- .../api/__tests__/admin/discount.js | 15 ++++++++ .../admin/discounts/delete-condition.ts | 34 +++++++++---------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/integration-tests/api/__tests__/admin/discount.js b/integration-tests/api/__tests__/admin/discount.js index 5640a9ff82481..6b79a4629c875 100644 --- a/integration-tests/api/__tests__/admin/discount.js +++ b/integration-tests/api/__tests__/admin/discount.js @@ -1574,6 +1574,21 @@ describe("/admin/discounts", () => { }) ) }) + + it("should fail if discount does not exist", async () => { + const api = useApi() + + const err = await api + .delete( + "/admin/discounts/not-exist/conditions/test-condition", + adminReqConfig + ) + .catch((e) => e) + + expect(err.response.data.message).toBe( + "Discount with id not-exist was not found" + ) + }) }) describe("POST /admin/discounts/:id/conditions", () => { diff --git a/packages/medusa/src/api/routes/admin/discounts/delete-condition.ts b/packages/medusa/src/api/routes/admin/discounts/delete-condition.ts index 0b958509a3ee5..b0e6b9c69076b 100644 --- a/packages/medusa/src/api/routes/admin/discounts/delete-condition.ts +++ b/packages/medusa/src/api/routes/admin/discounts/delete-condition.ts @@ -82,28 +82,24 @@ export default async (req, res) => { .retrieve(condition_id) .catch(() => void 0) - const defaultRes = { - id: condition_id, - object: "discount-condition", - deleted: true, - } - if (!condition) { + const discount = await discountService.retrieve( + discount_id, + req.retrieveConfig + ) // resolves idempotently in case of non-existing condition - return res.json(defaultRes) - } - - let discount = await discountService - .retrieve(discount_id, { - select: ["id", "rule_id"], + return res.json({ + id: condition_id, + object: "discount-condition", + deleted: true, + discount, }) - .catch(() => void 0) - - if (!discount) { - // resolves idempotently in case of non-existing condition - return res.json(defaultRes) } + let discount = await discountService.retrieve(discount_id, { + select: ["id", "rule_id"], + }) + if (condition.discount_rule_id !== discount.rule_id) { throw new MedusaError( MedusaError.Types.NOT_FOUND, @@ -121,7 +117,9 @@ export default async (req, res) => { discount = await discountService.retrieve(discount_id, req.retrieveConfig) res.json({ - ...defaultRes, + id: condition_id, + object: "discount-condition", + deleted: true, discount, }) }