From 870b71a3c712d4ed30b5ec9a3cf85436d7456281 Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Fri, 17 Jan 2020 01:54:06 +0100 Subject: [PATCH] WIP Changed all logic for searchRequest. --- app/helpers/db/realEstate.js | 37 ++- app/helpers/db/searchRequest.js | 401 ++++++++++++++++++++++------ app/services/notificationService.js | 2 + 3 files changed, 352 insertions(+), 88 deletions(-) diff --git a/app/helpers/db/realEstate.js b/app/helpers/db/realEstate.js index 2abac03..87cf07e 100644 --- a/app/helpers/db/realEstate.js +++ b/app/helpers/db/realEstate.js @@ -104,6 +104,9 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { accessRoadType } = searchRequest; + // ++ testing + console.log("SearchRequest za koji trazimo:", searchRequest); + //Needed for defining which attribute should exist or not const realEstateTypeObject = AD_CATEGORY[realEstateType]; @@ -202,7 +205,11 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (realEstateTypeObject.hasNumberOfRoom && numberOfRoomsMin != null && numberOfRoomsMax != null) { + if ( + realEstateTypeObject.hasNumberOfRoom && + numberOfRoomsMin != null && + numberOfRoomsMax != null + ) { query.numberOfRooms = { [Op.lte]: numberOfRoomsMax, [Op.gte]: numberOfRoomsMin @@ -218,7 +225,11 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (realEstateTypeObject.hasNumberOfFloors && numberOfFloorsMin != null && numberOfFloorsMax != null) { + if ( + realEstateTypeObject.hasNumberOfFloors && + numberOfFloorsMin != null && + numberOfFloorsMax != null + ) { query.numberOfFloors = { [Op.lte]: numberOfFloorsMax, [Op.gte]: numberOfFloorsMin @@ -234,7 +245,11 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (realEstateTypeObject.hasFloorProp && floorMin != null && floorMax != null) { + if ( + realEstateTypeObject.hasFloorProp && + floorMin != null && + floorMax != null + ) { query.floor = { [Op.lte]: floorMax, [Op.gte]: floorMin @@ -249,8 +264,10 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { } }; } - - if (realEstateTypeObject.hasBalconyProp && balcony != null) { + //Logic for balcony, newBuilding and elevator from users side + //If true is checked, then I want characteristic to be true but, + //if it is not checked, then I dont care - it can be null or false or true + if (realEstateTypeObject.hasBalconyProp && balcony === true) { query.balcony = { [Op.eq]: balcony }; @@ -262,7 +279,7 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (realEstateTypeObject.hasNewBuildingProp && newBuilding != null) { + if (realEstateTypeObject.hasNewBuildingProp && newBuilding === true) { query.newBuilding = { [Op.eq]: newBuilding }; @@ -274,7 +291,7 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { }; } - if (realEstateTypeObject.hasElevatorProp && elevator != null) { + if (realEstateTypeObject.hasElevatorProp && elevator === true) { query.elevator = { [Op.eq]: elevator }; @@ -301,6 +318,12 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => { const order = [["updatedAt", "desc"]]; + //++ + console.log( + "Query user to find real estates:", + includeIncompleteAds ? queryIncludeIncomplete : query + ); + return db.RealEstate.findAll({ where: includeIncompleteAds ? queryIncludeIncomplete : query, limit: maxResults, diff --git a/app/helpers/db/searchRequest.js b/app/helpers/db/searchRequest.js index b3e4e29..223ec89 100644 --- a/app/helpers/db/searchRequest.js +++ b/app/helpers/db/searchRequest.js @@ -49,99 +49,364 @@ const findSearchRequestsForRealEstate = async realEstate => { const geoSearchQueryPart = sequelize.where(contains, true); - //General query contains only attributes that are defined for every RealEstate - not null - const query = { - adType, - realEstateType, - subscribed: true, - [Op.and]: geoSearchQueryPart - }; //Needed for defining which attribute should exist or not const realEstateTypeObject = AD_CATEGORY[realEstateType]; - //Needed to decide on including incomplete RealEstates data + + // ?? Needed to decide on including incomplete RealEstates data let checkForIncompleteWanted = false; - //Attributes are checked separately and included in query only if defined - not null + //Attributes are checked separately to make different query parts - //Price and area should be defined for every property + //If price is null it will be excluded from query - it will show properties with null price values + //User always defines price and area (sliders) - not null in search req + let priceQuery = {}; if (price != null) { - query.priceMin = { - [Op.lte]: price - }; - query.priceMax = { - [Op.gte]: price + priceQuery = { + [Op.and]: [ + { + priceMin: { + [Op.lte]: price + } + }, + { + priceMax: { + [Op.gte]: price + } + } + ] }; } + let areaQuery = {}; if (area != null) { - query.sizeMin = { - [Op.lte]: area - }; - query.sizeMax = { - [Op.gte]: area + areaQuery = { + [Op.and]: [ + { + sizeMin: { + [Op.lte]: area + } + }, + { + sizeMax: { + [Op.gte]: area + } + } + ] }; } else { checkForIncompleteWanted = true; } + //Other attributes can be defined or not depending on RealEstate type //we check what to include in query based on real estate type object + let gardenSizeQuery = {}; if (realEstateTypeObject.hasGardenSize) { if (gardenSize != null) { - query.gardenSizeMin = { - [Op.lte]: gardenSize - }; - query.gardenSizeMax = { - [Op.gte]: gardenSize + gardenSizeQuery = { + [Op.and]: [ + { + gardenSizeMin: { + [Op.lte]: gardenSize + } + }, + { + gardenSizeMax: { + [Op.gte]: gardenSize + } + } + ] }; } else { checkForIncompleteWanted = true; } } + let numberOfRoomsQuery = {}; if (realEstateTypeObject.hasNumberOfRoom) { if (numberOfRooms != null) { - query.numberOfRoomsMin = { - [Op.lte]: numberOfRooms - }; - query.numberOfRoomsMax = { - [Op.gte]: numberOfRooms + //If real estate has defined number of rooms ex. 3 it returns req + // that accepts 3 rooms or ones that don't have defined number - null + //Ex. they didnt choose advanced filters at all + numberOfRoomsQuery = { + [Op.and]: [ + { + numberOfRoomsMin: { + [Op.or]: { + [Op.lte]: numberOfRooms, + [Op.is]: null + } + } + }, + { + numberOfRoomsMax: { + [Op.or]: { + [Op.gte]: numberOfRooms, + [Op.is]: null + } + } + } + ] }; } else { - checkForIncompleteWanted = true; + // If real estate dont have defined number of rooms ex. null + //It returns requests that didn't choose number of rooms - also null + //Or ones that picked some values but also picked to includeIncomplete ads + numberOfRoomsQuery = { + [Op.or]: [ + { + [Op.and]: [ + { + numberOfRoomsMin: { + [Op.is]: null + } + }, + { + numberOfRoomsMax: { + [Op.is]: null + } + } + ] + }, + { + includeIncompleteAds: { + [Op.eq]: true + } + } + ] + }; } } - + //Same logic for number of Floors and floors + let numberOfFloorsQuery = {}; if (realEstateTypeObject.hasNumberOfFloors) { if (numberOfFloors != null) { - query.numberOfFloorsMin = { - [Op.lte]: numberOfFloors - }; - query.numberOfFloorsMax = { - [Op.gte]: numberOfFloors + numberOfFloorsQuery = { + [Op.and]: [ + { + numberOfFloorsMin: { + [Op.or]: { + [Op.lte]: numberOfFloors, + [Op.is]: null + } + } + }, + { + numberOfFloorsMax: { + [Op.or]: { + [Op.gte]: numberOfFloors, + [Op.is]: null + } + } + } + ] }; } else { - checkForIncompleteWanted = true; + numberOfFloorsQuery = { + [Op.or]: [ + { + [Op.and]: [ + { + numberOfFloorsMin: { + [Op.is]: null + } + }, + { + numberOfFloorsMax: { + [Op.is]: null + } + } + ] + }, + { + includeIncompleteAds: { + [Op.eq]: true + } + } + ] + }; + } + } + let floorQuery = {}; + if (realEstateTypeObject.hasFloorProp) { + if (floor != null) { + floorQuery = { + [Op.and]: [ + { + floorMin: { + [Op.or]: { + [Op.lte]: floor, + [Op.is]: null + } + } + }, + { + floorMax: { + [Op.or]: { + [Op.gte]: floor, + [Op.is]: null + } + } + } + ] + }; + } else { + floorQuery = { + [Op.or]: [ + { + [Op.and]: [ + { + floorMin: { + [Op.is]: null + } + }, + { + floorMax: { + [Op.is]: null + } + } + ] + }, + { + includeIncompleteAds: { + [Op.eq]: true + } + } + ] + }; } } - if (realEstateTypeObject.hasFloorProp) { - if (floor != null) { - query.floorMin = { - [Op.lte]: floor + //Logic for balcony, newBuilding and elevator + //If user dont check checkbox for ex. elevator it does not mean he only wants no elevator + //If real estate characteristic =true find all req, one that wants charachertistic or dont care - dont need query + //If real estate characteristic = false, find all req exept for ones that wants characteristic to be true + //If real estate characteristic = null, dont know if true or false, find req that dont care or want char and want incomplete ads + let balconyQuery = {}; + if (realEstateTypeObject.hasBalconyProp && balcony !== true) { + if (balcony === false) { + balconyQuery = { + balcony: { + [Op.ne]: true + } }; - query.floorMax = { - [Op.gte]: floor + } else if (balcony === null) { + balconyQuery = { + [Op.or]: [ + { + balcony: { + [Op.ne]: true + } + }, + { + [Op.and]: [ + { + balcony: { + [Op.eq]: true + } + }, + { + includeIncompleteAds: { + [Op.eq]: true + } + } + ] + } + ] }; - } else { - checkForIncompleteWanted = true; } } + let newBuildingQuery = {}; + if (realEstateTypeObject.hasNewBuildingProp && newBuilding !== true) { + if (newBuilding === false) { + newBuildingQuery = { + newBuilding: { + [Op.ne]: true + } + }; + } else if (newBuilding === null) { + newBuildingQuery = { + [Op.or]: [ + { + newBuilding: { + [Op.ne]: true + } + }, + { + [Op.and]: [ + { + newBuilding: { + [Op.eq]: true + } + }, + { + includeIncompleteAds: { + [Op.eq]: true + } + } + ] + } + ] + }; + } + } + let elevatorQuery = {}; + if (realEstateTypeObject.hasElevatorProp && elevator !== true) { + if (elevator === false) { + elevatorQuery = { + elevator: { + [Op.ne]: true + } + }; + } else if (elevator === null) { + elevatorQuery = { + [Op.or]: [ + { + elevator: { + [Op.ne]: true + } + }, + { + [Op.and]: [ + { + elevator: { + [Op.eq]: true + } + }, + { + includeIncompleteAds: { + [Op.eq]: true + } + } + ] + } + ] + }; + } + } + //General query consists of each individual query + const query = { + adType, + realEstateType, + subscribed: true, + [Op.and]: [ + geoSearchQueryPart, + priceQuery, + areaQuery, + gardenSizeQuery, + numberOfRoomsQuery, + numberOfFloorsQuery, + floorQuery, + balconyQuery, + newBuildingQuery, + elevatorQuery + ] + }; //AccessRoadType is defined - should exists for each ad and estate type if (accessRoadType != null) { query.accessRoadType = { [Op.or]: { - [Op.eq]: "ANY", + [Op.like]: "ANY", [Op.eq]: accessRoadType } }; @@ -151,45 +416,19 @@ const findSearchRequestsForRealEstate = async realEstate => { [Op.eq]: "ANY" }; } - - if (realEstateTypeObject.hasBalconyProp) { - if (balcony != null) { - query.balcony = { - [Op.eq]: balcony - }; - } else { - checkForIncompleteWanted = true; - } - } - - if (realEstateTypeObject.hasNewBuildingProp) { - if (newBuilding != null) { - query.newBuilding = { - [Op.eq]: newBuilding - }; - } else { - checkForIncompleteWanted = true; - } - } - - if (realEstateTypeObject.hasElevatorProp) { - if (elevator != null) { - query.elevator = { - [Op.eq]: elevator - }; - } else { - checkForIncompleteWanted = true; - } - } - - //If one of the attributes that exists for property type is null - //we include in query to check if incomplete real estates are accepted + //Tag to check if incomplete ads are accepted in query if (checkForIncompleteWanted) { query.includeIncompleteAds = { [Op.eq]: true }; } - return await db.SearchRequest.findAll({ where: query }); + + //++ + console.log("Query koji koristimo:", query); + // + return await db.SearchRequest.findAll({ + where: query + }); }; module.exports = { diff --git a/app/services/notificationService.js b/app/services/notificationService.js index 7495e17..ab9f619 100644 --- a/app/services/notificationService.js +++ b/app/services/notificationService.js @@ -12,6 +12,8 @@ const { findNotNotifiedMatches } = require("../helpers/db/searchRequestMatch"); const { sendEmail } = require("../services/emailService"); const notifyForNewRealEstates = async newRealEstates => { + // + console.log("Real estates", newRealEstates); const matches = await matchRealEstates(newRealEstates); await notifyMatches(matches); };