diff --git a/app/helpers/db/realEstate.js b/app/helpers/db/realEstate.js index 55068da..2abac03 100644 --- a/app/helpers/db/realEstate.js +++ b/app/helpers/db/realEstate.js @@ -2,6 +2,8 @@ const db = require("../../models/index"); const sequelize = require("sequelize"); const Op = sequelize.Op; +const { AD_CATEGORY } = require("../../common/enums"); + const bulkUpsertRealEstates = async realEstateData => { try { const fieldsToUpdateIfDuplicate = [ @@ -102,6 +104,9 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { accessRoadType } = searchRequest; + //Needed for defining which attribute should exist or not + const realEstateTypeObject = AD_CATEGORY[realEstateType]; + const longitudeColumn = sequelize.col("locationLong"); const latitudeColumn = sequelize.col("locationLat"); @@ -175,8 +180,13 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { [Op.and]: geoSearchQueryPart }; - //Every other attribute is checked separately and included in query only if it is defined/not null - if (gardenSizeMax!=null && gardenSizeMin!=null) { + //Every other attribute is checked separately and included in query only if it is defined for real estate type + + if ( + realEstateTypeObject.hasGardenSize && + gardenSizeMax != null && + gardenSizeMin != null + ) { query.gardenSize = { [Op.lte]: gardenSizeMax, [Op.gte]: gardenSizeMin @@ -192,7 +202,7 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (numberOfRoomsMin!=null && numberOfRoomsMax!=null) { + if (realEstateTypeObject.hasNumberOfRoom && numberOfRoomsMin != null && numberOfRoomsMax != null) { query.numberOfRooms = { [Op.lte]: numberOfRoomsMax, [Op.gte]: numberOfRoomsMin @@ -208,7 +218,7 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (numberOfFloorsMin!=null && numberOfFloorsMax!=null) { + if (realEstateTypeObject.hasNumberOfFloors && numberOfFloorsMin != null && numberOfFloorsMax != null) { query.numberOfFloors = { [Op.lte]: numberOfFloorsMax, [Op.gte]: numberOfFloorsMin @@ -224,7 +234,7 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (floorMin!=null && floorMax!=null) { + if (realEstateTypeObject.hasFloorProp && floorMin != null && floorMax != null) { query.floor = { [Op.lte]: floorMax, [Op.gte]: floorMin @@ -240,7 +250,7 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (balcony!=null) { + if (realEstateTypeObject.hasBalconyProp && balcony != null) { query.balcony = { [Op.eq]: balcony }; @@ -252,7 +262,7 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (newBuilding!=null) { + if (realEstateTypeObject.hasNewBuildingProp && newBuilding != null) { query.newBuilding = { [Op.eq]: newBuilding }; @@ -264,7 +274,7 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (elevator!=null) { + if (realEstateTypeObject.hasElevatorProp && elevator != null) { query.elevator = { [Op.eq]: elevator }; @@ -275,7 +285,8 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { } }; } - + //If user wants 'ANY' road type acces then it is not included in query - + //returns every road type and null values if (accessRoadType !== "ANY") { query.accessRoadType = { [Op.eq]: accessRoadType diff --git a/app/helpers/db/searchRequest.js b/app/helpers/db/searchRequest.js index 4596fd5..b3e4e29 100644 --- a/app/helpers/db/searchRequest.js +++ b/app/helpers/db/searchRequest.js @@ -137,7 +137,7 @@ const findSearchRequestsForRealEstate = async realEstate => { } } - //AccessRoadType is defined - should exits for each ad and estate type + //AccessRoadType is defined - should exists for each ad and estate type if (accessRoadType != null) { query.accessRoadType = { [Op.or]: { @@ -145,8 +145,11 @@ const findSearchRequestsForRealEstate = async realEstate => { [Op.eq]: accessRoadType } }; - } else if (realEstateTypeObject.hasAccesRoadType) { - checkForIncompleteWanted = true; + } else { + //Null values are returned for user request that wanted ANY acces road type + query.accessRoadType = { + [Op.eq]: "ANY" + }; } if (realEstateTypeObject.hasBalconyProp) {