2019-09-30 10:28:29 +02:00
|
|
|
"use strict";
|
2019-10-07 20:49:54 +02:00
|
|
|
const {
|
|
|
|
|
matchRealEstates,
|
|
|
|
|
matchSearchRequest
|
|
|
|
|
} = require("../services/searchMatchService");
|
2019-09-30 10:28:29 +02:00
|
|
|
const {
|
|
|
|
|
generateNotificationEmail,
|
2019-10-11 23:07:45 +02:00
|
|
|
generateNewSearchRequestEmail,
|
|
|
|
|
generateEmailSubject
|
2019-09-30 10:28:29 +02:00
|
|
|
} = require("../helpers/emailContentGenerator");
|
2020-01-06 23:59:56 +01:00
|
|
|
const {
|
|
|
|
|
findNotNotifiedMatches,
|
|
|
|
|
findRealEstatesForSearchRequest
|
|
|
|
|
} = require("../helpers/db/searchRequestMatch");
|
2019-09-30 10:28:29 +02:00
|
|
|
const { sendEmail } = require("../services/emailService");
|
|
|
|
|
|
|
|
|
|
const notifyForNewRealEstates = async newRealEstates => {
|
2019-09-30 12:08:16 +02:00
|
|
|
const matches = await matchRealEstates(newRealEstates);
|
2019-10-07 20:49:54 +02:00
|
|
|
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;
|
2019-10-11 23:07:45 +02:00
|
|
|
await sendEmail(email, "Kivi - novi zahtjev za pretragu", emailContent);
|
2019-10-07 20:49:54 +02:00
|
|
|
};
|
|
|
|
|
|
2019-11-04 14:28:11 +01:00
|
|
|
const notifyMatches = async (matches, dailyNotification = false) => {
|
2019-09-30 10:28:29 +02:00
|
|
|
const searchRequestsToNotify = Object.keys(matches);
|
|
|
|
|
|
2019-09-30 12:08:16 +02:00
|
|
|
const asyncSendEmailActions = [];
|
2019-09-30 10:28:29 +02:00
|
|
|
for (const id of searchRequestsToNotify) {
|
2019-11-04 11:02:26 +01:00
|
|
|
const { searchRequest, notifyNow } = matches[id];
|
2019-11-05 07:54:23 +01:00
|
|
|
const { email, subscribed } = searchRequest;
|
|
|
|
|
if (notifyNow && subscribed) {
|
2019-11-04 11:02:26 +01:00
|
|
|
const allMatchingRealEstates = matches[id].realEstates || [];
|
2020-01-06 23:59:56 +01:00
|
|
|
|
|
|
|
|
//Variable allMatchingRealEstates are real estates that are "new" on the market
|
|
|
|
|
//the ones that we notify user in this moment, not all that already exists in db
|
|
|
|
|
//New variable allRealEstates are all real estates that exists in db for search req
|
|
|
|
|
const allRealEstates = await findRealEstatesForSearchRequest(id);
|
|
|
|
|
const noAllRealEstates = allRealEstates.length;
|
|
|
|
|
|
2019-11-04 11:02:26 +01:00
|
|
|
if (allMatchingRealEstates.length > 0) {
|
|
|
|
|
const emailContent = generateNotificationEmail(
|
|
|
|
|
allMatchingRealEstates,
|
2019-11-04 14:28:11 +01:00
|
|
|
id,
|
2020-01-06 23:59:56 +01:00
|
|
|
noAllRealEstates,
|
2019-11-04 14:28:11 +01:00
|
|
|
dailyNotification
|
2019-11-04 11:02:26 +01:00
|
|
|
);
|
|
|
|
|
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)
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-10-11 23:07:45 +02:00
|
|
|
}
|
2019-09-30 10:28:29 +02:00
|
|
|
}
|
2019-09-30 12:08:16 +02:00
|
|
|
|
|
|
|
|
await Promise.all(asyncSendEmailActions);
|
2019-09-30 10:28:29 +02:00
|
|
|
};
|
|
|
|
|
|
2019-11-04 11:02:26 +01:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 14:28:11 +01:00
|
|
|
await notifyMatches(matches, true);
|
2019-11-04 11:02:26 +01:00
|
|
|
};
|
|
|
|
|
|
2019-09-30 10:28:29 +02:00
|
|
|
module.exports = {
|
|
|
|
|
notifyForNewRealEstates,
|
2019-11-04 11:02:26 +01:00
|
|
|
notifyForNewSearchRequest,
|
|
|
|
|
notifyRequestsWithDailyOption
|
2019-09-30 10:28:29 +02:00
|
|
|
};
|