chore: add a validation in the service layer to conform to 1 region rule
This commit is contained in:
1 parent
7fcf2b86a9
commit
40694bd5d1
2 files changed
+25
-1
No files matched your search
@@ -9,7 +9,6 @@ const featureFlagRouter = new FlagRouter({})
|
|||||||
describe("DiscountService", () => {
|
describe("DiscountService", () => {
|
||||||
describe("create", () => {
|
describe("create", () => {
|
||||||
const discountRepository = MockRepository({})
|
const discountRepository = MockRepository({})
|
||||||
|
|
||||||
const discountRuleRepository = MockRepository({})
|
const discountRuleRepository = MockRepository({})
|
||||||
|
|
||||||
const regionService = {
|
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 () => {
|
it("successfully creates discount", async () => {
|
||||||
await discountService.create({
|
await discountService.create({
|
||||||
code: "test",
|
code: "test",
|
||||||
|
|||||||
@@ -212,6 +212,13 @@ class DiscountService extends TransactionBaseService {
|
|||||||
)) as Region[]
|
)) 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 discountRule = ruleRepo.create(validatedRule)
|
||||||
const createdDiscountRule = await ruleRepo.save(discountRule)
|
const createdDiscountRule = await ruleRepo.save(discountRule)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user