Move pledge validation into new function

This is so we can reuse it in two places: while saving a pledge,
and while validating/verifying it.
This commit is contained in:
Hippo 2022-01-05 19:10:01 +05:30
parent 3e0e775af9
commit 87afc8688e

View file

@ -102,25 +102,24 @@ router.get('/', async (req, res) => {
})
})
router.post('/pledge', async (req, res) => {
if (DEBUG) console.debug('New pledge:', req.body)
// validate pledge
// function to validate pledges before saving
function validatePledge(body) {
// errors get saved here
let errors = []
let submit = req.body.submit
let submit = body.submit
if (submit != 'Save Pledge') {
errors.push("This request seems to have been tampered with. Are you sure it wasn't you doing the tampering?")
}
let robo = req.body.robo
let robo = body.robo
if(robo != 'no') {
errors.push('Only humans are allowed to donate money. Robots are too digital 🙁')
}
let amount = req.body.amount
let amount = body.amount
if (!amount || amount == 'custom') {
amount = req.body['amount-custom']
amount = body['amount-custom']
}
if (!amount || amount <= 0) {
errors.push('Pledge amount too small. Please choose at least a rupee!')
@ -131,37 +130,32 @@ router.post('/pledge', async (req, res) => {
errors.push('Invalid amount. Please choose a positive number!')
}
let name = req.body.name
let name = body.name
if (name.length <=0) {
errors.push('What is your name? You can be anonymous to the world but at least we should know...')
}
let anonymous = req.body.anonymous == 'on' ? true : false
let anonymous = body.anonymous == 'on' ? true : false
let email = req.body.email
let email = body.email
if (email.length < 5) {
errors.push('Please enter a valid email address')
}
let phone = req.body.phone
let phone = body.phone
let retryTimes
try {
retryTimes = req.body['reminder-count']
retryTimes = body['reminder-count']
} catch (err) {
errors.push('Invalid retry count. Please choose a positive number!')
}
let newsletter = req.body.newsletter == 'yes' ? true : false
let overseas = req.body.overseas == 'yes' ? true : false
let messages = req.body.messages
let newsletter = body.newsletter == 'yes' ? true : false
let overseas = body.overseas == 'yes' ? true : false
let messages = body.messages
if (!!errors.length) {
res.send(`Errors: ${'' + errors}`)
return
}
// save the info
// enter the info
let pledge = new Pledge()
pledge.set('was_robot', robo)
pledge.set('amount', amount)
@ -174,8 +168,27 @@ router.post('/pledge', async (req, res) => {
pledge.set('get_newsletter', newsletter)
pledge.set('other_message', messages)
if (DEBUG) console.debug (`Saving pledge: ${JSON.stringify(pledge)}`)
// return it all!
return {
pledge: pledge,
errors: errors,
}
}
router.post('/pledge', async (req, res) => {
if (DEBUG) console.debug('New pledge:', req.body)
// process the pledge with our handy function
let {pledge, errors} = validatePledge(req.body)
// fail if there were errors
if (!!errors.length) {
res.send(`Errors: ${'' + errors}`)
return
}
// save if there weren't
if (DEBUG) console.debug (`Saving pledge: ${JSON.stringify(pledge)}`)
try {
await pledge.save()
} catch (err) {