WiP Changed welcome and input form for ad type

This commit is contained in:
Naida Vatric
2020-02-03 13:11:40 +01:00
parent 824db4fbc3
commit d45441f4be
12 changed files with 264 additions and 23 deletions

View File

@@ -1,6 +1,7 @@
const { createSearchRequest } = require("../helpers/db/searchRequest");
const { AD_TYPE, AD_CATEGORY } = require("../common/enums");
const { createRealEstate } = require("../helpers/db/realEstate");
const { createKiviOriginal } = require("../helpers/db/kiviOriginal");
const { AD_TYPE, AD_CATEGORY, AD_AGENCY } = require("../common/enums");
const getWelcome = (req, res) => {
res.render("welcome", {
@@ -11,7 +12,54 @@ const getWelcome = (req, res) => {
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
@@ -20,20 +68,7 @@ const postWelcome = async (req, res) => {
const adTypeStringId =
adTypeStringIds[adType] || AD_TYPE.AD_TYPE_SALE.stringId;
let nextStepUrl = "";
try {
const newSearchRequest = await createSearchRequest({
adType: adTypeStringId,
realEstateType: AD_CATEGORY.FLAT.id
});
nextStepUrl = `/vrstanekretnine/${newSearchRequest.id}`;
} catch (error) {
console.log(error);
nextStepUrl = `/`;
}
res.redirect(nextStepUrl);
return adTypeStringId;
};
module.exports = {