diff --git a/server/database/confirmations.json b/server/database/confirmations.json index e69de29..9e26dfe 100644 --- a/server/database/confirmations.json +++ b/server/database/confirmations.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/server/package-lock.json b/server/package-lock.json index 4752f6b..c41fa5f 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -3213,6 +3213,11 @@ "set-value": "^2.0.1" } }, + "uniqid": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/uniqid/-/uniqid-5.0.3.tgz", + "integrity": "sha512-R2qx3X/LYWSdGRaluio4dYrPXAJACTqyUjuyXHoJLBUOIfmMcnYOyY2d6Y4clZcIz5lK6ZaI0Zzmm0cPfsIqzQ==" + }, "unique-string": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unique-string/-/unique-string-1.0.0.tgz", diff --git a/server/package.json b/server/package.json index 69e5826..4bcf463 100644 --- a/server/package.json +++ b/server/package.json @@ -12,7 +12,8 @@ "http-errors": "~1.6.3", "morgan": "~1.9.1", "pug": "^2.0.4", - "sendmail": "^1.6.1" + "sendmail": "^1.6.1", + "uniqid": "^5.0.3" }, "devDependencies": { "cors": "^2.8.5", diff --git a/server/routes/mail.js b/server/routes/mail.js index 8f14b48..3dfd3e4 100644 --- a/server/routes/mail.js +++ b/server/routes/mail.js @@ -1,18 +1,85 @@ const express = require('express'); +const fs = require('fs'); const sendmail = require('sendmail')({silent: true}); +const uniqid = require('uniqid'); +const path = require('path'); var router = express.Router(); +const confirmationsPath = '../database/confirmations.json'; + +const writeJsonFile = (file, data) => { + fs.writeFileSync(path.join(__dirname, file), JSON.stringify(data)); +}; + +const readJsonFile = (file) => { + return JSON.parse(fs.readFileSync(path.join(__dirname, file)).toString()); +}; router.post('/', (req, res, _next) => { + const id = uniqid(); sendmail({ from: 'surpise-box@jobbel.nl', to: req.body.email, subject: 'test sendmail', - html: 'Mail of test sendmail', + html: ` + + +

+ Deze email is verzonden omdat u surprise box ${req.body.number} voor de kerstmarkt van Het Heerenlanden heeft aangevraagd.
+ Om uw aanvraag te bevestigen, moet u om de volgende link klikken:
+ http://localhost:3000/mail/validate/${id}
+

+ +

Met vriendelijk groet,
het surprise box team van Het Heerenlanden

+ + + `, }, function(err, reply) { - if (err) res.json({result: err}); - else res.json({result: reply}); + if (err) { + res.json({result: {success: false, data: err}}); + } else { + writeJsonFile(confirmationsPath, { + ...readJsonFile(confirmationsPath), + [id]: { + 'number': Number(req.body.number), + 'timestamp': Date.now() + } + }) + res.json({result: {success: true, data: reply}}); + } + }); }); + +router.get('/validate/:id', (req, res, _next) => { + const data = readJsonFile(confirmationsPath); + const id = req.params.id; + + // If id is found and the user is in time + if (data[id] && Date.now() <= (data[id].timestamp + 3600000 /* one hour in milliseconds */)) { + + // send success result + res.json({result: {success: true, data: data[id].number}}); + + // Delete the record + delete data[id]; + writeJsonFile(confirmationsPath, data); + + } else { + // Else if the id is found but the time is expired + if (data[id] && Date.now() > (data[id].timestamp + 3600000)) { + + // send result + res.json({result: {success: false, data: 'expired'}}); + + // delete the record + delete data[id]; + writeJsonFile(confirmationsPath, data); + + // If the id is not found + } else { + res.json({result: {success: false, data: 'ID not found'}}); + } + } }); module.exports = router;