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

78 lines
2.1 KiB
JavaScript
Raw Normal View History

const { createSearchRequest } = require("../helpers/db/searchRequest");
const { createRealEstate } = require("../helpers/db/realEstate");
const { createKiviOriginal } = require("../helpers/db/kiviOriginal");
const { AD_TYPE, AD_CATEGORY, AD_AGENCY } = require("../common/enums");
2019-09-05 11:14:54 +02:00
const getWelcome = (req, res) => {
res.render("welcome", {
title: false,
AD_TYPE
});
};
const postWelcome = async (req, res) => {
const adType = parseInt(req.body.adType);
const publishAdType = parseInt(req.body.publishAdType);
let nextStepUrl = "";
if (adType) {
const adTypeStringId = getAdTypeString(adType);
try {
const newSearchRequest = await createSearchRequest({
adType: adTypeStringId,
realEstateType: AD_CATEGORY.FLAT.id
});
nextStepUrl = `/vrstanekretnine/${newSearchRequest.id}`;
} catch (error) {
console.log(error);
nextStepUrl = `/`;
}
} else if (publishAdType) {
const adTypeStringId = getAdTypeString(publishAdType);
try {
//Firt we create new Kivi Ad Original object in db then new Real Estate
//Problem with id-s
const newKiviOriginal = await createKiviOriginal({
email: ""
});
const newRealEstate = await createRealEstate({
adType: adTypeStringId,
realEstateType: AD_CATEGORY.FLAT.id,
//Temp variables because of the not null constraints
url: "http://localhost:5000/",
originAgencyName: AD_AGENCY.KIVI,
agencyObjectId: newKiviOriginal.kiviAdId
});
nextStepUrl = `/objavinekretninu/${newRealEstate.id}`;
} catch (error) {
console.log(error);
nextStepUrl = `/`;
}
}
res.redirect(nextStepUrl);
};
//--- Helper function
const getAdTypeString = 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;
return adTypeStringId;
2019-05-16 19:58:48 +02:00
};
2019-05-15 15:27:10 +02:00
2019-05-16 19:58:48 +02:00
module.exports = {
getWelcome,
postWelcome
2019-05-15 15:27:10 +02:00
};