149 lines
2.8 KiB
JavaScript
149 lines
2.8 KiB
JavaScript
"use strict";
|
|
const db = require("../../models/index");
|
|
const sequelize = require("sequelize");
|
|
const Op = sequelize.Op;
|
|
|
|
const getSearchRequest = async searchRequestId => {
|
|
try {
|
|
return await db.SearchRequest.findByPk(searchRequestId);
|
|
} catch (error) {
|
|
console.log("searchrequest.js", error);
|
|
return null;
|
|
}
|
|
};
|
|
|
|
const createSearchRequest = async (searchRequestFields = {}) => {
|
|
return await db.SearchRequest.create(searchRequestFields);
|
|
};
|
|
|
|
const findSearchRequestsForRealEstate = async realEstate => {
|
|
const {
|
|
price,
|
|
area,
|
|
adType,
|
|
realEstateType,
|
|
locationLat,
|
|
locationLong,
|
|
accessRoadType,
|
|
balcony,
|
|
newBuilding,
|
|
elevator,
|
|
gardenSize,
|
|
numberOfRooms,
|
|
numberOfFloors,
|
|
floor
|
|
} = realEstate;
|
|
|
|
if (!locationLat || !locationLong) {
|
|
return [];
|
|
}
|
|
|
|
const stGeometry = sequelize.fn(
|
|
"ST_GEOMFROMTEXT",
|
|
`POINT (${locationLong} ${locationLat})`,
|
|
4326
|
|
);
|
|
const areaToSearchColumn = sequelize.col("areaToSearch");
|
|
const contains = sequelize.fn("ST_Contains", areaToSearchColumn, stGeometry);
|
|
|
|
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
|
|
};
|
|
|
|
//Every other attribute is checked separately and included in query only if it is defined
|
|
if (price) {
|
|
query.priceMin = {
|
|
[Op.lte]: price
|
|
};
|
|
query.priceMax = {
|
|
[Op.gte]: price
|
|
};
|
|
}
|
|
|
|
if (area) {
|
|
query.sizeMin = {
|
|
[Op.lte]: area
|
|
};
|
|
query.sizeMax = {
|
|
[Op.gte]: area
|
|
};
|
|
}
|
|
|
|
if (gardenSize) {
|
|
query.gardenSizeMin = {
|
|
[Op.lte]: gardenSize
|
|
};
|
|
query.gardenSizeMax = {
|
|
[Op.gte]: gardenSize
|
|
};
|
|
}
|
|
|
|
if (numberOfRooms) {
|
|
query.numberOfRoomsMin = {
|
|
[Op.lte]: numberOfRooms
|
|
};
|
|
query.numberOfRoomsMax = {
|
|
[Op.gte]: numberOfRooms
|
|
};
|
|
}
|
|
|
|
if (numberOfFloors) {
|
|
query.numberOfFloorsMin = {
|
|
[Op.lte]: numberOfFloors
|
|
};
|
|
query.numberOfFloorsMax = {
|
|
[Op.gte]: numberOfFloors
|
|
};
|
|
}
|
|
|
|
if (floor) {
|
|
query.floorMin = {
|
|
[Op.lte]: floor
|
|
};
|
|
query.floorMax = {
|
|
[Op.gte]: floor
|
|
};
|
|
}
|
|
|
|
if (accessRoadType) {
|
|
query.accessRoadType = {
|
|
[Op.or]: {
|
|
[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 });
|
|
};
|
|
|
|
module.exports = {
|
|
getSearchRequest,
|
|
createSearchRequest,
|
|
findSearchRequestsForRealEstate
|
|
};
|