2019-11-03 15:54:42 +01:00
|
|
|
const express = require('express');
|
2019-11-03 16:32:24 +01:00
|
|
|
const fs = require('fs');
|
2019-11-03 15:54:42 +01:00
|
|
|
const sendmail = require('sendmail')({silent: true});
|
2019-11-03 16:32:24 +01:00
|
|
|
const uniqid = require('uniqid');
|
|
|
|
const path = require('path');
|
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';
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
router.post('/', (req, res, _next) => {
|
2019-11-03 16:32:24 +01:00
|
|
|
const id = uniqid();
|
2019-11-03 15:54:42 +01:00
|
|
|
sendmail({
|
|
|
|
from: 'surpise-box@jobbel.nl',
|
|
|
|
to: req.body.email,
|
|
|
|
subject: 'test sendmail',
|
2019-11-03 16:32:24 +01:00
|
|
|
html: `
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<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>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>Met vriendelijk groet,<br> het surprise box team van Het Heerenlanden</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`,
|
2019-11-03 15:54:42 +01:00
|
|
|
}, function(err, reply) {
|
2019-11-03 16:32:24 +01:00
|
|
|
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}});
|
|
|
|
}
|
|
|
|
});
|
2019-11-03 15:54:42 +01:00
|
|
|
});
|
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
|
|
|
|
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'}});
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 15:54:42 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|