add notificationService to handle when, what and to whom to send email
This commit is contained in:
@@ -1,28 +1,56 @@
|
|||||||
const db = require("../models/index");
|
"use strict";
|
||||||
const { allMarketAlerts } = require("../helpers/db/dbHelper");
|
|
||||||
const {
|
const {
|
||||||
createMarketAlertEmailTemplate,
|
findSearchRequestsForRealEstate
|
||||||
sendBulkEmail
|
} = require("../helpers/db/searchRequest");
|
||||||
} = require("../helpers/awsEmail");
|
const {
|
||||||
|
generateNotificationEmail,
|
||||||
|
generateNewSearchRequestEmail
|
||||||
|
} = require("../helpers/emailContentGenerator");
|
||||||
|
const { sendEmail } = require("../services/emailService");
|
||||||
|
|
||||||
async function processNotifications() {
|
const notifyForNewRealEstates = async newRealEstates => {
|
||||||
try {
|
const matches = {};
|
||||||
const marketAlerts = await allMarketAlerts(false, false);
|
const asyncSearchActions = [];
|
||||||
await createMarketAlertEmailTemplate();
|
for (const realEstate of newRealEstates) {
|
||||||
if (marketAlerts.length > 0) {
|
const searchRequestsPromise = findSearchRequestsForRealEstate(realEstate);
|
||||||
await sendBulkEmail(marketAlerts);
|
|
||||||
}
|
|
||||||
|
|
||||||
await db.MarketAlert.update(
|
asyncSearchActions.push(searchRequestsPromise);
|
||||||
{ notified: true } /* set attributes' value */,
|
searchRequestsPromise.then(searchRequests => {
|
||||||
{ where: { notified: false } } /* where criteria */
|
for (const searchRequest of searchRequests) {
|
||||||
);
|
const { id } = searchRequest;
|
||||||
} catch (e) {
|
if (!matches[id]) {
|
||||||
console.log(
|
matches[id] = {
|
||||||
"NOTIFICATION SERVICE: could not send notifications reason: ",
|
searchRequest,
|
||||||
e
|
realEstates: []
|
||||||
);
|
};
|
||||||
|
}
|
||||||
|
matches[id].realEstates.push(realEstate);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = processNotifications;
|
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
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user