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