WIP Changed all logic for searchRequest.

This commit is contained in:
Naida Vatric
2020-01-17 01:54:06 +01:00
parent 4fd4018bf6
commit 870b71a3c7
3 changed files with 352 additions and 88 deletions

View File

@@ -104,6 +104,9 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
accessRoadType accessRoadType
} = searchRequest; } = searchRequest;
// ++ testing
console.log("SearchRequest za koji trazimo:", searchRequest);
//Needed for defining which attribute should exist or not //Needed for defining which attribute should exist or not
const realEstateTypeObject = AD_CATEGORY[realEstateType]; 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 = { query.numberOfRooms = {
[Op.lte]: numberOfRoomsMax, [Op.lte]: numberOfRoomsMax,
[Op.gte]: numberOfRoomsMin [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 = { query.numberOfFloors = {
[Op.lte]: numberOfFloorsMax, [Op.lte]: numberOfFloorsMax,
[Op.gte]: numberOfFloorsMin [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 = { query.floor = {
[Op.lte]: floorMax, [Op.lte]: floorMax,
[Op.gte]: floorMin [Op.gte]: floorMin
@@ -249,8 +264,10 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
} }
}; };
} }
//Logic for balcony, newBuilding and elevator from users side
if (realEstateTypeObject.hasBalconyProp && balcony != null) { //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 = { query.balcony = {
[Op.eq]: 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 = { query.newBuilding = {
[Op.eq]: 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 = { query.elevator = {
[Op.eq]: elevator [Op.eq]: elevator
}; };
@@ -301,6 +318,12 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
const order = [["updatedAt", "desc"]]; const order = [["updatedAt", "desc"]];
//++
console.log(
"Query user to find real estates:",
includeIncompleteAds ? queryIncludeIncomplete : query
);
return db.RealEstate.findAll({ return db.RealEstate.findAll({
where: includeIncompleteAds ? queryIncludeIncomplete : query, where: includeIncompleteAds ? queryIncludeIncomplete : query,
limit: maxResults, limit: maxResults,

View File

@@ -49,99 +49,364 @@ const findSearchRequestsForRealEstate = async realEstate => {
const geoSearchQueryPart = sequelize.where(contains, true); 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 //Needed for defining which attribute should exist or not
const realEstateTypeObject = AD_CATEGORY[realEstateType]; const realEstateTypeObject = AD_CATEGORY[realEstateType];
//Needed to decide on including incomplete RealEstates data
// ?? Needed to decide on including incomplete RealEstates data
let checkForIncompleteWanted = false; 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) { if (price != null) {
query.priceMin = { priceQuery = {
[Op.lte]: price [Op.and]: [
}; {
query.priceMax = { priceMin: {
[Op.gte]: price [Op.lte]: price
}
},
{
priceMax: {
[Op.gte]: price
}
}
]
}; };
} }
let areaQuery = {};
if (area != null) { if (area != null) {
query.sizeMin = { areaQuery = {
[Op.lte]: area [Op.and]: [
}; {
query.sizeMax = { sizeMin: {
[Op.gte]: area [Op.lte]: area
}
},
{
sizeMax: {
[Op.gte]: area
}
}
]
}; };
} else { } else {
checkForIncompleteWanted = true; checkForIncompleteWanted = true;
} }
//Other attributes can be defined or not depending on RealEstate type //Other attributes can be defined or not depending on RealEstate type
//we check what to include in query based on real estate type object //we check what to include in query based on real estate type object
let gardenSizeQuery = {};
if (realEstateTypeObject.hasGardenSize) { if (realEstateTypeObject.hasGardenSize) {
if (gardenSize != null) { if (gardenSize != null) {
query.gardenSizeMin = { gardenSizeQuery = {
[Op.lte]: gardenSize [Op.and]: [
}; {
query.gardenSizeMax = { gardenSizeMin: {
[Op.gte]: gardenSize [Op.lte]: gardenSize
}
},
{
gardenSizeMax: {
[Op.gte]: gardenSize
}
}
]
}; };
} else { } else {
checkForIncompleteWanted = true; checkForIncompleteWanted = true;
} }
} }
let numberOfRoomsQuery = {};
if (realEstateTypeObject.hasNumberOfRoom) { if (realEstateTypeObject.hasNumberOfRoom) {
if (numberOfRooms != null) { if (numberOfRooms != null) {
query.numberOfRoomsMin = { //If real estate has defined number of rooms ex. 3 it returns req
[Op.lte]: numberOfRooms // that accepts 3 rooms or ones that don't have defined number - null
}; //Ex. they didnt choose advanced filters at all
query.numberOfRoomsMax = { numberOfRoomsQuery = {
[Op.gte]: numberOfRooms [Op.and]: [
{
numberOfRoomsMin: {
[Op.or]: {
[Op.lte]: numberOfRooms,
[Op.is]: null
}
}
},
{
numberOfRoomsMax: {
[Op.or]: {
[Op.gte]: numberOfRooms,
[Op.is]: null
}
}
}
]
}; };
} else { } 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 (realEstateTypeObject.hasNumberOfFloors) {
if (numberOfFloors != null) { if (numberOfFloors != null) {
query.numberOfFloorsMin = { numberOfFloorsQuery = {
[Op.lte]: numberOfFloors [Op.and]: [
}; {
query.numberOfFloorsMax = { numberOfFloorsMin: {
[Op.gte]: numberOfFloors [Op.or]: {
[Op.lte]: numberOfFloors,
[Op.is]: null
}
}
},
{
numberOfFloorsMax: {
[Op.or]: {
[Op.gte]: numberOfFloors,
[Op.is]: null
}
}
}
]
}; };
} else { } 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) { //Logic for balcony, newBuilding and elevator
if (floor != null) { //If user dont check checkbox for ex. elevator it does not mean he only wants no elevator
query.floorMin = { //If real estate characteristic =true find all req, one that wants charachertistic or dont care - dont need query
[Op.lte]: floor //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 = { } else if (balcony === null) {
[Op.gte]: floor 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 //AccessRoadType is defined - should exists for each ad and estate type
if (accessRoadType != null) { if (accessRoadType != null) {
query.accessRoadType = { query.accessRoadType = {
[Op.or]: { [Op.or]: {
[Op.eq]: "ANY", [Op.like]: "ANY",
[Op.eq]: accessRoadType [Op.eq]: accessRoadType
} }
}; };
@@ -151,45 +416,19 @@ const findSearchRequestsForRealEstate = async realEstate => {
[Op.eq]: "ANY" [Op.eq]: "ANY"
}; };
} }
//Tag to check if incomplete ads are accepted in query
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
if (checkForIncompleteWanted) { if (checkForIncompleteWanted) {
query.includeIncompleteAds = { query.includeIncompleteAds = {
[Op.eq]: true [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 = { module.exports = {

View File

@@ -12,6 +12,8 @@ const { findNotNotifiedMatches } = require("../helpers/db/searchRequestMatch");
const { sendEmail } = require("../services/emailService"); const { sendEmail } = require("../services/emailService");
const notifyForNewRealEstates = async newRealEstates => { const notifyForNewRealEstates = async newRealEstates => {
//
console.log("Real estates", newRealEstates);
const matches = await matchRealEstates(newRealEstates); const matches = await matchRealEstates(newRealEstates);
await notifyMatches(matches); await notifyMatches(matches);
}; };