return new and existing real estates when saving results
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
const moment = require("moment");
|
const moment = require("moment");
|
||||||
|
|
||||||
const {
|
const { bulkUpsertRealEstates } = require("../../helpers/db/realEstate");
|
||||||
bulkUpsertRealEstates,
|
|
||||||
checkIfAlreadyExist
|
|
||||||
} = require("../../helpers/db/realEstate");
|
|
||||||
|
|
||||||
class PostgresSaver {
|
class PostgresSaver {
|
||||||
connect() {
|
connect() {
|
||||||
@@ -14,27 +11,33 @@ class PostgresSaver {
|
|||||||
|
|
||||||
async save(results) {
|
async save(results) {
|
||||||
console.log("[POSTGRES] Saving...");
|
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);
|
const savedRecords = await bulkUpsertRealEstates(results);
|
||||||
|
|
||||||
return {
|
if (Array.isArray(savedRecords)) {
|
||||||
exist,
|
const newRealEstates = [];
|
||||||
savedRecords
|
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() {
|
close() {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const db = require("../../models/index");
|
const db = require("../../models/index");
|
||||||
const sequelize = require("sequelize");
|
|
||||||
|
|
||||||
const bulkUpsertRealEstates = async realEstateData => {
|
const bulkUpsertRealEstates = async realEstateData => {
|
||||||
try {
|
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 = {
|
module.exports = {
|
||||||
bulkUpsertRealEstates,
|
bulkUpsertRealEstates
|
||||||
checkIfAlreadyExist
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user