2019-09-30 12:07:21 +02:00
|
|
|
"use strict";
|
|
|
|
|
const db = require("../../models/index");
|
|
|
|
|
|
|
|
|
|
const findRealEstatesForSearchRequest = async searchRequestId => {
|
|
|
|
|
const query = {
|
|
|
|
|
searchRequestId
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-07 20:49:54 +02:00
|
|
|
const realEstatesModel = { model: db.RealEstate, as: "realEstates" };
|
|
|
|
|
const order = [[realEstatesModel, "updatedAt", "desc"]];
|
|
|
|
|
const include = [realEstatesModel];
|
2019-09-30 12:07:21 +02:00
|
|
|
|
2019-09-30 13:20:38 +02:00
|
|
|
const matches = await db.SearchRequestMatch.findAll({
|
|
|
|
|
where: query,
|
2019-10-07 20:49:54 +02:00
|
|
|
include,
|
|
|
|
|
order
|
2019-09-30 13:20:38 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const matchingRealEstates = [];
|
|
|
|
|
for (const match of matches) {
|
|
|
|
|
matchingRealEstates.push(...match.realEstates);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return matchingRealEstates;
|
2019-09-30 12:07:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const addMatches = async matchingRecords => {
|
|
|
|
|
return await db.SearchRequestMatch.bulkCreate(matchingRecords, {
|
|
|
|
|
ignoreDuplicates: true
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
findRealEstatesForSearchRequest,
|
|
|
|
|
addMatches
|
|
|
|
|
};
|