add service to find real estate - search request matches
This commit is contained in:
45
app/services/searchMatchService.js
Normal file
45
app/services/searchMatchService.js
Normal file
@@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
|
||||
const {
|
||||
findSearchRequestsForRealEstate
|
||||
} = require("../helpers/db/searchRequest");
|
||||
const { addMatches } = require("../helpers/db/searchRequestMatch");
|
||||
|
||||
const matchRealEstates = async realEstates => {
|
||||
if (Array.isArray(realEstates)) {
|
||||
const asyncMatchActions = [];
|
||||
const matches = {};
|
||||
const matchingRecords = [];
|
||||
for (const realEstate of realEstates) {
|
||||
const searchRequestsPromise = findSearchRequestsForRealEstate(realEstate);
|
||||
asyncMatchActions.push(searchRequestsPromise);
|
||||
|
||||
searchRequestsPromise.then(searchRequests => {
|
||||
for (const searchRequest of searchRequests) {
|
||||
const { id } = searchRequest;
|
||||
if (!matches[id]) {
|
||||
matches[id] = {
|
||||
searchRequest,
|
||||
realEstates: []
|
||||
};
|
||||
}
|
||||
matches[id].realEstates.push(realEstate);
|
||||
matchingRecords.push({
|
||||
searchRequestId: searchRequest.id,
|
||||
realEstateId: realEstate.id,
|
||||
notified: false
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
await Promise.all(asyncMatchActions);
|
||||
|
||||
await addMatches(matchingRecords);
|
||||
return matches;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
matchRealEstates
|
||||
};
|
||||
Reference in New Issue
Block a user