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

82 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

const { currentSearchRequest } = require("../helpers/url");
const { createSearchRequest } = require("../helpers/db/searchRequest");
2019-10-30 22:13:22 +01:00
const { AD_CATEGORY, AD_TYPE } = require("../common/enums");
const getRealEstateTypes = async (req, res) => {
const searchRequest = await currentSearchRequest(req);
2019-04-14 06:01:37 +02:00
2019-09-05 11:14:54 +02:00
const title = "Koju nekretninu tražite?";
2019-10-30 22:13:22 +01:00
let selectedAdType = AD_TYPE.AD_TYPE_SALE.id;
if (
searchRequest &&
searchRequest.adType &&
searchRequest.adType === AD_TYPE.AD_TYPE_RENT.stringId
) {
selectedAdType = AD_TYPE.AD_TYPE_RENT.id;
}
const realEstateTypes = Object.keys(AD_CATEGORY)
.map(category => AD_CATEGORY[category])
.filter(category => category.title);
2019-10-30 22:13:22 +01:00
res.render("realEstateType", {
selectedAdType,
realEstateTypes,
title,
AD_TYPE
});
2019-05-16 19:40:26 +02:00
};
2019-03-26 05:06:15 +01:00
const postRealEstateTypes = async (req, res) => {
const searchRequest = await currentSearchRequest(req);
2019-10-30 22:13:22 +01:00
const adType = parseInt(req.body.adType);
const adTypeStringIds = {
[AD_TYPE.AD_TYPE_SALE.id]: AD_TYPE.AD_TYPE_SALE.stringId,
[AD_TYPE.AD_TYPE_RENT.id]: AD_TYPE.AD_TYPE_RENT.stringId
};
const adTypeStringId =
adTypeStringIds[adType] || AD_TYPE.AD_TYPE_SALE.stringId;
2019-10-28 10:38:31 +01:00
const validRealEstateTypes = Object.keys(AD_CATEGORY).filter(
category => !!AD_CATEGORY[category].title
);
const selectedRealEstateType = req.body.realEstateType || null;
2019-10-28 10:38:31 +01:00
if (validRealEstateTypes.indexOf(selectedRealEstateType) === -1) {
res.render("notFound", { title: " " });
return;
}
const nextStepPage = req.query.nextStep || "lokacija";
let nextStepUrl = "";
if (searchRequest && searchRequest.id) {
nextStepUrl = `/${nextStepPage}/${searchRequest.id}`;
2019-10-30 22:13:22 +01:00
searchRequest.adType = adTypeStringId;
searchRequest.realEstateType = selectedRealEstateType;
await searchRequest.save();
} else {
try {
const newSearchRequest = await createSearchRequest({
2019-10-30 22:13:22 +01:00
adType: adTypeStringId,
realEstateType: selectedRealEstateType
2019-09-05 11:14:54 +02:00
});
nextStepUrl = `/${nextStepPage}/${newSearchRequest.id}`;
} catch (error) {
console.log(error);
nextStepUrl = `/`;
}
}
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
};