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-11-01 19:21:37 +01:00
|
|
|
const { AD_CATEGORY, AD_TYPE, EMAIL_FREQUENCY } = require("../common/enums");
|
2019-05-19 12:29:55 +02:00
|
|
|
|
2019-11-01 19:21:37 +01:00
|
|
|
const getQueryReviewTableData = searchRequest => {
|
2019-09-05 11:14:54 +02:00
|
|
|
const {
|
2019-09-13 14:17:46 +02:00
|
|
|
id,
|
2019-10-30 22:13:22 +01:00
|
|
|
adType,
|
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-10-30 22:13:22 +01:00
|
|
|
let adTypeTitle = "";
|
|
|
|
|
switch (adType) {
|
|
|
|
|
case AD_TYPE.AD_TYPE_SALE.stringId:
|
|
|
|
|
adTypeTitle = AD_TYPE.AD_TYPE_SALE.title;
|
|
|
|
|
break;
|
|
|
|
|
case AD_TYPE.AD_TYPE_RENT.stringId:
|
|
|
|
|
adTypeTitle = AD_TYPE.AD_TYPE_RENT.title;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
adTypeTitle = "-";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-27 19:36:20 +02:00
|
|
|
const realEstateTypeTitle = realEstateTypeObject
|
2019-10-30 22:13:22 +01:00
|
|
|
? `[${adTypeTitle}] ${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 05:33:03 -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;
|
2019-11-01 19:21:37 +01:00
|
|
|
const queryReviewTableData = getQueryReviewTableData(searchRequest);
|
2019-10-14 07:51:26 +02:00
|
|
|
const email = searchRequest.email;
|
2019-11-01 19:21:37 +01:00
|
|
|
let selectedEmailFrequency;
|
|
|
|
|
switch (searchRequest.emailFrequency) {
|
|
|
|
|
case EMAIL_FREQUENCY.ASAP.stringId:
|
|
|
|
|
selectedEmailFrequency = EMAIL_FREQUENCY.ASAP.id;
|
|
|
|
|
break;
|
|
|
|
|
case EMAIL_FREQUENCY.DAILY.stringId:
|
|
|
|
|
selectedEmailFrequency = EMAIL_FREQUENCY.DAILY.id;
|
|
|
|
|
break;
|
|
|
|
|
}
|
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-11-01 19:21:37 +01:00
|
|
|
queryReviewTableData,
|
2019-10-08 20:42:43 +02:00
|
|
|
title,
|
2019-10-14 07:51:26 +02:00
|
|
|
email,
|
2019-11-01 19:21:37 +01:00
|
|
|
selectedEmailFrequency,
|
|
|
|
|
error,
|
|
|
|
|
EMAIL_FREQUENCY
|
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";
|
2019-11-01 19:21:37 +01:00
|
|
|
const emailFrequency =
|
|
|
|
|
parseInt(req.body.emailFrequency) || EMAIL_FREQUENCY.ASAP.id;
|
2019-10-08 20:42:43 +02:00
|
|
|
const emailInput = req.body.email;
|
|
|
|
|
const emailConfirmInput = req.body.confirmEmail;
|
|
|
|
|
const title = "Da li je ovo to što ste tražili ?";
|
2019-11-01 19:21:37 +01:00
|
|
|
const queryReviewTableData = getQueryReviewTableData(searchRequest);
|
|
|
|
|
|
|
|
|
|
let emailFrequencyStringId = EMAIL_FREQUENCY.ASAP.stringId;
|
|
|
|
|
if (emailFrequency === EMAIL_FREQUENCY.DAILY.id) {
|
|
|
|
|
emailFrequencyStringId = EMAIL_FREQUENCY.DAILY.stringId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
searchRequest.emailFrequency = emailFrequencyStringId;
|
2019-10-08 20:42:43 +02:00
|
|
|
|
|
|
|
|
if (emailInput !== emailConfirmInput) {
|
|
|
|
|
const error = "Greška ! Unešeni emailovi nisu isti";
|
|
|
|
|
res.render("queryReview", {
|
|
|
|
|
error,
|
|
|
|
|
title,
|
2019-11-01 19:21:37 +01:00
|
|
|
queryReviewTableData,
|
2019-10-14 10:56:01 +02:00
|
|
|
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-11-01 19:21:37 +01:00
|
|
|
queryReviewTableData,
|
2019-10-14 10:56:01 +02:00
|
|
|
email: ""
|
2019-10-08 20:42:43 +02:00
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
searchRequest.email = emailInput;
|
|
|
|
|
searchRequest.subscribed = true;
|
|
|
|
|
|
2019-10-28 12:34:14 +01:00
|
|
|
try {
|
|
|
|
|
await searchRequest.save();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log("[ERROR] Failed to save search request !", e);
|
|
|
|
|
console.log("Search request : ", searchRequest);
|
|
|
|
|
const error =
|
|
|
|
|
"Greška ! Nismo uspjeli kreirati zahtjev za Vašu pretragu. Molimo pokuštajte ponovo";
|
|
|
|
|
res.render("queryReview", {
|
|
|
|
|
error,
|
|
|
|
|
title,
|
2019-11-01 19:21:37 +01:00
|
|
|
queryReviewTableData,
|
2019-10-28 12:34:14 +01:00
|
|
|
email: ""
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await notifyForNewSearchRequest(searchRequest);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log("[ERROR] Failed to send initial welcome email", e);
|
|
|
|
|
console.log("Search request : ", searchRequest);
|
|
|
|
|
|
|
|
|
|
const error =
|
|
|
|
|
"Greška ! Nismo uspjeli poslati email na Vašu adresu, pokušajte sa drugom email adresom";
|
|
|
|
|
res.render("queryReview", {
|
|
|
|
|
error,
|
|
|
|
|
title,
|
2019-11-01 19:21:37 +01:00
|
|
|
queryReviewTableData,
|
2019-10-28 12:34:14 +01:00
|
|
|
email: ""
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-05-19 13:34:44 +02:00
|
|
|
|
|
|
|
|
res.redirect(nextStep);
|
2019-05-19 12:29:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
getQueryReview,
|
|
|
|
|
postQueryReview
|
|
|
|
|
};
|