First review changes: applied prettier, ternary and changed accesRoadType filter

This commit is contained in:
Naida Vatric
2019-12-17 11:18:58 +01:00
parent 5a2fdb7291
commit c672b3ab9f
5 changed files with 94 additions and 77 deletions

View File

@@ -2,7 +2,6 @@
const db = require("../../models/index");
const sequelize = require("sequelize");
const Op = sequelize.Op;
const bulkUpsertRealEstates = async realEstateData => {
try {
const fieldsToUpdateIfDuplicate = [
@@ -131,42 +130,36 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
//General queries contain only attributes that are defined for every searchreq
//Query for case of complete ads
//Query for case of complete ads
const query = {
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
}
adType,
realEstateType,
price: {
[Op.lte]: priceMax,
[Op.gte]: priceMin
},
area: {
[Op.lte]: sizeMax,
[Op.gte]: sizeMin
},
[Op.and]: geoSearchQueryPart
};
//Query for case of incomplete ads
const queryIncludeIncomplete = {
adType,
realEstateType,
price: {
[Op.or] : {
[Op.and] : {
[Op.or]: {
[Op.and]: {
[Op.lte]: priceMax,
[Op.gte]: priceMin
},
[Op.is] : null
[Op.is]: null
}
},
area: {
[Op.or] : {
[Op.or]: {
[Op.and]: {
[Op.lte]: sizeMax,
[Op.gte]: sizeMin
@@ -174,133 +167,129 @@ const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
[Op.is]: null
}
},
accessRoadType: {
[Op.or]: {
[Op.eq]: 'ANY',
[Op.eq]: accessRoadType,
[Op.is]: null
}
},
[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.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.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.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.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
}
}
};
}
if (newBuilding) {
query.newBuilding = {
[Op.eq]: newBuilding
}
};
queryIncludeIncomplete.newBuilding = {
[Op.or]: {
[Op.eq]: newBuilding,
[Op.is]: null
}
}
};
}
if (elevator) {
query.elevator = {
[Op.eq]: elevator
}
};
queryIncludeIncomplete.elevator = {
[Op.or]: {
[Op.eq]: elevator,
[Op.is]: null
}
}
};
}
if (accessRoadType !== "ANY") {
query.accessRoadType = {
[Op.eq]: accessRoadType
};
queryIncludeIncomplete.accessRoadType = {
[Op.or]: {
[Op.eq]: accessRoadType,
[Op.is]: null
}
};
}
const order = [["updatedAt", "desc"]];
if(!includeIncompleteAds) {
return await db.RealEstate.findAll({
where: query,
limit: maxResults,
order
});
} else {
return await db.RealEstate.findAll({
where: queryIncludeIncomplete,
limit: maxResults,
order
});
}
return db.RealEstate.findAll({
where: includeIncompleteAds ? queryIncludeIncomplete : query,
limit: maxResults,
order
});
};
module.exports = {

View File

@@ -5,11 +5,9 @@ const Op = sequelize.Op;
const getSearchRequest = async searchRequestId => {
try {
return await db.SearchRequest.findByPk(searchRequestId);
} catch (error) {
console.log("searchrequest.js",error);
console.log("searchrequest.js", error);
return null;
}
};
@@ -116,30 +114,30 @@ const findSearchRequestsForRealEstate = async realEstate => {
if (accessRoadType) {
query.accessRoadType = {
[Op.or]: {
[Op.eq]: 'ANY',
[Op.eq]: "ANY",
[Op.eq]: accessRoadType
}
}
};
}
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 });
};