184 lines
5.6 KiB
JavaScript
184 lines
5.6 KiB
JavaScript
const express = require('express');
|
|
const fs = require('fs');
|
|
const uniqid = require('uniqid');
|
|
const path = require('path');
|
|
const nodemailer = require('nodemailer');
|
|
const secret = require('../secret');
|
|
|
|
var router = express.Router();
|
|
const confirmationsPath = '../database/confirmations.json';
|
|
const transporter = nodemailer.createTransport({
|
|
host: "jobbel.nl",
|
|
port: 465,
|
|
secure: true, // upgrade later with STARTTLS
|
|
auth: {
|
|
user: "job",
|
|
pass: secret
|
|
}
|
|
});
|
|
|
|
// Helper functions
|
|
|
|
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());
|
|
};
|
|
|
|
/** This function is for the validation of the email address.
|
|
* Required arguments (in POST) are:
|
|
* email: string
|
|
* message: string
|
|
* number: number
|
|
* name: string
|
|
*/
|
|
router.post('/', (req, res, _next) => {
|
|
|
|
// Generate an id.
|
|
const id = uniqid();
|
|
|
|
// Send an email with a link to validate the id.
|
|
// 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: 'Het Surprise box team van Het Heerenlanden <job@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>
|
|
`
|
|
};
|
|
|
|
transporter.sendMail(mailOptions, (error, info) => {
|
|
if (error) {
|
|
// Notify the front-end
|
|
res.json({result: {success: false, data: error}});
|
|
} else {
|
|
|
|
// Add the user's data to the database identified by the id.
|
|
writeJsonFile(confirmationsPath, {
|
|
// Make sure that the other records are preserved.
|
|
...readJsonFile(confirmationsPath),
|
|
// Then add the new one
|
|
[id]: {
|
|
'name': req.body.name,
|
|
'email': req.body.email,
|
|
'number': Number(req.body.number),
|
|
'timestamp': Date.now(),
|
|
'message': req.body.message
|
|
}
|
|
})
|
|
// Notify the front-end
|
|
res.json({result: {success: true, data: info}});
|
|
}
|
|
});
|
|
});
|
|
|
|
/** This function is used when a user clicks on the link in their email.
|
|
*
|
|
* This should be extremely user-friendly.
|
|
*/
|
|
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}});
|
|
|
|
var mailOptions = {
|
|
from: 'HHFSBRS <job@jobbel.nl>',
|
|
to: 'job@jobbel.nl',
|
|
subject: `Aanvraag voor surprise box ${data[id].number}`,
|
|
html: `
|
|
<html>
|
|
<body>
|
|
<p>Beste Simone,</p>
|
|
<p>
|
|
${data[id].name} heeft surprise box ${data[id].number} aangevraagd op ${new Date(data[id].timestamp).toUTCString()}.
|
|
Het bericht luidt: ${data[id].message}
|
|
</p>
|
|
|
|
<p>Met vriendelijk groet,<br> het computersysteem van Job en Arne</p>
|
|
</body>
|
|
</html>
|
|
`
|
|
};
|
|
|
|
transporter.sendMail(mailOptions, (error, info) => {
|
|
if (error) {
|
|
// Notify the front-end
|
|
res.json({result: {success: false, data: error}});
|
|
} else {
|
|
|
|
// And set the corresponding records in data.json to 'Gereserveerd'
|
|
let boxes = readJsonFile('../database/data.json');
|
|
boxes[Number(data[id].number) - 1] = true;
|
|
console.log(boxes);
|
|
writeJsonFile('../database/data.json', boxes);
|
|
|
|
res.render('validate-success', {number: 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'}});
|
|
res.render('validate-error', {data: 'expired', number: data[id].number });
|
|
|
|
// 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'}});
|
|
res.render('validate-error', {data: 'not found'});
|
|
}
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|