add method for sending daily notifications
This commit is contained in:
@@ -8,6 +8,7 @@ const {
|
||||
generateNewSearchRequestEmail,
|
||||
generateEmailSubject
|
||||
} = require("../helpers/emailContentGenerator");
|
||||
const { findNotNotifiedMatches } = require("../helpers/db/searchRequestMatch");
|
||||
const { sendEmail } = require("../services/emailService");
|
||||
|
||||
const notifyForNewRealEstates = async newRealEstates => {
|
||||
@@ -34,29 +35,81 @@ const notifyMatches = async 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 { searchRequest, notifyNow } = matches[id];
|
||||
if (notifyNow) {
|
||||
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));
|
||||
const sendEmailPromise = sendEmail(email, emailSubject, emailContent);
|
||||
asyncSendEmailActions.push(sendEmailPromise);
|
||||
sendEmailPromise.catch(err =>
|
||||
console.log("[Email Sending Failed]", err)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(asyncSendEmailActions);
|
||||
};
|
||||
|
||||
const notifyRequestsWithDailyOption = async () => {
|
||||
const notNotifiedSearchRequestMatches = await findNotNotifiedMatches();
|
||||
|
||||
const matches = {};
|
||||
|
||||
for (const searchRequestMatch of notNotifiedSearchRequestMatches) {
|
||||
const { searchRequests, realEstates } = searchRequestMatch;
|
||||
|
||||
if (!Array.isArray(searchRequests) || searchRequests.length !== 1) {
|
||||
// Something is wrong with this match
|
||||
// (search request not found for specified search request id)
|
||||
// OR
|
||||
// there are multiple search requests with the same ID (this should never be the case !
|
||||
// TODO: Maybe if association is defined better, this will be automatically only one object instead of array
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Array.isArray(realEstates) || realEstates.length !== 1) {
|
||||
// Something is wrong with this match
|
||||
// (real estate not found for specified real estate id)
|
||||
// OR
|
||||
// there are multiple real estates with the same ID (this should never be the case !
|
||||
// TODO: Maybe if association is defined better, this will be automatically only one object instead of array
|
||||
continue;
|
||||
}
|
||||
|
||||
const searchRequest = searchRequests[0];
|
||||
const realEstate = realEstates[0];
|
||||
const searchRequestId = searchRequest.id;
|
||||
|
||||
if (!matches[searchRequestId]) {
|
||||
matches[searchRequestId] = {
|
||||
searchRequest,
|
||||
realEstates: [],
|
||||
notifyNow: true
|
||||
};
|
||||
}
|
||||
|
||||
matches[searchRequestId].realEstates.push(realEstate);
|
||||
|
||||
searchRequestMatch.notified = true;
|
||||
searchRequestMatch.save();
|
||||
}
|
||||
|
||||
await notifyMatches(matches);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
notifyForNewRealEstates,
|
||||
notifyForNewSearchRequest
|
||||
notifyForNewSearchRequest,
|
||||
notifyRequestsWithDailyOption
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user