Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Oct 13, 2022
1 parent 1ca41ce commit 939af44
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
15 changes: 15 additions & 0 deletions integration-tests/api/__tests__/admin/discount.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
34 changes: 16 additions & 18 deletions packages/medusa/src/api/routes/admin/discounts/delete-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
})
}
Expand Down

0 comments on commit 939af44

Please sign in to comment.