chore: add a validation in the service layer to conform to 1 region rule

This commit is contained in:
Riqwan Thamir
2022-12-07 10:49:40 +01:00
committed by Adrien de Peretti
parent 7fcf2b86a9
commit 40694bd5d1
2 changed files with 25 additions and 1 deletions

View File

@@ -9,7 +9,6 @@ const featureFlagRouter = new FlagRouter({})
describe("DiscountService", () => {
describe("create", () => {
const discountRepository = MockRepository({})
const discountRuleRepository = MockRepository({})
const regionService = {
@@ -54,6 +53,24 @@ describe("DiscountService", () => {
}
})
it("fails to create a discount without regions", async () => {
expect.assertions(3)
try {
await discountService.create({
code: "test",
rule: {
type: "fixed",
allocation: "total",
value: 20,
},
})
} catch (err) {
expect(err.type).toEqual("invalid_data")
expect(err.message).toEqual("Discount must have atleast 1 region")
expect(discountRepository.create).toHaveBeenCalledTimes(0)
}
})
it("successfully creates discount", async () => {
await discountService.create({
code: "test",

View File

@@ -212,6 +212,13 @@ class DiscountService extends TransactionBaseService {
)) as Region[]
}
if (isEmpty(discount.regions)) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
"Discount must have atleast 1 region"
)
}
const discountRule = ruleRepo.create(validatedRule)
const createdDiscountRule = await ruleRepo.save(discountRule)