2019-11-03 15:54:42 +01:00
|
|
|
const express = require('express');
|
2019-11-03 16:32:24 +01:00
|
|
|
const fs = require('fs');
|
|
|
|
const uniqid = require('uniqid');
|
|
|
|
const path = require('path');
|
2019-11-04 18:22:32 +01:00
|
|
|
const nodemailer = require('nodemailer');
|
|
|
|
const secret = require('../secret');
|
2019-11-03 15:54:42 +01:00
|
|
|
|
|
|
|
var router = express.Router();
|
2019-11-03 16:32:24 +01:00
|
|
|
const confirmationsPath = '../database/confirmations.json';
|
2019-11-04 18:22:32 +01:00
|
|
|
const transporter = nodemailer.createTransport({
|
|
|
|
host: "smtp.office365.com",
|
|
|
|
port: 587,
|
|
|
|
secure: false, // upgrade later with STARTTLS
|
|
|
|
auth: {
|
2019-11-04 23:26:34 +01:00
|
|
|
user: "36899@leerling.heerenlanden.nl",
|
2019-11-04 18:22:32 +01:00
|
|
|
pass: secret
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Helper functions
|
2019-11-03 16:32:24 +01:00
|
|
|
|
|
|
|
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());
|
|
|
|
};
|
2019-11-03 15:54:42 +01:00
|
|
|
|
2019-11-04 10:14:16 +01:00
|
|
|
/** This function is for the validation of the email address.
|
|
|
|
* Required arguments (in POST) are:
|
|
|
|
* email: string
|
|
|
|
* message: string
|
|
|
|
* number: number
|
|
|
|
* name: string
|
|
|
|
*/
|
2019-11-03 15:54:42 +01:00
|
|
|
router.post('/', (req, res, _next) => {
|
2019-11-04 10:14:16 +01:00
|
|
|
|
|
|
|
// Generate an id.
|
2019-11-03 16:32:24 +01:00
|
|
|
const id = uniqid();
|
2019-11-04 10:14:16 +01:00
|
|
|
|
|
|
|
// Send an email with a link to validate the id.
|
2019-11-04 18:22:32 +01:00
|
|
|
// sendmail({
|
|
|
|
// from: 'surpise-box@jobbel.nl',
|
|
|
|
// to: req.body.email,
|
|
|
|
// subject: `Aanvraag voor surprise box ${req.body.number}`,
|
|
|
|
// html: `
|
|
|
|
// <html>
|
|
|
|
// <body>
|
|
|
|
// <p>Beste ${req.body.name},</p>
|
|
|
|
// <p>
|
|
|
|
// Deze email is verzonden omdat u surprise box ${req.body.number} voor de kerstmarkt van Het Heerenlanden heeft aangevraagd.<br>
|
|
|
|
// Om uw aanvraag te bevestigen, moet u om de volgende link klikken:<br>
|
|
|
|
// <a href='http://localhost:3000/mail/validate/${id}'>http://localhost:3000/mail/validate/${id}</a><br>
|
|
|
|
// Deze link verloopt na één uur.
|
|
|
|
// </p>
|
|
|
|
|
|
|
|
// <p>Met vriendelijk groet,<br> het surprise box team van Het Heerenlanden</p>
|
|
|
|
// </body>
|
|
|
|
// </html>
|
|
|
|
// `,
|
|
|
|
|
|
|
|
var mailOptions = {
|
|
|
|
from: 'Surprise box team van Het Heerenlanden',
|
2019-11-03 15:54:42 +01:00
|
|
|
to: req.body.email,
|
2019-11-04 18:22:32 +01:00
|
|
|
subject: `Aanvraag voor surprise box ${req.body.number}`,
|
2019-11-03 16:32:24 +01:00
|
|
|
html: `
|
2019-11-04 18:22:32 +01:00
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<p>Beste ${req.body.name},</p>
|
|
|
|
<p>
|
|
|
|
Deze email is verzonden omdat u surprise box ${req.body.number} voor de kerstmarkt van Het Heerenlanden heeft aangevraagd.<br>
|
|
|
|
Om uw aanvraag te bevestigen, moet u om de volgende link klikken:<br>
|
|
|
|
<a href='http://localhost:3000/mail/validate/${id}'>http://localhost:3000/mail/validate/${id}</a><br>
|
|
|
|
Deze link verloopt na één uur.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>Met vriendelijk groet,<br> het surprise box team van Het Heerenlanden</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
};
|
|
|
|
|
|
|
|
transporter.sendMail(mailOptions, function(error, info){
|
|
|
|
if (error) {
|
2019-11-04 10:14:16 +01:00
|
|
|
// Notify the front-end
|
2019-11-04 23:26:34 +01:00
|
|
|
res.json({result: {success: false, data: error}});
|
2019-11-03 16:32:24 +01:00
|
|
|
} else {
|
2019-11-04 10:14:16 +01:00
|
|
|
|
|
|
|
// Add the user's data to the database identified by the id.
|
2019-11-03 16:32:24 +01:00
|
|
|
writeJsonFile(confirmationsPath, {
|
2019-11-04 10:14:16 +01:00
|
|
|
// Make sure that the other records are preserved.
|
2019-11-03 16:32:24 +01:00
|
|
|
...readJsonFile(confirmationsPath),
|
2019-11-04 10:14:16 +01:00
|
|
|
// Then add the new one
|
2019-11-03 16:32:24 +01:00
|
|
|
[id]: {
|
2019-11-04 10:14:16 +01:00
|
|
|
'name': req.body.name,
|
2019-11-04 18:22:32 +01:00
|
|
|
'email': req.body.email,
|
2019-11-03 16:32:24 +01:00
|
|
|
'number': Number(req.body.number),
|
2019-11-04 10:14:16 +01:00
|
|
|
'timestamp': Date.now(),
|
|
|
|
'message': req.body.message
|
2019-11-03 16:32:24 +01:00
|
|
|
}
|
|
|
|
})
|
2019-11-04 10:14:16 +01:00
|
|
|
// Notify the front-end
|
2019-11-03 16:32:24 +01:00
|
|
|
res.json({result: {success: true, data: reply}});
|
|
|
|
}
|
|
|
|
});
|
2019-11-03 15:54:42 +01:00
|
|
|
});
|
2019-11-03 16:32:24 +01:00
|
|
|
|
2019-11-04 10:14:16 +01:00
|
|
|
/** This function is used when a user clicks on the link in their email.
|
|
|
|
*
|
|
|
|
* This should be extremely user-friendly.
|
|
|
|
*/
|
2019-11-03 16:32:24 +01:00
|
|
|
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
|
2019-11-04 10:14:16 +01:00
|
|
|
// res.json({result: {success: true, data: data[id].number}});
|
|
|
|
res.render('validate-success', {number: data[id].number})
|
2019-11-03 16:32:24 +01:00
|
|
|
|
|
|
|
// Delete the record
|
|
|
|
delete data[id];
|
|
|
|
writeJsonFile(confirmationsPath, data);
|
|
|
|
|
2019-11-04 18:22:32 +01:00
|
|
|
// Process the email to doemiddag.hetheerenlanden@cvo-av.nl
|
|
|
|
// And set the corresponding records in data.json to 'Gereserveerd'
|
|
|
|
|
|
|
|
let boxes = readJsonFile('../database/data.json');
|
|
|
|
boxes[Number(data[id].number)] = true;
|
|
|
|
writeJsonFile('../database/data.json', boxes);
|
|
|
|
|
2019-11-03 16:32:24 +01:00
|
|
|
} else {
|
|
|
|
// Else if the id is found but the time is expired
|
|
|
|
if (data[id] && Date.now() > (data[id].timestamp + 3600000)) {
|
|
|
|
|
|
|
|
// send result
|
2019-11-04 10:14:16 +01:00
|
|
|
//res.json({result: {success: false, data: 'expired'}});
|
|
|
|
res.render('validate-error', {data: 'expired', number: data[id].number });
|
2019-11-03 16:32:24 +01:00
|
|
|
|
|
|
|
// delete the record
|
|
|
|
delete data[id];
|
|
|
|
writeJsonFile(confirmationsPath, data);
|
|
|
|
|
|
|
|
// If the id is not found
|
|
|
|
} else {
|
2019-11-04 10:14:16 +01:00
|
|
|
//res.json({result: {success: false, data: 'ID not found'}});
|
|
|
|
res.render('validate-error', {data: 'not found'});
|
2019-11-03 16:32:24 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-03 15:54:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|