Files
old-web/app/controllers/querySubmit.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

const { currentSearchRequest } = require("../helpers/url");
2019-09-05 11:14:54 +02:00
const { isValidEmail } = require("../helpers/email");
const {
notifyForNewSearchRequest
} = require("../services/notificationService");
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) => {
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;
2019-09-30 19:33:01 +02:00
const title = "Upišite vaš e-mail";
2019-05-29 17:04:16 +02:00
let error = "Greška ! Unesite validan email";
2019-05-22 16:57:08 +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-09-30 19:33:01 +02:00
error,
title
2019-05-29 17:04:16 +02:00
});
return;
2019-05-22 16:57:08 +02:00
}
2019-05-29 17:04:16 +02:00
if (!isValidEmail(emailInput)) {
error = "Greška ! Unesite validan email";
2019-09-05 11:14:54 +02:00
res.render("querySubmit", {
2019-09-30 19:33:01 +02:00
error,
title
2019-05-29 17:04:16 +02:00
});
return;
}
searchRequest.email = emailInput;
searchRequest.subscribed = true;
await searchRequest.save();
await notifyForNewSearchRequest(searchRequest);
2019-09-05 11:14:54 +02:00
res.redirect(nextStep);
2019-05-19 13:34:44 +02:00
};
module.exports = {
getQuerySubmit,
postQuerySubmit
};