63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
"use strict";
|
|
const {
|
|
matchRealEstates,
|
|
matchSearchRequest
|
|
} = require("../services/searchMatchService");
|
|
const {
|
|
generateNotificationEmail,
|
|
generateNewSearchRequestEmail,
|
|
generateEmailSubject
|
|
} = require("../helpers/emailContentGenerator");
|
|
const { sendEmail } = require("../services/emailService");
|
|
|
|
const notifyForNewRealEstates = async newRealEstates => {
|
|
const matches = await matchRealEstates(newRealEstates);
|
|
await notifyMatches(matches);
|
|
};
|
|
|
|
const notifyForNewSearchRequest = async searchRequest => {
|
|
const matches = await matchSearchRequest(searchRequest);
|
|
|
|
const searchRequestId = searchRequest.id;
|
|
const matchingRealEstates = matches[searchRequestId].realEstates;
|
|
|
|
const emailContent = generateNewSearchRequestEmail(
|
|
searchRequest,
|
|
matchingRealEstates
|
|
);
|
|
const { email } = searchRequest;
|
|
await sendEmail(email, "Kivi - novi zahtjev za pretragu", emailContent);
|
|
};
|
|
|
|
const notifyMatches = async matches => {
|
|
const searchRequestsToNotify = Object.keys(matches);
|
|
|
|
const asyncSendEmailActions = [];
|
|
for (const id of searchRequestsToNotify) {
|
|
const { searchRequest } = matches[id];
|
|
const { email } = searchRequest;
|
|
const allMatchingRealEstates = matches[id].realEstates || [];
|
|
if (allMatchingRealEstates.length > 0) {
|
|
const emailContent = generateNotificationEmail(
|
|
allMatchingRealEstates,
|
|
id
|
|
);
|
|
const emailSubject = generateEmailSubject(
|
|
allMatchingRealEstates.length,
|
|
allMatchingRealEstates[0].title
|
|
);
|
|
|
|
const sendEmailPromise = sendEmail(email, emailSubject, emailContent);
|
|
asyncSendEmailActions.push(sendEmailPromise);
|
|
sendEmailPromise.catch(err => console.log("[Email Sending Failed]", err));
|
|
}
|
|
}
|
|
|
|
await Promise.all(asyncSendEmailActions);
|
|
};
|
|
|
|
module.exports = {
|
|
notifyForNewRealEstates,
|
|
notifyForNewSearchRequest
|
|
};
|