Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB-3240 Fix booking window validation #116

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions server/routes/prisons/configuration/prisonConfigController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export default class PrisonConfigController {
const prison = await this.prisonService.getPrison(res.locals.user.username, prisonId)

const formValues = {
policyNoticeDaysMin: prison.policyNoticeDaysMin,
policyNoticeDaysMax: prison.policyNoticeDaysMax,
policyNoticeDaysMin: prison.policyNoticeDaysMin.toString(),
policyNoticeDaysMax: prison.policyNoticeDaysMax.toString(),
...req.flash('formValues')?.[0],
}

Expand Down Expand Up @@ -90,7 +90,8 @@ export default class PrisonConfigController {
return res.redirect(originalUrl)
}

const { policyNoticeDaysMin, policyNoticeDaysMax } = req.body
const { policyNoticeDaysMin, policyNoticeDaysMax }: { policyNoticeDaysMin: number; policyNoticeDaysMax: number } =
req.body

try {
await this.prisonService.updatePrisonDetails(res.locals.user.username, prisonId, {
Expand All @@ -113,15 +114,18 @@ export default class PrisonConfigController {
body('policyNoticeDaysMin')
.trim()
.toInt()
.isInt({ min: 0 })
.isInt({ min: 1 })
.withMessage('Enter a min booking window value of at least 1'),
body('policyNoticeDaysMax')
.trim()
.toInt()
.isInt({ min: 0 })
.isInt({ min: 1 })
.withMessage('Enter a max booking window value of at least 1'),
body(['policyNoticeDaysMin']).custom((_value, { req }) => {
const { policyNoticeDaysMin, policyNoticeDaysMax } = req.body
const {
policyNoticeDaysMin,
policyNoticeDaysMax,
}: { policyNoticeDaysMin: number; policyNoticeDaysMax: number } = req.body
if (policyNoticeDaysMin > policyNoticeDaysMax) {
throw new Error('Enter a Min window less than or equal to the Max')
}
Expand Down
Loading