find matching real estates for new search request and notify
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
"use strict";
|
||||
const { matchRealEstates } = require("../services/searchMatchService");
|
||||
const {
|
||||
matchRealEstates,
|
||||
matchSearchRequest
|
||||
} = require("../services/searchMatchService");
|
||||
const {
|
||||
generateNotificationEmail,
|
||||
generateNewSearchRequestEmail
|
||||
@@ -8,6 +11,24 @@ 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, "Market Alert", emailContent);
|
||||
};
|
||||
|
||||
const notifyMatches = async matches => {
|
||||
const searchRequestsToNotify = Object.keys(matches);
|
||||
|
||||
const asyncSendEmailActions = [];
|
||||
@@ -27,12 +48,6 @@ const notifyForNewRealEstates = async newRealEstates => {
|
||||
await Promise.all(asyncSendEmailActions);
|
||||
};
|
||||
|
||||
const notifyForNewSearchRequest = async searchRequest => {
|
||||
const emailContent = generateNewSearchRequestEmail(searchRequest);
|
||||
const { email } = searchRequest;
|
||||
await sendEmail(email, "Market Alert", emailContent);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
notifyForNewRealEstates,
|
||||
notifyForNewSearchRequest
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
const {
|
||||
findSearchRequestsForRealEstate
|
||||
} = require("../helpers/db/searchRequest");
|
||||
const { findRealEstatesForSearchRequest } = require("../helpers/db/realEstate");
|
||||
const { addMatches } = require("../helpers/db/searchRequestMatch");
|
||||
const { MAX_REAL_ESTATES_IN_FIRST_EMAIL } = require("../config/appConfig");
|
||||
|
||||
const matchRealEstates = async realEstates => {
|
||||
if (Array.isArray(realEstates)) {
|
||||
@@ -40,6 +42,35 @@ const matchRealEstates = async realEstates => {
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
matchRealEstates
|
||||
const matchSearchRequest = async searchRequest => {
|
||||
const { id: searchRequestId } = searchRequest;
|
||||
|
||||
const realEstates = await findRealEstatesForSearchRequest(
|
||||
searchRequest,
|
||||
MAX_REAL_ESTATES_IN_FIRST_EMAIL
|
||||
);
|
||||
const matches = {
|
||||
[searchRequestId]: {
|
||||
searchRequest,
|
||||
realEstates: []
|
||||
}
|
||||
};
|
||||
const matchingRecords = [];
|
||||
|
||||
for (const realEstate of realEstates) {
|
||||
matches[searchRequestId].realEstates.push(realEstate);
|
||||
matchingRecords.push({
|
||||
searchRequestId,
|
||||
realEstateId: realEstate.id,
|
||||
notified: false
|
||||
});
|
||||
}
|
||||
|
||||
await addMatches(matchingRecords);
|
||||
return matches;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
matchRealEstates,
|
||||
matchSearchRequest
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user