find matching real estates for new search request and notify
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
"use strict";
|
||||
const db = require("../../models/index");
|
||||
const sequelize = require("sequelize");
|
||||
const Op = sequelize.Op;
|
||||
|
||||
const bulkUpsertRealEstates = async realEstateData => {
|
||||
try {
|
||||
@@ -26,10 +28,12 @@ const bulkUpsertRealEstates = async realEstateData => {
|
||||
"updatedAt",
|
||||
"renewedDate"
|
||||
];
|
||||
const order = [["updatedAt", "desc"]];
|
||||
|
||||
return await db.RealEstate.bulkCreate(realEstateData, {
|
||||
updateOnDuplicate: fieldsToUpdateIfDuplicate,
|
||||
returning: true
|
||||
returning: true,
|
||||
order
|
||||
});
|
||||
} catch (e) {
|
||||
console.log("Error bulk upserting realEstates : ", e);
|
||||
@@ -40,7 +44,68 @@ const getRealEstateById = async id => {
|
||||
return db.RealEstate.findByPk(id);
|
||||
};
|
||||
|
||||
const findRealEstatesForSearchRequest = async (searchRequest, maxResults) => {
|
||||
const {
|
||||
priceMin,
|
||||
priceMax,
|
||||
sizeMin,
|
||||
sizeMax,
|
||||
adType,
|
||||
realEstateType,
|
||||
areaToSearch
|
||||
} = searchRequest;
|
||||
|
||||
const longitudeColumn = sequelize.col("locationLong");
|
||||
const latitudeColumn = sequelize.col("locationLat");
|
||||
|
||||
const pointGeometry = sequelize.fn(
|
||||
"ST_Point",
|
||||
longitudeColumn,
|
||||
latitudeColumn
|
||||
);
|
||||
const pointWithSRID = sequelize.fn("ST_SetSRID", pointGeometry, 4326);
|
||||
const areaToSearchAsGeometry = sequelize.fn(
|
||||
"ST_GeomFromGeoJSON",
|
||||
JSON.stringify(areaToSearch)
|
||||
);
|
||||
const areaToSearchWithSRID = sequelize.fn(
|
||||
"ST_SetSRID",
|
||||
areaToSearchAsGeometry,
|
||||
4326
|
||||
);
|
||||
const contains = sequelize.fn(
|
||||
"ST_Contains",
|
||||
areaToSearchWithSRID,
|
||||
pointWithSRID
|
||||
);
|
||||
|
||||
const geoSearchQueryPart = sequelize.where(contains, true);
|
||||
|
||||
const query = {
|
||||
adType,
|
||||
realEstateType,
|
||||
price: {
|
||||
[Op.lte]: priceMax,
|
||||
[Op.gte]: priceMin
|
||||
},
|
||||
area: {
|
||||
[Op.lte]: sizeMax,
|
||||
[Op.gte]: sizeMin
|
||||
},
|
||||
[Op.and]: geoSearchQueryPart
|
||||
};
|
||||
|
||||
const order = [["updatedAt", "desc"]];
|
||||
|
||||
return await db.RealEstate.findAll({
|
||||
where: query,
|
||||
limit: maxResults,
|
||||
order
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
bulkUpsertRealEstates,
|
||||
getRealEstateById
|
||||
getRealEstateById,
|
||||
findRealEstatesForSearchRequest
|
||||
};
|
||||
|
||||
@@ -6,11 +6,14 @@ const findRealEstatesForSearchRequest = async searchRequestId => {
|
||||
searchRequestId
|
||||
};
|
||||
|
||||
const include = [{ model: db.RealEstate, as: "realEstates" }];
|
||||
const realEstatesModel = { model: db.RealEstate, as: "realEstates" };
|
||||
const order = [[realEstatesModel, "updatedAt", "desc"]];
|
||||
const include = [realEstatesModel];
|
||||
|
||||
const matches = await db.SearchRequestMatch.findAll({
|
||||
where: query,
|
||||
include
|
||||
include,
|
||||
order
|
||||
});
|
||||
|
||||
const matchingRealEstates = [];
|
||||
|
||||
Reference in New Issue
Block a user