use matching service to find matches
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const {
|
const { matchRealEstates } = require("../services/searchMatchService");
|
||||||
findSearchRequestsForRealEstate
|
|
||||||
} = require("../helpers/db/searchRequest");
|
|
||||||
const {
|
const {
|
||||||
generateNotificationEmail,
|
generateNotificationEmail,
|
||||||
generateNewSearchRequestEmail
|
generateNewSearchRequestEmail
|
||||||
@@ -9,39 +7,24 @@ const {
|
|||||||
const { sendEmail } = require("../services/emailService");
|
const { sendEmail } = require("../services/emailService");
|
||||||
|
|
||||||
const notifyForNewRealEstates = async newRealEstates => {
|
const notifyForNewRealEstates = async newRealEstates => {
|
||||||
const matches = {};
|
const matches = await matchRealEstates(newRealEstates);
|
||||||
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
await Promise.all(asyncSearchActions);
|
|
||||||
|
|
||||||
const searchRequestsToNotify = Object.keys(matches);
|
const searchRequestsToNotify = Object.keys(matches);
|
||||||
|
|
||||||
|
const asyncSendEmailActions = [];
|
||||||
for (const id of searchRequestsToNotify) {
|
for (const id of searchRequestsToNotify) {
|
||||||
const { searchRequest } = matches[id];
|
const { searchRequest } = matches[id];
|
||||||
const { email } = searchRequest;
|
const { email } = searchRequest;
|
||||||
const allMatchingRealEstates = matches[id].realEstates || [];
|
const allMatchingRealEstates = matches[id].realEstates || [];
|
||||||
const emailContent = generateNotificationEmail(allMatchingRealEstates, id);
|
const emailContent = generateNotificationEmail(allMatchingRealEstates, id);
|
||||||
|
|
||||||
const res = await sendEmail(email, "Nove nekretnine", emailContent);
|
const sendEmailPromise = sendEmail(email, "Nove nekretnine", emailContent);
|
||||||
console.log("Email sent, result :", res);
|
asyncSendEmailActions.push(sendEmailPromise);
|
||||||
|
sendEmailPromise
|
||||||
|
.then(res => console.log(res))
|
||||||
|
.catch(err => console.log(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await Promise.all(asyncSendEmailActions);
|
||||||
};
|
};
|
||||||
|
|
||||||
const notifyForNewSearchRequest = async searchRequest => {
|
const notifyForNewSearchRequest = async searchRequest => {
|
||||||
|
|||||||
Reference in New Issue
Block a user