diff --git a/app/helpers/db/realEstate.js b/app/helpers/db/realEstate.js index b2de369..9dd4dfe 100644 --- a/app/helpers/db/realEstate.js +++ b/app/helpers/db/realEstate.js @@ -88,7 +88,6 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { adType, realEstateType, areaToSearch, - // gardenSizeMin, gardenSizeMax, numberOfRoomsMin, @@ -102,7 +101,6 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { elevator, newBuilding, accessRoadType - // } = searchRequest; const longitudeColumn = sequelize.col("locationLong"); @@ -131,52 +129,30 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { const geoSearchQueryPart = sequelize.where(contains, true); - //Query for case of complete ads + //General queries contain only attributes that are defined for every searchreq - //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 - + //Query for case of complete ads const query = { - adType, - realEstateType, - price: { - [Op.lte]: priceMax, - [Op.gte]: priceMin - }, - area: { - [Op.lte]: sizeMax, - [Op.gte]: sizeMin - }, - [Op.and]: geoSearchQueryPart, - // - gardenSize: { - [Op.lte]: gardenSizeMax, - [Op.gte]: gardenSizeMin - }, - numberOfRooms: { - [Op.lte]: numberOfRoomsMax, - [Op.gte]: numberOfRoomsMin - }, - numberOfFloors: { - [Op.lte]: numberOfFloorsMax, - [Op.gte]: numberOfFloorsMin - }, - floor: { - [Op.lte]: floorMax, - [Op.gte]: floorMin - }, - accessRoadType: { - [Op.or]: { - [Op.eq]: 'ANY', - [Op.eq]: accessRoadType - } - }, - balcony, - newBuilding, - elevator - }; + adType, + realEstateType, + price: { + [Op.lte]: priceMax, + [Op.gte]: priceMin + }, + area: { + [Op.lte]: sizeMax, + [Op.gte]: sizeMin + }, + accessRoadType: { + [Op.or]: { + [Op.eq]: 'ANY', + [Op.eq]: accessRoadType + } + }, + [Op.and]: geoSearchQueryPart + } - //Query for case of incomplete ads + //Query for case of incomplete ads const queryIncludeIncomplete = { adType, realEstateType, @@ -198,44 +174,6 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { [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', @@ -243,25 +181,109 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { [Op.is]: null } }, - balcony: { + [Op.and]: geoSearchQueryPart + } + + //Every other attribute is checked separately and included in query only if it is defined + if (gardenSizeMax && gardenSizeMin) { + query.gardenSize = { + [Op.lte]: gardenSizeMax, + [Op.gte]: gardenSizeMin + } + queryIncludeIncomplete.gardenSize = { + [Op.or] : { + [Op.and]: { + [Op.lte]: gardenSizeMax, + [Op.gte]: gardenSizeMin + }, + [Op.is]: null + } + } + } + + if (numberOfRoomsMin && numberOfRoomsMax) { + query.numberOfRooms = { + [Op.lte]: numberOfRoomsMax, + [Op.gte]: numberOfRoomsMin + } + queryIncludeIncomplete.numberOfRooms = { + [Op.or] : { + [Op.and]: { + [Op.lte]: numberOfRoomsMax, + [Op.gte]: numberOfRoomsMin + }, + [Op.is]: null + } + } + } + + if (numberOfFloorsMin && numberOfFloorsMax) { + query.numberOfFloors = { + [Op.lte]: numberOfFloorsMax, + [Op.gte]: numberOfFloorsMin + } + queryIncludeIncomplete.numberOfFloors = { + [Op.or] : { + [Op.and]: { + [Op.lte]: numberOfFloorsMax, + [Op.gte]: numberOfFloorsMin + }, + [Op.is]: null + } + } + } + + if (floorMin && floorMax) { + query.floor = { + [Op.lte]: floorMax, + [Op.gte]: floorMin + } + queryIncludeIncomplete.floor = { + [Op.or] : { + [Op.and]: { + [Op.lte]: floorMax, + [Op.gte]: floorMin + }, + [Op.is]: null + } + } + } + + if (balcony) { + query.balcony = { + [Op.eq]: balcony + } + queryIncludeIncomplete.balcony = { [Op.or]: { [Op.eq]: balcony, [Op.is]: null } - }, - newBuilding: { + } + } + + if (newBuilding) { + query.newBuilding = { + [Op.eq]: newBuilding + } + queryIncludeIncomplete.newBuilding = { [Op.or]: { [Op.eq]: newBuilding, [Op.is]: null } - }, - elevator: { + } + } + + if (elevator) { + query.elevator = { + [Op.eq]: elevator + } + queryIncludeIncomplete.elevator = { [Op.or]: { [Op.eq]: elevator, [Op.is]: null } } - }; + } const order = [["updatedAt", "desc"]]; diff --git a/app/helpers/db/searchRequest.js b/app/helpers/db/searchRequest.js index a075c01..54faeb3 100644 --- a/app/helpers/db/searchRequest.js +++ b/app/helpers/db/searchRequest.js @@ -26,7 +26,6 @@ const findSearchRequestsForRealEstate = async realEstate => { realEstateType, locationLat, locationLong, - // accessRoadType, balcony, newBuilding, @@ -51,19 +50,15 @@ 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 + //General query contains only attributes that are defined for every realestate - not null const query = { adType, realEstateType, subscribed: true, - // - balcony, - newBuilding, - elevator, [Op.and]: geoSearchQueryPart }; + //Every other attribute is checked separately and included in query only if it is defined if (price) { query.priceMin = { [Op.lte]: price @@ -81,7 +76,7 @@ const findSearchRequestsForRealEstate = async realEstate => { [Op.gte]: area }; } - // + if (gardenSize) { query.gardenSizeMin = { [Op.lte]: gardenSize @@ -90,6 +85,7 @@ const findSearchRequestsForRealEstate = async realEstate => { [Op.gte]: gardenSize }; } + if (numberOfRooms) { query.numberOfRoomsMin = { [Op.lte]: numberOfRooms @@ -98,6 +94,7 @@ const findSearchRequestsForRealEstate = async realEstate => { [Op.gte]: numberOfRooms }; } + if (numberOfFloors) { query.numberOfFloorsMin = { [Op.lte]: numberOfFloors @@ -106,6 +103,7 @@ const findSearchRequestsForRealEstate = async realEstate => { [Op.gte]: numberOfFloors }; } + if (floor) { query.floorMin = { [Op.lte]: floor @@ -114,6 +112,7 @@ const findSearchRequestsForRealEstate = async realEstate => { [Op.gte]: floor }; } + if (accessRoadType) { query.accessRoadType = { [Op.or]: { @@ -122,6 +121,25 @@ const findSearchRequestsForRealEstate = async realEstate => { } } } + + if (balcony) { + query.balcony = { + [Op.eq]: balcony + } + } + + if (newBuilding) { + query.newBuilding = { + [Op.eq]: newBuilding + } + } + + if (elevator) { + query.elevator = { + [Op.eq]: elevator + } + } + return await db.SearchRequest.findAll({ where: query }); };