diff --git a/app/common/enums.js b/app/common/enums.js index 20aa4dd..c89b21e 100644 --- a/app/common/enums.js +++ b/app/common/enums.js @@ -303,7 +303,8 @@ const AD_AGENCY = { OLX: "OLX", RENTAL: "RENTAL", PROSTOR: "PROSTOR", - AKTIDO: "AKTIDO" + AKTIDO: "AKTIDO", + KIVI: "KIVI" }; const CRAWLER_AD_TYPE = { diff --git a/app/controllers/publishRealEstateTypes.js b/app/controllers/publishRealEstateTypes.js new file mode 100644 index 0000000..7965add --- /dev/null +++ b/app/controllers/publishRealEstateTypes.js @@ -0,0 +1,95 @@ +const { currentRealEstate } = require("../helpers/url"); +const { createRealEstate } = require("../helpers/db/realEstate"); +const { createKiviOriginal } = require("../helpers/db/kiviOriginal"); + +const { AD_CATEGORY, AD_TYPE, AD_AGENCY } = require("../common/enums"); + +const getPublishTypes = async (req, res) => { + const realEstate = await currentRealEstate(req); + + const title = "Koju nekretninu nudite?"; + let selectedAdType = AD_TYPE.AD_TYPE_SALE.id; + const labelAdType = ["Prodaj", "Iznajmi"]; + + if ( + realEstate && + realEstate.adType && + realEstate.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); + + res.render("realEstateType", { + selectedAdType, + labelAdType, + realEstateTypes, + title, + AD_TYPE + }); +}; + +const postPublishTypes = async (req, res) => { + const realEstate = await currentRealEstate(req); + + 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; + + const validRealEstateTypes = Object.keys(AD_CATEGORY).filter( + category => !!AD_CATEGORY[category].title + ); + + const selectedRealEstateType = req.body.realEstateType || null; + if (validRealEstateTypes.indexOf(selectedRealEstateType) === -1) { + res.render("notFound", { title: " " }); + return; + } + // Sta je nextStepPage - treba napraviti !!!! + ///// + ///// + const nextStepPage = req.query.nextStep || "/lokacija"; + + let nextStepUrl = ""; + if (realEstate && realEstate.id) { + nextStepUrl = `/${nextStepPage}/${realEstate.id}`; + realEstate.adType = adTypeStringId; + realEstate.realEstateType = selectedRealEstateType; + + await realEstate.save(); + } else { + try { + const newKiviOriginal = await createKiviOriginal({ + email: "" + }); + + const newRealEstate = await createRealEstate({ + adType: adTypeStringId, + realEstateType: selectedRealEstateType, + url: "http://localhost:5000/", + originAgencyName: AD_AGENCY.KIVI, + agencyObjectId: newKiviOriginal.kiviAdId + }); + + //Da li ovaj id ili kivioriginal id + nextStepUrl = `/${nextStepPage}/${newRealEstate.id}`; + } catch (error) { + console.log(error); + nextStepUrl = `/`; + } + } + res.redirect(nextStepUrl); +}; + +module.exports = { + getPublishTypes, + postPublishTypes +}; diff --git a/app/controllers/realEstateTypes.js b/app/controllers/realEstateTypes.js index 8cc1638..bfc8a7f 100644 --- a/app/controllers/realEstateTypes.js +++ b/app/controllers/realEstateTypes.js @@ -8,6 +8,7 @@ const getRealEstateTypes = async (req, res) => { const title = "Koju nekretninu tražite?"; let selectedAdType = AD_TYPE.AD_TYPE_SALE.id; + const labelAdType = [AD_TYPE.AD_TYPE_SALE.title, AD_TYPE.AD_TYPE_RENT.title]; if ( searchRequest && searchRequest.adType && @@ -21,6 +22,7 @@ const getRealEstateTypes = async (req, res) => { res.render("realEstateType", { selectedAdType, + labelAdType, realEstateTypes, title, AD_TYPE diff --git a/app/controllers/welcome.js b/app/controllers/welcome.js index 3104d4e..bbb1057 100644 --- a/app/controllers/welcome.js +++ b/app/controllers/welcome.js @@ -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 = { diff --git a/app/helpers/db/kiviOriginal.js b/app/helpers/db/kiviOriginal.js new file mode 100644 index 0000000..c1e7296 --- /dev/null +++ b/app/helpers/db/kiviOriginal.js @@ -0,0 +1,11 @@ +"use strict"; +const db = require("../../models/index"); +const sequelize = require("sequelize"); + +const createKiviOriginal = async (kiviAdFields = {}) => { + return await db.KiviOriginal.create(kiviAdFields); +}; + +module.exports = { + createKiviOriginal +}; diff --git a/app/helpers/db/realEstate.js b/app/helpers/db/realEstate.js index 0f77260..afd9bb9 100644 --- a/app/helpers/db/realEstate.js +++ b/app/helpers/db/realEstate.js @@ -77,7 +77,16 @@ const bulkUpsertRealEstates = async realEstateData => { }; const getRealEstateById = async id => { - return db.RealEstate.findByPk(id); + try { + return db.RealEstate.findByPk(id); + } catch (error) { + console.log("realEstate.js", error); + return null; + } +}; + +const createRealEstate = async (realEstateFields = {}) => { + return await db.RealEstate.create(realEstateFields); }; const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { @@ -344,5 +353,6 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { module.exports = { bulkUpsertRealEstates, getRealEstateById, + createRealEstate, findRealEstatesForSearchRequest }; diff --git a/app/helpers/url.js b/app/helpers/url.js index 9ed771f..7dc0108 100644 --- a/app/helpers/url.js +++ b/app/helpers/url.js @@ -1,4 +1,5 @@ const { getSearchRequest } = require("./db/searchRequest"); +const { getRealEstateById } = require("./db/realEstate"); const currentSearchRequest = async req => { const searchRequestId = @@ -7,6 +8,14 @@ const currentSearchRequest = async req => { return await getSearchRequest(searchRequestId); }; -module.exports = { - currentSearchRequest + +const currentRealEstate = async req => { + const realEstateId = req && req.params ? req.params["realEstateId"] : null; + if (!realEstateId) return null; + + return await getRealEstateById(realEstateId); +}; +module.exports = { + currentSearchRequest, + currentRealEstate }; diff --git a/app/migrations/20200203114630-add-kiviOriginal-table.js b/app/migrations/20200203114630-add-kiviOriginal-table.js new file mode 100644 index 0000000..dddd6e6 --- /dev/null +++ b/app/migrations/20200203114630-add-kiviOriginal-table.js @@ -0,0 +1,27 @@ +"use strict"; + +module.exports = { + up: (queryInterface, Sequelize) => { + const tableFields = { + kiviAdId: { + type: Sequelize.BIGINT, + autoIncrement: true, + primaryKey: true + }, + email: Sequelize.TEXT, + createdAt: { + type: Sequelize.DATE, + defaultValue: Sequelize.literal("NOW()") + }, + updatedAt: { + type: Sequelize.DATE, + defaultValue: Sequelize.literal("NOW()") + } + }; + return queryInterface.createTable("KiviOriginal", tableFields); + }, + + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable("KiviOriginal", {}); + } +}; diff --git a/app/models/kiviOriginal.js b/app/models/kiviOriginal.js new file mode 100644 index 0000000..54026e0 --- /dev/null +++ b/app/models/kiviOriginal.js @@ -0,0 +1,20 @@ +"use strict"; + +module.exports = (sequalize, DataTypes) => { + const KiviOriginal = sequalize.define( + "KiviOriginal", + { + kiviAdId: { + type: DataTypes.BIGINT, + autoIncrement: true, + primaryKey: true + }, + email: DataTypes.TEXT + }, + { + freezeTableName: true + } + ); + + return KiviOriginal; +}; diff --git a/app/routes/index.js b/app/routes/index.js index 9043bbf..65b67a2 100644 --- a/app/routes/index.js +++ b/app/routes/index.js @@ -7,6 +7,10 @@ const { getRealEstateTypes, postRealEstateTypes } = require("../controllers/realEstateTypes"); +const { + getPublishTypes, + postPublishTypes +} = require("../controllers/publishRealEstateTypes"); const { getQueryReview, postQueryReview @@ -28,6 +32,11 @@ router.get("/vrstanekretnine", getRealEstateTypes); router.post("/vrstanekretnine/:searchRequestId", postRealEstateTypes); router.post("/vrstanekretnine", postRealEstateTypes); +router.get("/objavinekretninu/:realEstateId", getPublishTypes); +router.get("/objavinekretninu", getPublishTypes); +router.post("/objavinekretninu/:realEstateId", postPublishTypes); +router.post("/objavinekretninu", postPublishTypes); + router.get("/lokacija/:searchRequestId", getLocation); router.post("/lokacija/:searchRequestId", postLocation); diff --git a/app/views/realEstateType.ejs b/app/views/realEstateType.ejs index 480478f..0610d27 100644 --- a/app/views/realEstateType.ejs +++ b/app/views/realEstateType.ejs @@ -9,7 +9,7 @@ <% if (selectedAdType === AD_TYPE.AD_TYPE_SALE.id) { %> checked <% } %>> - <%= AD_TYPE.AD_TYPE_SALE.title %> + <%= labelAdType[0] %> diff --git a/app/views/welcome.ejs b/app/views/welcome.ejs index 9ec6a94..845ec43 100644 --- a/app/views/welcome.ejs +++ b/app/views/welcome.ejs @@ -18,7 +18,20 @@ - +
+
Objavite svoj oglas.
+
+
+
+
+ Prodaj +
+
+ Iznajmi +
+
+ +