From cb376b91ec4325f906d6d9dd5a33196ad0f3f835 Mon Sep 17 00:00:00 2001 From: Hippo Date: Wed, 29 Dec 2021 22:35:25 +0530 Subject: [PATCH] Save and accept incoming pledges --- server/index.js | 82 +++++++++++++++++++++++++++++++++++++++++++++++-- src/index.htm | 20 +++++++----- 2 files changed, 91 insertions(+), 11 deletions(-) diff --git a/server/index.js b/server/index.js index 79a1995..185faf7 100644 --- a/server/index.js +++ b/server/index.js @@ -1,4 +1,5 @@ const express = require('express') +const bodyParser = require('body-parser') const path = require('path') require('dotenv').config() @@ -69,7 +70,9 @@ const app = express() const router = express.Router() app.use(baseUrl, router) -app.use(express.static('dist')) +router.use(bodyParser.urlencoded({ + extended: true, +})) // main views router.get('/', async (req, res) => { @@ -82,8 +85,6 @@ router.get('/', async (req, res) => { let result = await Pledge.fetchAll() for (let pledge of result.models) { - if (DEBUG) console.log('Adding', pledge.get('amount')) - total_people += 1 total_rupees += pledge.get('amount') } @@ -100,6 +101,81 @@ router.get('/', async (req, res) => { }) }) +router.post('/pledge', async (req, res) => { + if (DEBUG) console.debug('New pledge:', req.body) + + // validate pledge + let errors = [] + + let submit = req.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 + if(robo != 'no') { + errors.push('Only humans are allowed to donate money. Robots are too digital 🙁') + } + + let amount = req.body.amount + if (!amount || amount == 'custom') { + amount = req.body['amount-custom'] + } + if (!amount || amount <= 0) { + errors.push('Pledge amount too small. Please choose at least a rupee!') + } + try { + amount = Number(amount) + } catch (err) { + errors.push('Invalid amount. Please choose a positive number!') + } + + let name = req.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 email = req.body.email + if (email.length < 5) { + errors.push('Please enter a valid email address') + } + + let phone = req.body.phone + let newsletter = req.body.newsletter == 'yes' ? true : false + let messages = req.body.messages + + if (!!errors.length) { + res.end(`Errors: ${'' + errors}`) + return + } + + // save the info + let pledge = new Pledge() + pledge.set('was_robot', robo) + pledge.set('amount', amount) + pledge.set('name', name) + pledge.set('anonymous', anonymous) + pledge.set('email', email) + pledge.set('phone', phone) + pledge.set('get_newsletter', newsletter) + pledge.set('other_message', messages) + + if (DEBUG) console.debug (`Saving pledge: ${JSON.stringify(pledge)}`) + + try { + await pledge.save() + } catch (err) { + res.end("Sorry, something went wrong while saving your pledge and we don't know what 🙁. Please try again...") + return + } + + res.end('Thanks!') +}) + +router.use(express.static('dist')) + // start the listener! app.listen(port, () => { console.log(`Server is up at port ${port}`) diff --git a/src/index.htm b/src/index.htm index c805340..45e8b21 100644 --- a/src/index.htm +++ b/src/index.htm @@ -172,7 +172,7 @@
-
+

Donation options

    @@ -232,9 +232,15 @@
- - - + + + +
+ +
+ + +
@@ -274,11 +280,9 @@
- + - +