Skip to content

Commit

Permalink
PP-13312 reuse sanitisedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
james-peacock-gds committed Dec 19, 2024
1 parent a248bc0 commit 846fee9
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ async function post (req, res, next) {
const accountType = req.account.type
const currentAcceptedCardTypeIds = req.body.currentAcceptedCardTypeIds?.split(',') || []

let selectedCardTypeIds
const sanitiseToArray = value => Array.isArray(value) ? value : (value ? [value] : [])
const validations = [
body('debit').customSanitizer(sanitiseToArray),
body('credit').customSanitizer(sanitiseToArray),
body()
.custom((_, { req }) => (req.body.debit?.length ?? 0) + (req.body.credit?.length ?? 0) > 0)
.withMessage('You must choose at least one card')
.custom((_, { req }) => {
selectedCardTypeIds = sanitiseToArray(req.body.debit).concat(sanitiseToArray(req.body.credit))
if (selectedCardTypeIds < 1) {
throw new Error('You must choose at least one card')
}
return true
})
]
await Promise.all(validations.map(validation => validation.run(req)))
const validationErrors = validationResult(req)
Expand All @@ -56,7 +62,6 @@ async function post (req, res, next) {
})
}

const selectedCardTypeIds = sanitiseToArray(req.body.debit).concat(sanitiseToArray(req.body.credit))
const noChangesToAcceptedCardTypes = (
currentAcceptedCardTypeIds.length === selectedCardTypeIds.length &&
currentAcceptedCardTypeIds.every(item => selectedCardTypeIds.includes(item))
Expand Down

0 comments on commit 846fee9

Please sign in to comment.