UNPKG

@golemio/pid

Version:
80 lines 5.28 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.JISInfotextsDataService = void 0; const JISContainerToken_1 = require("../ioc/JISContainerToken"); const JISInfotextsPresetsRepository_1 = require("../repositories/JISInfotextsPresetsRepository"); const JISInfotextsRepository_1 = require("../repositories/JISInfotextsRepository"); const JISInfotextsRopidGTFSStopsRepository_1 = require("../repositories/JISInfotextsRopidGTFSStopsRepository"); const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken"); const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors"); const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe"); let JISInfotextsDataService = class JISInfotextsDataService { constructor(infotextsRepository, infotextsRopidGTFSStopsRepository, infotextsPresetsRepository, databaseConnector, logger) { this.infotextsRepository = infotextsRepository; this.infotextsRopidGTFSStopsRepository = infotextsRopidGTFSStopsRepository; this.infotextsPresetsRepository = infotextsPresetsRepository; this.databaseConnector = databaseConnector; this.logger = logger; // do nothing } /** * Insert or update all given infotexts, their stop links and preset links. In case of an update, all attributes except for * `created_at` shall be overwritten. Rows absent from this refresh cycle are deleted as stale. */ async refreshData(infotexts, infotextsRopidGTFSStops, infotextsPresets) { const connection = this.databaseConnector.getConnection(); const transaction = await connection.transaction(); const saveTime = new Date(); try { if (infotexts.length === 0) { await this.infotextsPresetsRepository.deleteAll({ transaction }); await this.infotextsRopidGTFSStopsRepository.deleteAll({ transaction }); await this.infotextsRepository.deleteAll({ transaction }); await transaction.commit(); this.logger.info(`${this.constructor.name}.refreshData: all JIS infotexts were deleted because 0 infotexts were imported`); } else { await this.infotextsRepository.refreshData(infotexts, { transaction }); await this.infotextsRopidGTFSStopsRepository.upsertAll(infotextsRopidGTFSStops, { transaction }); await this.infotextsRopidGTFSStopsRepository.deleteAllLastUpdatedBefore(saveTime, { transaction }); await this.infotextsPresetsRepository.upsertAll(infotextsPresets, { transaction }); await this.infotextsPresetsRepository.deleteAllLastUpdatedBefore(saveTime, { transaction }); await transaction.commit(); this.logger.info(`${this.constructor.name}.refreshData: ${infotexts.length} JIS infotexts, ` + `${infotextsRopidGTFSStops.length} JIS infotextsRopidGTFSStops and ` + `${infotextsPresets.length} JIS infotextsPresets were saved`); } } catch (err) { await transaction.rollback(); if (err instanceof golemio_errors_1.GeneralError) throw err; throw new golemio_errors_1.GeneralError("Refreshing JIS Infotexts in database failed", this.constructor.name, err); } } }; exports.JISInfotextsDataService = JISInfotextsDataService; exports.JISInfotextsDataService = JISInfotextsDataService = __decorate([ (0, tsyringe_1.injectable)(), __param(0, (0, tsyringe_1.inject)(JISContainerToken_1.JISContainerToken.JISInfotextsRepository)), __param(1, (0, tsyringe_1.inject)(JISContainerToken_1.JISContainerToken.JISInfotextsRopidGTFSStopsRepository)), __param(2, (0, tsyringe_1.inject)(JISContainerToken_1.JISContainerToken.JISInfotextsPresetsRepository)), __param(3, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.PostgresConnector)), __param(4, (0, tsyringe_1.inject)(CoreToken_1.CoreToken.Logger)), __metadata("design:paramtypes", [JISInfotextsRepository_1.JISInfotextsRepository, JISInfotextsRopidGTFSStopsRepository_1.JISInfotextsRopidGTFSStopsRepository, JISInfotextsPresetsRepository_1.JISInfotextsPresetsRepository, Object, Object]) ], JISInfotextsDataService); //# sourceMappingURL=JISInfotextsDataService.js.map