From e83712fb33155037420127274bf1410d6f33d5ab Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Fri, 13 Dec 2019 00:45:28 +0100 Subject: [PATCH] Changed acces road type check and include incomplete --- app/controllers/realEstateFilters.js | 2 +- app/helpers/db/realEstate.js | 130 ++++++++++++++++++++++++--- app/helpers/db/searchRequest.js | 13 ++- 3 files changed, 129 insertions(+), 16 deletions(-) diff --git a/app/controllers/realEstateFilters.js b/app/controllers/realEstateFilters.js index 7d6c06e..ab9eed4 100644 --- a/app/controllers/realEstateFilters.js +++ b/app/controllers/realEstateFilters.js @@ -120,6 +120,7 @@ const getFilters = async (req, res) => { }; const postFilters = async (req, res) => { + const searchRequest = await currentSearchRequest(req); if (!searchRequest || !searchRequest.dataValues) { @@ -232,7 +233,6 @@ const postFilters = async (req, res) => { searchRequest.gardenSizeMin = gardenSizeMin; searchRequest.gardenSizeMax = gardenSizeMax; } - await searchRequest.save(); res.redirect(nextStepUrl); diff --git a/app/helpers/db/realEstate.js b/app/helpers/db/realEstate.js index 435bdab..b2de369 100644 --- a/app/helpers/db/realEstate.js +++ b/app/helpers/db/realEstate.js @@ -130,9 +130,12 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { ); const geoSearchQueryPart = sequelize.where(contains, true); - //Does not work with incomplete data - //issue https://github.com/sequelize/sequelize/issues/11564 - + + //Query for case of complete ads + + //WIP This wont work, need to separate queries by adType and realEstateType + //because for example flat does not have gardenSize and that can't be in query + const query = { adType, realEstateType, @@ -148,33 +151,134 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { // gardenSize: { [Op.lte]: gardenSizeMax, - [Op.gte]: gardenSizeMin, + [Op.gte]: gardenSizeMin }, numberOfRooms: { [Op.lte]: numberOfRoomsMax, - [Op.gte]: numberOfRoomsMin, + [Op.gte]: numberOfRoomsMin }, numberOfFloors: { [Op.lte]: numberOfFloorsMax, - [Op.gte]: numberOfFloorsMin, + [Op.gte]: numberOfFloorsMin }, floor: { [Op.lte]: floorMax, - [Op.gte]: floorMin, + [Op.gte]: floorMin + }, + accessRoadType: { + [Op.or]: { + [Op.eq]: 'ANY', + [Op.eq]: accessRoadType + } }, - accessRoadType, balcony, newBuilding, elevator }; + + //Query for case of incomplete ads + const queryIncludeIncomplete = { + adType, + realEstateType, + price: { + [Op.or] : { + [Op.and] : { + [Op.lte]: priceMax, + [Op.gte]: priceMin + }, + [Op.is] : null + } + }, + area: { + [Op.or] : { + [Op.and]: { + [Op.lte]: sizeMax, + [Op.gte]: sizeMin + }, + [Op.is]: null + } + }, + [Op.and]: geoSearchQueryPart, + // + gardenSize: { + [Op.or] : { + [Op.and]: { + [Op.lte]: gardenSizeMax, + [Op.gte]: gardenSizeMin + }, + [Op.is]: null + } + }, + numberOfRooms: { + [Op.or]: { + [Op.and]: { + [Op.lte]: numberOfRoomsMax, + [Op.gte]: numberOfRoomsMin + }, + [Op.is]: null + } + }, + numberOfFloors: { + [Op.or] : { + [Op.and]: { + [Op.lte]: numberOfFloorsMax, + [Op.gte]: numberOfFloorsMin + }, + [Op.is]: null + } + }, + floor: { + [Op.or] : { + [Op.and]: { + [Op.lte]: floorMax, + [Op.gte]: floorMin + }, + [Op.is]: null + } + }, + accessRoadType: { + [Op.or]: { + [Op.eq]: 'ANY', + [Op.eq]: accessRoadType, + [Op.is]: null + } + }, + balcony: { + [Op.or]: { + [Op.eq]: balcony, + [Op.is]: null + } + }, + newBuilding: { + [Op.or]: { + [Op.eq]: newBuilding, + [Op.is]: null + } + }, + elevator: { + [Op.or]: { + [Op.eq]: elevator, + [Op.is]: null + } + } + }; const order = [["updatedAt", "desc"]]; - return await db.RealEstate.findAll({ - where: query, - limit: maxResults, - order - }); + if(!includeIncompleteAds) { + return await db.RealEstate.findAll({ + where: query, + limit: maxResults, + order + }); + } else { + return await db.RealEstate.findAll({ + where: queryIncludeIncomplete, + limit: maxResults, + order + }); + } + }; module.exports = { diff --git a/app/helpers/db/searchRequest.js b/app/helpers/db/searchRequest.js index 3fff2e5..a075c01 100644 --- a/app/helpers/db/searchRequest.js +++ b/app/helpers/db/searchRequest.js @@ -5,7 +5,7 @@ const Op = sequelize.Op; const getSearchRequest = async searchRequestId => { try { - console.log("test"); + return await db.SearchRequest.findByPk(searchRequestId); } catch (error) { @@ -51,12 +51,13 @@ const findSearchRequestsForRealEstate = async realEstate => { const geoSearchQueryPart = sequelize.where(contains, true); + //WIP This wont work, need to separate queries by adType and realEstateType + //because for example flat does not have gardenSize and that can't be in query const query = { adType, realEstateType, subscribed: true, // - accessRoadType, balcony, newBuilding, elevator, @@ -113,6 +114,14 @@ const findSearchRequestsForRealEstate = async realEstate => { [Op.gte]: floor }; } + if (accessRoadType) { + query.accessRoadType = { + [Op.or]: { + [Op.eq]: 'ANY', + [Op.eq]: accessRoadType + } + } + } return await db.SearchRequest.findAll({ where: query }); };