UNPKG

@golemio/parkings

Version:
149 lines • 7.31 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParkingsRepository = void 0; const ModuleContainerToken_1 = require("../ioc/ModuleContainerToken"); const ParkingTariffRelationsRepository_1 = require("./ParkingTariffRelationsRepository"); 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"); const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe"); let ParkingsRepository = exports.ParkingsRepository = class ParkingsRepository extends models_1.PostgresModel { constructor(parkingTariffRelationsRepository) { super(_sch_1.Parkings.name + "Model", { outputSequelizeAttributes: ParkingModel_1.ParkingModel.attributeModel, pgSchema: _sch_1.Parkings.pgSchema, pgTableName: ParkingModel_1.ParkingModel.tableName, savingType: "insertOrUpdate", }, new golemio_validator_1.JSONSchemaValidator("ParkingsRepositoryValidator", ParkingsDtoSchema_1.ParkingsDtoSchema)); this.parkingTariffRelationsRepository = parkingTariffRelationsRepository; this.associate = () => { this.sequelizeModel.hasOne(this.parkingTariffRelationsRepository["sequelizeModel"], { as: "parking_tariffs_relation", foreignKey: "parking_id", sourceKey: "id", }); }; this.GetOne = async (id) => { try { return await this.sequelizeModel.findOne({ where: { id }, include: [ { model: this.parkingTariffRelationsRepository["sequelizeModel"], as: "parking_tariffs_relation", attributes: ["tariff_id"], required: false, subQuery: false, }, ], raw: true, nest: true, }); } catch (err) { throw new golemio_errors_1.GeneralError("Database error", "ParkingsModel", err, 500); } }; this.saveActiveParkingsWithoutAddress = async (parkings, source, dataProvider) => { const transaction = await this.sequelizeModel.sequelize.transaction(); try { const activeIds = parkings.map((parking) => parking.id); await this.markInactiveParkings(activeIds, source, transaction, dataProvider); 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.saveActiveParkingsWithoutAddressAndName = async (parkings, source, dataProvider) => { const transaction = await this.sequelizeModel.sequelize.transaction(); try { const activeIds = parkings.map((parking) => parking.id); await this.markInactiveParkings(activeIds, source, transaction, dataProvider); await this.saveWithoutAddressAndName(parkings, transaction); await transaction.commit(); } catch (err) { await transaction.rollback(); 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, ["source", "source_id"]); }; this.saveWithoutAddressAndName = async (parkings, transaction) => { await this.bulkSave(parkings, this.getParkingAttributesToUpdateWithoutName(), false, false, transaction, [ "source", "source_id", ]); }; this.associate(); } async findWithOutdatedAddress(batchSize, updateInterval) { return this.find({ limit: batchSize, where: { address_updated_at: { [sequelize_1.Op.lte]: sequelize_1.Sequelize.literal(`NOW() - INTERVAL '${updateInterval} days'`), }, }, }); } async findWithoutAddress() { return this.find({ where: { address: null, address_updated_at: null, }, }); } async markInactiveParkings(activeIds, source, transaction, dataProvider) { await this.update({ active: false, }, { where: { source, ...(dataProvider && { data_provider: dataProvider }), 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"); } getParkingAttributesToUpdateWithoutName() { return ParkingModel_1.ParkingModel.attributeList .filter((key) => !["address", "address_updated_at", "name", "sanitized_location", "area"].includes(key)) .concat("updated_at"); } }; exports.ParkingsRepository = ParkingsRepository = __decorate([ (0, tsyringe_1.injectable)(), __param(0, (0, tsyringe_1.inject)(ModuleContainerToken_1.ModuleContainerToken.ParkingTariffRelationsRepository)), __metadata("design:paramtypes", [ParkingTariffRelationsRepository_1.ParkingTariffRelationsRepository]) ], ParkingsRepository); //# sourceMappingURL=ParkingsRepository.js.map