UNPKG

@golemio/parkings

Version:
69 lines 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParkingsSecondaryRepository = void 0; const _sch_1 = require("../../schema-definitions"); const ParkingsDtoSchema_1 = require("../../schema-definitions/datasources/ParkingsDtoSchema"); const ParkingModel_1 = require("../../schema-definitions/models/ParkingModel"); const models_1 = require("@golemio/core/dist/integration-engine/models"); const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors"); const golemio_validator_1 = require("@golemio/core/dist/shared/golemio-validator"); const sequelize_1 = require("@golemio/core/dist/shared/sequelize"); class ParkingsSecondaryRepository extends models_1.PostgresModel { constructor() { super(_sch_1.Parkings.name + "Model", { outputSequelizeAttributes: ParkingModel_1.ParkingModel.attributeModel, pgSchema: _sch_1.Parkings.pgSchema, pgTableName: "parkings_secondary", savingType: "insertOrUpdate", }, new golemio_validator_1.JSONSchemaValidator(_sch_1.Parkings.name + "SecondaryPgModelValidator", ParkingsDtoSchema_1.ParkingsDtoSchema)); this.saveActiveParkingsWithoutAddress = async (parkings, source) => { const transaction = await this.sequelizeModel.sequelize.transaction(); try { const activeIds = parkings.map((parking) => parking.id); await this.markInactiveParkings(activeIds, source, transaction); await this.saveWithoutAddress(parkings, transaction); await transaction.commit(); } catch (err) { await transaction.rollback(); throw new golemio_errors_1.GeneralError("Database error", "ParkingsModel", err, 500); } }; this.GetOne = async (id) => { try { return await this.sequelizeModel.findOne({ where: { id }, raw: true, nest: true, }); } catch (err) { throw new golemio_errors_1.GeneralError("Database error", "ParkingsModel", err, 500); } }; this.saveWithoutAddress = async (parkings, transaction) => { await this.bulkSave(parkings, this.getParkingAttributesToUpdate(), false, false, transaction); }; } async markInactiveParkings(activeIds, source, transaction) { await this.update({ active: false, }, { where: { source, active: true, id: { [sequelize_1.Op.notIn]: activeIds, }, }, transaction, }); } getParkingAttributesToUpdate() { return ParkingModel_1.ParkingModel.attributeList .filter((key) => !["address", "address_updated_at", "sanitized_location", "area"].includes(key)) .concat("updated_at"); } } exports.ParkingsSecondaryRepository = ParkingsSecondaryRepository; //# sourceMappingURL=ParkingsSecondaryRepository.js.map