fix: regions are changed from an optional to required field

A discount should always be associated to atleast 1 region - we validate it against the region in the cart service.

Adding a request parameter validation to pass in atleast one regionID in an array of regions will ensure that regionID will be validated as a part of the discount service and fail if its not a valid region ID.
This commit is contained in:
Riqwan Thamir
2022-12-06 14:41:53 +01:00
committed by Adrien de Peretti
parent 15c667fbd3
commit d4e565b0b9
2 changed files with 54 additions and 3 deletions

View File

@@ -16,6 +16,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
starts_at: "02/02/2021 13:45",
ends_at: "03/14/2021 04:30",
},
@@ -41,6 +42,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
starts_at: new Date("02/02/2021 13:45"),
ends_at: new Date("03/14/2021 04:30"),
is_disabled: false,
@@ -63,6 +65,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
starts_at: "02/02/2021 13:45",
is_dynamic: true,
valid_duration: "PaMT2D",
@@ -100,6 +103,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
starts_at: "02/02/2021 13:45",
is_dynamic: true,
valid_duration: "P1Y2M03DT04H05M",
@@ -126,6 +130,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
starts_at: new Date("02/02/2021 13:45"),
is_disabled: false,
is_dynamic: true,
@@ -146,6 +151,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
},
adminSession: {
jwt: {
@@ -186,6 +192,7 @@ describe("POST /admin/discounts", () => {
},
],
},
regions: [IdMap.getId("region-france")],
starts_at: "02/02/2021 13:45",
is_dynamic: true,
valid_duration: "P1Y2M03DT04H05M",
@@ -222,6 +229,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
ends_at: "02/02/2021",
starts_at: "03/14/2021",
},
@@ -259,6 +267,7 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
starts_at: "03/14/2021 14:30",
},
adminSession: {
@@ -284,8 +293,48 @@ describe("POST /admin/discounts", () => {
value: 10,
allocation: "total",
},
regions: [IdMap.getId("region-france")],
starts_at: new Date("03/14/2021 14:30"),
})
})
})
describe("unsuccessful creation with invalid regions", () => {
let subject
beforeAll(async () => {
jest.clearAllMocks()
subject = await request("POST", "/admin/discounts", {
payload: {
code: "TEST",
rule: {
description: "Test",
type: "fixed",
value: 10,
allocation: "total",
},
},
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
})
})
it("returns 400", () => {
expect(subject.status).toEqual(400)
})
it("returns error", () => {
expect(subject.body.message).toEqual(
`each value in regions must be a string, regions must be an array`
)
})
it("does not call service create", () => {
expect(DiscountServiceMock.create).toHaveBeenCalledTimes(0)
})
})
})

View File

@@ -42,6 +42,7 @@ import { FindParams } from "../../../../types/common"
* required:
* - code
* - rule
* - regions
* properties:
* code:
* type: string
@@ -151,6 +152,7 @@ import { FindParams } from "../../../../types/common"
* value: 10,
* allocation: AllocationType.ITEM
* },
* regions: ['reg_XXXXXXXX'],
* is_dynamic: false,
* is_disabled: false
* })
@@ -169,7 +171,8 @@ import { FindParams } from "../../../../types/common"
* "type": "fixed",
* "value": 10,
* "allocation": "item"
* }
* },
* "regions": ['reg_XXXXXXXX']
* }'
* security:
* - api_token: []
@@ -257,9 +260,8 @@ export class AdminPostDiscountsReq {
usage_limit?: number
@IsArray()
@IsOptional()
@IsString({ each: true })
regions?: string[]
regions: string[]
@IsObject()
@IsOptional()