47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
const moment = require("moment");
|
|
|
|
const {
|
|
bulkUpsertRealEstates,
|
|
checkIfAlreadyExist
|
|
} = require("../../helpers/db/realEstate");
|
|
|
|
class PostgresSaver {
|
|
connect() {
|
|
//TODO: It seems we never worry about open/close connection with Sequelize ?
|
|
//TODO: Check if postgres is ready
|
|
return true;
|
|
}
|
|
|
|
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
|
|
};
|
|
}
|
|
|
|
close() {
|
|
//TODO: It seems we never worry about open/close connection with Sequelize ?
|
|
return true;
|
|
}
|
|
}
|
|
|
|
module.exports = PostgresSaver;
|