Skip to content

Commit

Permalink
feat(medusa): Delete a condition should be idempotent on discount and…
Browse files Browse the repository at this point in the history
… condition
  • Loading branch information
adrien2p committed Oct 13, 2022
1 parent 0debdd0 commit 1ca41ce
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions packages/medusa/src/api/routes/admin/discounts/delete-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,27 @@ export default async (req, res) => {
.retrieve(condition_id)
.catch(() => void 0)

const defaultRes = {
id: condition_id,
object: "discount-condition",
deleted: true,
}

if (!condition) {
// resolves idempotently in case of non-existing condition
return res.json({
id: condition_id,
object: "discount-condition",
deleted: true,
})
return res.json(defaultRes)
}

let discount = await discountService.retrieve(discount_id, {
select: ["id", "rule_id"],
})
let discount = await discountService
.retrieve(discount_id, {
select: ["id", "rule_id"],
})
.catch(() => void 0)

if (!discount) {
// resolves idempotently in case of non-existing condition
return res.json(defaultRes)
}

if (condition.discount_rule_id !== discount.rule_id) {
throw new MedusaError(
Expand All @@ -112,9 +121,7 @@ export default async (req, res) => {
discount = await discountService.retrieve(discount_id, req.retrieveConfig)

res.json({
id: condition_id,
object: "discount-condition",
deleted: true,
...defaultRes,
discount,
})
}
Expand Down

0 comments on commit 1ca41ce

Please sign in to comment.