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:
parent
3e0e775af9
commit
87afc8688e
1 changed files with 36 additions and 23 deletions
|
@ -102,25 +102,24 @@ router.get('/', async (req, res) => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
router.post('/pledge', async (req, res) => {
|
// function to validate pledges before saving
|
||||||
if (DEBUG) console.debug('New pledge:', req.body)
|
function validatePledge(body) {
|
||||||
|
// errors get saved here
|
||||||
// validate pledge
|
|
||||||
let errors = []
|
let errors = []
|
||||||
|
|
||||||
let submit = req.body.submit
|
let submit = body.submit
|
||||||
if (submit != 'Save Pledge') {
|
if (submit != 'Save Pledge') {
|
||||||
errors.push("This request seems to have been tampered with. Are you sure it wasn't you doing the tampering?")
|
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') {
|
if(robo != 'no') {
|
||||||
errors.push('Only humans are allowed to donate money. Robots are too digital 🙁')
|
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') {
|
if (!amount || amount == 'custom') {
|
||||||
amount = req.body['amount-custom']
|
amount = body['amount-custom']
|
||||||
}
|
}
|
||||||
if (!amount || amount <= 0) {
|
if (!amount || amount <= 0) {
|
||||||
errors.push('Pledge amount too small. Please choose at least a rupee!')
|
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!')
|
errors.push('Invalid amount. Please choose a positive number!')
|
||||||
}
|
}
|
||||||
|
|
||||||
let name = req.body.name
|
let name = body.name
|
||||||
if (name.length <=0) {
|
if (name.length <=0) {
|
||||||
errors.push('What is your name? You can be anonymous to the world but at least we should know...')
|
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) {
|
if (email.length < 5) {
|
||||||
errors.push('Please enter a valid email address')
|
errors.push('Please enter a valid email address')
|
||||||
}
|
}
|
||||||
|
|
||||||
let phone = req.body.phone
|
let phone = body.phone
|
||||||
|
|
||||||
let retryTimes
|
let retryTimes
|
||||||
try {
|
try {
|
||||||
retryTimes = req.body['reminder-count']
|
retryTimes = body['reminder-count']
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
errors.push('Invalid retry count. Please choose a positive number!')
|
errors.push('Invalid retry count. Please choose a positive number!')
|
||||||
}
|
}
|
||||||
|
|
||||||
let newsletter = req.body.newsletter == 'yes' ? true : false
|
let newsletter = body.newsletter == 'yes' ? true : false
|
||||||
let overseas = req.body.overseas == 'yes' ? true : false
|
let overseas = body.overseas == 'yes' ? true : false
|
||||||
let messages = req.body.messages
|
let messages = body.messages
|
||||||
|
|
||||||
if (!!errors.length) {
|
// enter the info
|
||||||
res.send(`Errors: ${'' + errors}`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// save the info
|
|
||||||
let pledge = new Pledge()
|
let pledge = new Pledge()
|
||||||
pledge.set('was_robot', robo)
|
pledge.set('was_robot', robo)
|
||||||
pledge.set('amount', amount)
|
pledge.set('amount', amount)
|
||||||
|
@ -174,8 +168,27 @@ router.post('/pledge', async (req, res) => {
|
||||||
pledge.set('get_newsletter', newsletter)
|
pledge.set('get_newsletter', newsletter)
|
||||||
pledge.set('other_message', messages)
|
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 {
|
try {
|
||||||
await pledge.save()
|
await pledge.save()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
Loading…
Reference in a new issue