2019-09-13 14:17:46 +02:00
|
|
|
const { currentSearchRequest } = require("../helpers/url");
|
2019-10-08 20:42:43 +02:00
|
|
|
const { isValidEmail } = require("../helpers/email");
|
|
|
|
|
const {
|
|
|
|
|
notifyForNewSearchRequest
|
|
|
|
|
} = require("../services/notificationService");
|
2019-09-27 19:36:20 +02:00
|
|
|
const { AD_CATEGORY } = require("../common/enums");
|
2019-05-19 12:29:55 +02:00
|
|
|
|
2019-10-08 20:42:43 +02:00
|
|
|
const getQueryReviewData = searchRequest => {
|
2019-09-05 11:14:54 +02:00
|
|
|
const {
|
2019-09-13 14:17:46 +02:00
|
|
|
id,
|
2019-09-05 11:14:54 +02:00
|
|
|
realEstateType,
|
|
|
|
|
sizeMin,
|
|
|
|
|
sizeMax,
|
|
|
|
|
gardenSizeMin,
|
|
|
|
|
gardenSizeMax,
|
|
|
|
|
priceMin,
|
2019-09-13 14:17:46 +02:00
|
|
|
priceMax
|
|
|
|
|
} = searchRequest.dataValues;
|
2019-05-19 12:29:55 +02:00
|
|
|
|
2019-09-27 19:36:20 +02:00
|
|
|
const realEstateTypeObject = AD_CATEGORY[realEstateType];
|
2019-09-05 11:14:54 +02:00
|
|
|
const enableGardenSizeEdit = realEstateTypeObject
|
|
|
|
|
? realEstateTypeObject.hasGardenSize
|
|
|
|
|
: false;
|
2019-05-22 11:36:01 +02:00
|
|
|
|
2019-09-27 19:36:20 +02:00
|
|
|
const realEstateTypeTitle = realEstateTypeObject
|
|
|
|
|
? realEstateTypeObject.title
|
2019-09-13 14:17:46 +02:00
|
|
|
: "-";
|
2019-09-10 07:44:43 +02:00
|
|
|
|
2019-10-18 03:29:15 -07:00
|
|
|
const locationTitle = "Promjenite lokaciju";
|
2019-10-14 07:44:22 +02:00
|
|
|
const sizeTitle = `${sizeMin} - ${sizeMax} m2`;
|
|
|
|
|
const gardenSizeTitle = enableGardenSizeEdit
|
|
|
|
|
? `${gardenSizeMin} - ${gardenSizeMax} m2`
|
|
|
|
|
: "-";
|
|
|
|
|
const priceTitle = `${priceMin} - ${priceMax} KM`;
|
2019-09-10 07:44:43 +02:00
|
|
|
|
2019-10-08 20:42:43 +02:00
|
|
|
return [
|
2019-05-19 12:29:55 +02:00
|
|
|
{
|
2019-09-05 11:14:54 +02:00
|
|
|
id: "realEstateType",
|
2019-05-19 12:29:55 +02:00
|
|
|
title: realEstateTypeTitle,
|
2019-10-15 18:46:11 +02:00
|
|
|
url: `/vrstanekretnine/${id}?nextStep=filteri`
|
2019-05-19 12:29:55 +02:00
|
|
|
},
|
2019-10-18 03:29:15 -07:00
|
|
|
{
|
|
|
|
|
id: "location",
|
|
|
|
|
title: locationTitle,
|
|
|
|
|
url: `/lokacija/${id}?nextStep=pregled`
|
|
|
|
|
},
|
2019-05-19 12:29:55 +02:00
|
|
|
{
|
2019-09-05 11:14:54 +02:00
|
|
|
id: "size",
|
2019-05-19 12:29:55 +02:00
|
|
|
title: sizeTitle,
|
2019-10-11 15:37:47 +02:00
|
|
|
url: `/filteri/${id}?nextStep=pregled`
|
2019-05-19 12:29:55 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-09-05 11:14:54 +02:00
|
|
|
id: "gardenSize",
|
2019-05-19 12:29:55 +02:00
|
|
|
title: gardenSizeTitle,
|
2019-10-11 15:37:47 +02:00
|
|
|
url: enableGardenSizeEdit ? `/filteri/${id}?nextStep=pregled` : ""
|
2019-05-19 12:29:55 +02:00
|
|
|
},
|
|
|
|
|
{
|
2019-09-05 11:14:54 +02:00
|
|
|
id: "price",
|
2019-05-19 12:29:55 +02:00
|
|
|
title: priceTitle,
|
2019-10-11 15:37:47 +02:00
|
|
|
url: `/filteri/${id}?nextStep=pregled`
|
2019-05-19 12:29:55 +02:00
|
|
|
}
|
2019-10-18 03:29:15 -07:00
|
|
|
].filter((data) => data.title != "-");
|
2019-10-08 20:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getQueryReview = async (req, res) => {
|
|
|
|
|
const searchRequest = await currentSearchRequest(req);
|
|
|
|
|
|
|
|
|
|
if (!searchRequest || !searchRequest.dataValues) {
|
2019-10-14 11:20:45 +02:00
|
|
|
res.render("notFound", { title: " " });
|
|
|
|
|
return;
|
2019-10-08 20:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const title = "Da li je ovo to što ste tražili ?";
|
|
|
|
|
const nextStep = req.query.nextStep;
|
|
|
|
|
const error = req.query.error;
|
|
|
|
|
const queryReviewData = getQueryReviewData(searchRequest);
|
2019-10-14 07:51:26 +02:00
|
|
|
const email = searchRequest.email;
|
2019-05-19 12:29:55 +02:00
|
|
|
|
2019-09-05 11:14:54 +02:00
|
|
|
res.render("queryReview", {
|
2019-05-19 12:29:55 +02:00
|
|
|
nextStep,
|
2019-09-13 14:17:46 +02:00
|
|
|
queryReviewData,
|
2019-10-08 20:42:43 +02:00
|
|
|
title,
|
2019-10-14 07:51:26 +02:00
|
|
|
email,
|
2019-10-08 20:42:43 +02:00
|
|
|
error
|
2019-05-19 12:29:55 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const postQueryReview = async (req, res) => {
|
2019-09-13 14:17:46 +02:00
|
|
|
const searchRequest = await currentSearchRequest(req);
|
2019-10-08 20:42:43 +02:00
|
|
|
|
|
|
|
|
if (!searchRequest || !searchRequest.dataValues) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextStep = req.query.nextStep || "/ponovo";
|
|
|
|
|
const emailInput = req.body.email;
|
|
|
|
|
const emailConfirmInput = req.body.confirmEmail;
|
|
|
|
|
const title = "Da li je ovo to što ste tražili ?";
|
|
|
|
|
const queryReviewData = getQueryReviewData(searchRequest);
|
|
|
|
|
|
|
|
|
|
if (emailInput !== emailConfirmInput) {
|
|
|
|
|
const error = "Greška ! Unešeni emailovi nisu isti";
|
|
|
|
|
res.render("queryReview", {
|
|
|
|
|
error,
|
|
|
|
|
title,
|
2019-10-14 10:56:01 +02:00
|
|
|
queryReviewData,
|
|
|
|
|
email: ""
|
2019-10-08 20:42:43 +02:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isValidEmail(emailInput)) {
|
|
|
|
|
const error = "Greška ! Unesite validan email";
|
|
|
|
|
res.render("queryReview", {
|
|
|
|
|
error,
|
|
|
|
|
title,
|
2019-10-14 10:56:01 +02:00
|
|
|
queryReviewData,
|
|
|
|
|
email: ""
|
2019-10-08 20:42:43 +02:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
searchRequest.email = emailInput;
|
|
|
|
|
searchRequest.subscribed = true;
|
|
|
|
|
await searchRequest.save();
|
|
|
|
|
|
|
|
|
|
await notifyForNewSearchRequest(searchRequest);
|
2019-05-19 13:34:44 +02:00
|
|
|
|
|
|
|
|
res.redirect(nextStep);
|
2019-05-19 12:29:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
getQueryReview,
|
|
|
|
|
postQueryReview
|
|
|
|
|
};
|