return new and existing real estates when saving results
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
const moment = require("moment");
|
||||
|
||||
const {
|
||||
bulkUpsertRealEstates,
|
||||
checkIfAlreadyExist
|
||||
} = require("../../helpers/db/realEstate");
|
||||
const { bulkUpsertRealEstates } = require("../../helpers/db/realEstate");
|
||||
|
||||
class PostgresSaver {
|
||||
connect() {
|
||||
@@ -14,27 +11,33 @@ class PostgresSaver {
|
||||
|
||||
async save(results) {
|
||||
console.log("[POSTGRES] Saving...");
|
||||
const resultsWithPublishedAndRenewedDateSame = results.filter(
|
||||
realEstate => {
|
||||
const { publishedDate, renewedDate } = realEstate;
|
||||
|
||||
const publishedMomentDate = moment.utc(publishedDate);
|
||||
const renewedMomentDate = moment.utc(renewedDate);
|
||||
|
||||
return publishedMomentDate.isSame(renewedMomentDate, "minute");
|
||||
}
|
||||
);
|
||||
|
||||
const exist =
|
||||
resultsWithPublishedAndRenewedDateSame.length > 0
|
||||
? await checkIfAlreadyExist(resultsWithPublishedAndRenewedDateSame)
|
||||
: false;
|
||||
const savedRecords = await bulkUpsertRealEstates(results);
|
||||
|
||||
return {
|
||||
exist,
|
||||
savedRecords
|
||||
};
|
||||
if (Array.isArray(savedRecords)) {
|
||||
const newRealEstates = [];
|
||||
const existingRealEstates = [];
|
||||
|
||||
for (const savedRecord of savedRecords) {
|
||||
const { createdAt, updatedAt } = savedRecord;
|
||||
|
||||
const createdAtMoment = moment.utc(createdAt);
|
||||
const updatedAtMoment = moment.utc(updatedAt);
|
||||
|
||||
if (createdAtMoment.isSame(updatedAtMoment, "second")) {
|
||||
newRealEstates.push(savedRecord);
|
||||
} else {
|
||||
existingRealEstates.push(savedRecord);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
newRecords: newRealEstates,
|
||||
existingRecords: existingRealEstates
|
||||
};
|
||||
} else {
|
||||
throw { message: "[POSTGRES] Failed to save records" };
|
||||
}
|
||||
}
|
||||
|
||||
close() {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"use strict";
|
||||
const db = require("../../models/index");
|
||||
const sequelize = require("sequelize");
|
||||
|
||||
const bulkUpsertRealEstates = async realEstateData => {
|
||||
try {
|
||||
@@ -37,29 +36,6 @@ const bulkUpsertRealEstates = async realEstateData => {
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
bulkUpsertRealEstates
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user