2019-09-30 10:28:29 +02:00
|
|
|
"use strict";
|
2019-09-05 11:14:54 +02:00
|
|
|
const {
|
2019-09-30 10:28:29 +02:00
|
|
|
findSearchRequestsForRealEstate
|
|
|
|
|
} = require("../helpers/db/searchRequest");
|
|
|
|
|
const {
|
|
|
|
|
generateNotificationEmail,
|
|
|
|
|
generateNewSearchRequestEmail
|
|
|
|
|
} = require("../helpers/emailContentGenerator");
|
|
|
|
|
const { sendEmail } = require("../services/emailService");
|
|
|
|
|
|
|
|
|
|
const notifyForNewRealEstates = async newRealEstates => {
|
|
|
|
|
const matches = {};
|
|
|
|
|
const asyncSearchActions = [];
|
|
|
|
|
for (const realEstate of newRealEstates) {
|
|
|
|
|
const searchRequestsPromise = findSearchRequestsForRealEstate(realEstate);
|
|
|
|
|
|
|
|
|
|
asyncSearchActions.push(searchRequestsPromise);
|
|
|
|
|
searchRequestsPromise.then(searchRequests => {
|
|
|
|
|
for (const searchRequest of searchRequests) {
|
|
|
|
|
const { id } = searchRequest;
|
|
|
|
|
if (!matches[id]) {
|
|
|
|
|
matches[id] = {
|
|
|
|
|
searchRequest,
|
|
|
|
|
realEstates: []
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
matches[id].realEstates.push(realEstate);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-09-05 11:14:54 +02:00
|
|
|
}
|
2019-06-25 17:06:07 +02:00
|
|
|
|
2019-09-30 10:28:29 +02:00
|
|
|
await Promise.all(asyncSearchActions);
|
|
|
|
|
|
|
|
|
|
const searchRequestsToNotify = Object.keys(matches);
|
|
|
|
|
|
|
|
|
|
for (const id of searchRequestsToNotify) {
|
|
|
|
|
const { searchRequest } = matches[id];
|
|
|
|
|
const { email } = searchRequest;
|
|
|
|
|
const allMatchingRealEstates = matches[id].realEstates || [];
|
|
|
|
|
const emailContent = generateNotificationEmail(allMatchingRealEstates, id);
|
|
|
|
|
|
|
|
|
|
const res = await sendEmail(email, "Nove nekretnine", emailContent);
|
|
|
|
|
console.log("Email sent, result :", res);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const notifyForNewSearchRequest = async searchRequest => {
|
|
|
|
|
const emailContent = generateNewSearchRequestEmail(searchRequest);
|
|
|
|
|
const { email } = searchRequest;
|
|
|
|
|
await sendEmail(email, "Market Alert", emailContent);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
notifyForNewRealEstates,
|
|
|
|
|
notifyForNewSearchRequest
|
|
|
|
|
};
|