2019-09-13 14:40:10 +02:00
|
|
|
const { currentSearchRequest } = require("../helpers/url");
|
2019-09-05 11:14:54 +02:00
|
|
|
const { isValidEmail } = require("../helpers/email");
|
|
|
|
|
const { sendTemplatedEmail } = require("../helpers/awsEmail");
|
2019-05-19 13:34:44 +02:00
|
|
|
|
2019-05-29 17:04:16 +02:00
|
|
|
const getQuerySubmit = async (req, res) => {
|
2019-09-05 11:14:54 +02:00
|
|
|
const title = "Upišite vaš e-mail";
|
2019-05-19 13:34:44 +02:00
|
|
|
const nextStep = req.query.nextStep;
|
2019-05-22 16:57:08 +02:00
|
|
|
const error = req.query.error;
|
2019-05-19 13:34:44 +02:00
|
|
|
|
2019-09-05 11:14:54 +02:00
|
|
|
res.render("querySubmit", {
|
2019-05-22 16:57:08 +02:00
|
|
|
nextStep,
|
2019-07-11 14:25:38 +02:00
|
|
|
error,
|
|
|
|
|
title
|
2019-05-19 13:34:44 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const postQuerySubmit = async (req, res) => {
|
2019-09-13 14:40:10 +02:00
|
|
|
const searchRequest = await currentSearchRequest(req);
|
2019-09-05 11:14:54 +02:00
|
|
|
const nextStep = req.query.nextStep || "/ponovo";
|
2019-05-22 16:57:08 +02:00
|
|
|
|
|
|
|
|
const emailInput = req.body.email;
|
2019-05-29 17:04:16 +02:00
|
|
|
const emailConfirmInput = req.body.confirm;
|
|
|
|
|
let error = "Greška ! Unesite validan email";
|
2019-05-22 16:57:08 +02:00
|
|
|
|
2019-09-13 14:40:10 +02:00
|
|
|
if (emailInput !== emailConfirmInput) {
|
|
|
|
|
error = "Greška ! Unešeni emailovi nisu isti";
|
2019-09-05 11:14:54 +02:00
|
|
|
res.render("querySubmit", {
|
2019-05-29 17:04:16 +02:00
|
|
|
error
|
|
|
|
|
});
|
|
|
|
|
return;
|
2019-05-22 16:57:08 +02:00
|
|
|
}
|
2019-05-29 17:04:16 +02:00
|
|
|
|
2019-09-13 14:40:10 +02:00
|
|
|
if (!isValidEmail(emailInput)) {
|
|
|
|
|
error = "Greška ! Unesite validan email";
|
2019-09-05 11:14:54 +02:00
|
|
|
res.render("querySubmit", {
|
2019-05-29 17:04:16 +02:00
|
|
|
error
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 14:40:10 +02:00
|
|
|
searchRequest.email = emailInput;
|
|
|
|
|
searchRequest.subscribed = true;
|
|
|
|
|
await searchRequest.save();
|
|
|
|
|
|
2019-09-13 14:50:23 +02:00
|
|
|
sendTemplatedEmail(emailInput, searchRequest);
|
2019-09-05 11:14:54 +02:00
|
|
|
res.redirect(nextStep);
|
2019-05-19 13:34:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
getQuerySubmit,
|
|
|
|
|
postQuerySubmit
|
|
|
|
|
};
|