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 = [];
|
||||
|
||||
@@ -10,6 +10,16 @@ const generateEmailFooter = searchRequestId => {
|
||||
<strong>Vaš,<br/>Javimi tim</strong>`;
|
||||
};
|
||||
|
||||
const generateRealEstateLinks = realEstates => {
|
||||
let realEstateLinks = "";
|
||||
for (const realEstate of realEstates) {
|
||||
const { id: realEstateId, title } = realEstate;
|
||||
|
||||
realEstateLinks += `<li><a href="${APP_URL}/redirect/${realEstateId}">${title}</a></li>`;
|
||||
}
|
||||
return realEstateLinks;
|
||||
};
|
||||
|
||||
const generateNotificationEmail = (realEstates, searchRequestId) => {
|
||||
const truncateList = realEstates.length > MAX_REAL_ESTATES_IN_EMAIL;
|
||||
const realEstatesToShow = truncateList
|
||||
@@ -17,16 +27,8 @@ const generateNotificationEmail = (realEstates, searchRequestId) => {
|
||||
: realEstates;
|
||||
|
||||
const allRealEstatesLink = `${APP_URL}/nekretnine/${searchRequestId}`;
|
||||
|
||||
let realEstateLinks = "";
|
||||
for (const realEstate of realEstatesToShow) {
|
||||
const { id: realEstateId, title } = realEstate;
|
||||
|
||||
realEstateLinks += `<li><a href="${APP_URL}/redirect/${realEstateId}">${title}</a></li><br />`;
|
||||
}
|
||||
|
||||
const realEstateLinks = generateRealEstateLinks(realEstatesToShow);
|
||||
const moreRealEstates = `<div>Kompletan spisak nekretnina možete pegledati na <a href="${allRealEstatesLink}">listi nekretnina</a><div>`;
|
||||
|
||||
const emailFooter = generateEmailFooter(searchRequestId);
|
||||
|
||||
return `<h3>Zdravo</h3>
|
||||
@@ -40,7 +42,7 @@ const generateNotificationEmail = (realEstates, searchRequestId) => {
|
||||
${emailFooter}`;
|
||||
};
|
||||
|
||||
const generateNewSearchRequestEmail = searchRequest => {
|
||||
const generateNewSearchRequestEmail = (searchRequest, matchingRealEstates) => {
|
||||
const realEstateType = AD_CATEGORY[searchRequest.realEstateType];
|
||||
const {
|
||||
id,
|
||||
@@ -52,8 +54,15 @@ const generateNewSearchRequestEmail = searchRequest => {
|
||||
priceMax
|
||||
} = searchRequest;
|
||||
|
||||
const realEstateLinks = generateRealEstateLinks(matchingRealEstates);
|
||||
const instantRealEstatesText = `<br/>
|
||||
<div>
|
||||
U međuvremenu pogledajte neke od nedavno objavljenih nekretnina koje odgovaraju Vašim uslovima pretrage :<br/>
|
||||
${realEstateLinks}
|
||||
</div>`;
|
||||
|
||||
const gardenSize = realEstateType.hasGardenSize
|
||||
? `<div><strong>Kvadratura okućnice: Od ${gardenSizeMin} do ${gardenSizeMax} m2 </strong></div>`
|
||||
? `<div><strong>Kvadratura okućnice: Od ${gardenSizeMin} do ${gardenSizeMax} m2</strong></div>`
|
||||
: ``;
|
||||
|
||||
const emailFooter = generateEmailFooter(id);
|
||||
@@ -68,6 +77,7 @@ const generateNewSearchRequestEmail = searchRequest => {
|
||||
${gardenSize}
|
||||
<div><strong>Cijena:</strong> ${priceMin} do ${priceMax} KM</div>
|
||||
</div>
|
||||
${matchingRealEstates.length > 0 ? instantRealEstatesText : ""}
|
||||
<br/>
|
||||
${emailFooter}`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user