UNPKG

@golemio/pid

Version:
155 lines 8.73 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.InfotextFacade = void 0; const DateTimeUtils_1 = require("../../../../helpers/DateTimeUtils"); const JISInfotextsRedisRepository_1 = require("../../../jis/data-access/cache/JISInfotextsRedisRepository"); const JISInfotextRepository_1 = require("../../data-access/JISInfotextRepository"); const JISInfotextStopSuppressionFilter_1 = require("../../helpers/JISInfotextStopSuppressionFilter"); const OgPidToken_1 = require("../../ioc/OgPidToken"); const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors"); const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe"); const JISInfotextCacheTransferTransformation_1 = require("../transformations/JISInfotextCacheTransferTransformation"); const JISInfotextDepartureTransformation_1 = require("../transformations/JISInfotextDepartureTransformation"); const JISInfotextOverviewTransformation_1 = require("../transformations/JISInfotextOverviewTransformation"); const JISInfotextTransferTransformation_1 = require("../transformations/JISInfotextTransferTransformation"); let InfotextFacade = class InfotextFacade { constructor(infotextRepository, infotextDepartureTransformation, infotextTransferTransformation, infotextOverviewTransformation, infotextStopFilter, infotextsRedisRepository, cacheTransferTransformation) { this.infotextRepository = infotextRepository; this.infotextDepartureTransformation = infotextDepartureTransformation; this.infotextTransferTransformation = infotextTransferTransformation; this.infotextOverviewTransformation = infotextOverviewTransformation; this.infotextStopFilter = infotextStopFilter; this.infotextsRedisRepository = infotextsRedisRepository; this.cacheTransferTransformation = cacheTransferTransformation; } async getInfotextsForDepartureBoards(stopIds, currentMoment, options) { try { const nowDate = (options.timeFrom ?? currentMoment).toDate(); const infotexts = await this.infotextRepository.findAllForDepartureBoard(stopIds, nowDate); if (infotexts.length === 0) { return []; } const filteredInfotexts = this.filterInfotexts(infotexts, nowDate); if (filteredInfotexts.length === 0) { return []; } const infotextsToInclude = []; for (const infotext of filteredInfotexts) { const infotextDto = this.infotextDepartureTransformation.transformElement({ data: infotext, timeZone: options.timezone, }); infotextsToInclude.push(infotextDto); } return infotextsToInclude; } catch (error) { if (error instanceof golemio_errors_1.AbstractGolemioError) { throw error; } throw new golemio_errors_1.GeneralError("Failed to retrieve infotexts for departure boards", this.constructor.name, error, 500); } } async getInfotextsForTransferBoards(stopIds, currentMoment, timeFrom) { try { const nowDate = (timeFrom ?? currentMoment).toDate(); const infotexts = await this.infotextRepository.findAllForDepartureBoard(stopIds, nowDate); if (infotexts.length === 0) { return []; } const filteredInfotexts = this.filterInfotexts(infotexts, nowDate); if (filteredInfotexts.length === 0) { return []; } return this.infotextTransferTransformation.transformArray(filteredInfotexts); } catch (error) { if (error instanceof golemio_errors_1.AbstractGolemioError) throw error; throw new golemio_errors_1.GeneralError("Failed to retrieve infotexts for transfer boards", this.constructor.name, error, 500); } } async getInfotextsForOverview(includeFuture) { try { const infotexts = await this.infotextRepository.findAllForOverview(includeFuture); if (infotexts.length === 0) { return []; } const filteredInfotexts = this.filterInfotexts(infotexts, new Date(), includeFuture); if (filteredInfotexts.length === 0) { return []; } return this.infotextOverviewTransformation.transformArray(filteredInfotexts); } catch (error) { if (error instanceof golemio_errors_1.AbstractGolemioError) { throw error; } throw new golemio_errors_1.GeneralError("Failed to retrieve infotexts for overview", this.constructor.name, error, 500); } } async getInfotextsCache(stopIds, currentMoment, timeFrom) { try { const nowDate = timeFrom ?? currentMoment; const infotexts = await this.infotextsRedisRepository.getActiveStopsInfotexts(stopIds, nowDate); if (infotexts.length === 0) { return []; } const filteredInfotexts = this.filterInfotexts(infotexts, nowDate); if (filteredInfotexts.length === 0) { return []; } return this.cacheTransferTransformation.transformArray(filteredInfotexts); } catch (error) { if (error instanceof golemio_errors_1.AbstractGolemioError) throw error; throw new golemio_errors_1.GeneralError("Failed to retrieve infotexts for V4 transfer boards", this.constructor.name, error, 500); } } filterInfotexts(list, now, includeFuture = false) { const currentTime = DateTimeUtils_1.DateTimeUtils.toLocalTimeFormat(now); const repeatFiltered = list.filter((infotext) => { if (!infotext.repeat_enabled) { return true; } if (includeFuture) { // With includeFuture=true the infotext will be shown at some point in its repeat window return true; } return DateTimeUtils_1.DateTimeUtils.isInRepeatTimeWindow(infotext.repeat_time_start, infotext.repeat_time_end, currentTime); }); return this.infotextStopFilter.filterInfotexts(repeatFiltered, { includeFuture }); } }; exports.InfotextFacade = InfotextFacade; exports.InfotextFacade = InfotextFacade = __decorate([ (0, tsyringe_1.injectable)(), __param(0, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.JISInfotextRepository)), __param(1, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.JISInfotextDepartureTransformation)), __param(2, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.JISInfotextTransferTransformation)), __param(3, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.JISInfotextOverviewTransformation)), __param(4, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.JISInfotextStopSuppressionFilter)), __param(5, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.JISInfotextsRedisRepository)), __param(6, (0, tsyringe_1.inject)(OgPidToken_1.OgPidToken.JISInfotextCacheTransferTransformation)), __metadata("design:paramtypes", [JISInfotextRepository_1.JISInfotextRepository, JISInfotextDepartureTransformation_1.JISInfotextDepartureTransformation, JISInfotextTransferTransformation_1.JISInfotextTransferTransformation, JISInfotextOverviewTransformation_1.JISInfotextOverviewTransformation, JISInfotextStopSuppressionFilter_1.JISInfotextStopSuppressionFilter, JISInfotextsRedisRepository_1.JISInfotextsRedisRepository, JISInfotextCacheTransferTransformation_1.JISInfotextCacheTransferTransformation]) ], InfotextFacade); //# sourceMappingURL=InfotextFacade.js.map