66 lines
1.4 KiB
JavaScript
66 lines
1.4 KiB
JavaScript
"use strict";
|
|
const db = require("../../models/index");
|
|
const sequelize = require("sequelize");
|
|
|
|
const bulkUpsertRealEstates = async realEstateData => {
|
|
try {
|
|
const fieldsToUpdateIfDuplicate = [
|
|
"realEstateType",
|
|
"adType",
|
|
"price",
|
|
"area",
|
|
"streetNumber",
|
|
"streetName",
|
|
"locality",
|
|
"municipality",
|
|
"city",
|
|
"region",
|
|
"entity",
|
|
"country",
|
|
"locationLat",
|
|
"locationLong",
|
|
"title",
|
|
"shortDescription",
|
|
"longDescription",
|
|
"gardenSize",
|
|
"adStatus",
|
|
"updatedAt",
|
|
"renewedDate"
|
|
];
|
|
|
|
return await db.RealEstate.bulkCreate(realEstateData, {
|
|
updateOnDuplicate: fieldsToUpdateIfDuplicate,
|
|
returning: true
|
|
});
|
|
} catch (e) {
|
|
console.log("Error bulk upserting realEstates : ", e);
|
|
}
|
|
};
|
|
|
|
const checkIfAlreadyExist = async realEstateData => {
|
|
const orQueryPart = [];
|
|
|
|
for (const realEstate of realEstateData) {
|
|
const { agencyObjectId, originAgencyName } = realEstate;
|
|
|
|
const singleRealEstateQueryPart = {
|
|
agencyObjectId,
|
|
originAgencyName
|
|
};
|
|
|
|
orQueryPart.push(singleRealEstateQueryPart);
|
|
}
|
|
|
|
const query = {
|
|
[sequelize.Op.or]: orQueryPart
|
|
};
|
|
|
|
const result = await db.RealEstate.count({ where: query });
|
|
return result > 0;
|
|
};
|
|
|
|
module.exports = {
|
|
bulkUpsertRealEstates,
|
|
checkIfAlreadyExist
|
|
};
|