2019-09-13 11:08:45 +02:00
|
|
|
const { currentSearchRequest } = require("../helpers/url");
|
|
|
|
|
const { createSearchRequest } = require("../helpers/db/searchRequest");
|
2019-05-21 15:25:28 +02:00
|
|
|
|
2019-09-27 18:20:16 +02:00
|
|
|
const { AD_CATEGORY } = require("../common/enums");
|
2019-04-14 06:01:37 +02:00
|
|
|
|
2019-09-05 11:14:54 +02:00
|
|
|
const getRealEstateTypes = (req, res) => {
|
|
|
|
|
const title = "Koju nekretninu tražite?";
|
2019-09-27 18:20:16 +02:00
|
|
|
const realEstateTypes = Object.keys(AD_CATEGORY).map(
|
|
|
|
|
category => AD_CATEGORY[category]
|
|
|
|
|
);
|
2019-09-05 11:14:54 +02:00
|
|
|
res.render("realEstateType", { realEstateTypes, title });
|
2019-05-16 19:40:26 +02:00
|
|
|
};
|
2019-03-26 05:06:15 +01:00
|
|
|
|
2019-05-21 15:25:28 +02:00
|
|
|
const postRealEstateTypes = async (req, res) => {
|
2019-09-13 11:08:45 +02:00
|
|
|
const searchRequest = await currentSearchRequest(req);
|
|
|
|
|
|
|
|
|
|
//TODO: check if selected real estate type is valid
|
|
|
|
|
const selectedRealEstateType = req.body.realEstateType || null;
|
2019-05-21 15:25:28 +02:00
|
|
|
|
2019-09-10 07:43:43 +02:00
|
|
|
const nextStepPage = req.query.nextStep || "lokacija";
|
2019-05-21 15:25:28 +02:00
|
|
|
|
2019-09-13 11:08:45 +02:00
|
|
|
let nextStepUrl = "";
|
|
|
|
|
if (searchRequest && searchRequest.id) {
|
|
|
|
|
nextStepUrl = `/${nextStepPage}/${searchRequest.id}`;
|
|
|
|
|
searchRequest.realEstateType = selectedRealEstateType;
|
2019-09-13 14:30:54 +02:00
|
|
|
|
|
|
|
|
await searchRequest.save();
|
2019-05-21 15:25:28 +02:00
|
|
|
} else {
|
2019-09-13 11:08:45 +02:00
|
|
|
try {
|
|
|
|
|
const newSearchRequest = await createSearchRequest({
|
|
|
|
|
realEstateType: selectedRealEstateType
|
2019-09-05 11:14:54 +02:00
|
|
|
});
|
2019-09-13 11:08:45 +02:00
|
|
|
|
|
|
|
|
nextStepUrl = `/${nextStepPage}/${newSearchRequest.id}`;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
nextStepUrl = `/`;
|
|
|
|
|
}
|
2019-05-21 15:25:28 +02:00
|
|
|
}
|
2019-09-13 11:08:45 +02:00
|
|
|
res.redirect(nextStepUrl);
|
2019-05-16 19:40:26 +02:00
|
|
|
};
|
2019-04-16 06:27:11 +02:00
|
|
|
|
2019-05-16 19:40:26 +02:00
|
|
|
module.exports = {
|
2019-05-15 15:27:10 +02:00
|
|
|
getRealEstateTypes,
|
|
|
|
|
postRealEstateTypes
|
2019-03-26 05:06:15 +01:00
|
|
|
};
|