UNPKG

@golemio/parkings

Version:
78 lines 4.9 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.SaveIptOictPaymentsDataTask = void 0; const CachedParkingSourcesRepository_1 = require("../../../helpers/data-access/CachedParkingSourcesRepository"); const IptOictPaymentsDataSource_1 = require("../../datasources/iptoict/IptOictPaymentsDataSource"); const ModuleContainerToken_1 = require("../../ioc/ModuleContainerToken"); const ParkingPaymentsRepository_1 = require("../../repositories/ParkingPaymentsRepository"); const IptOictPaymentsTransformation_1 = require("../../transformations/IptOictPaymentsTransformation"); const IptOictTaskTypeSchema_1 = require("../schemas/IptOictTaskTypeSchema"); const integration_engine_1 = require("@golemio/core/dist/integration-engine"); const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors"); const tsyringe_1 = require("@golemio/core/dist/shared/tsyringe"); const constants_1 = require("../constants"); let SaveIptOictPaymentsDataTask = exports.SaveIptOictPaymentsDataTask = class SaveIptOictPaymentsDataTask extends integration_engine_1.AbstractTask { constructor(iptOictPaymentsDataSource, parkingPaymentsRepository, cachedParkingSourcesRepository) { super(constants_1.NEW_PARKINGS_WORKER_NAME); this.iptOictPaymentsDataSource = iptOictPaymentsDataSource; this.parkingPaymentsRepository = parkingPaymentsRepository; this.cachedParkingSourcesRepository = cachedParkingSourcesRepository; this.queueName = "saveIptOictPaymentsData"; this.queueTtl = 60 * 60 * 1000; // 1 hour this.schema = IptOictTaskTypeSchema_1.IptOictTaskTypeValidationSchema; } async execute(data) { try { const jobConfig = await this.getSourceConfig(data.source); await this.updatePaymentsData(jobConfig.source, jobConfig.datasource_payments); } catch (err) { if (err instanceof golemio_errors_1.AbstractGolemioError) { throw err; } throw new golemio_errors_1.GeneralError("Error while saving IPT payments data", this.constructor.name, err); } } async updatePaymentsData(source, sourceUrl) { const processingDate = new Date(); const data = await this.iptOictPaymentsDataSource.getPaymentsData(sourceUrl); const transformation = new IptOictPaymentsTransformation_1.IptOictPaymentsTransformation(source); const transformedData = transformation.transformArray(data); await this.parkingPaymentsRepository.updatePaymentsBySource(transformedData, source, processingDate); } async getSourceConfig(source) { const sources = await this.cachedParkingSourcesRepository.getParkingPaymentsDataSourceConfig({ isRestrictedToOpenData: false, }); if (!sources.length) { throw new golemio_errors_1.GeneralError(`No sources found for IPT payment task`, this.constructor.name); } const config = sources.find((el) => el.source === source); if (!config) { throw new golemio_errors_1.ValidationError(`Invalid source value: ${source}, must use one of: [${sources.join(", ")}]`); } return config; } }; exports.SaveIptOictPaymentsDataTask = SaveIptOictPaymentsDataTask = __decorate([ (0, tsyringe_1.injectable)(), __param(0, (0, tsyringe_1.inject)(ModuleContainerToken_1.ModuleContainerToken.IptOictPaymentsDataSource)), __param(1, (0, tsyringe_1.inject)(ModuleContainerToken_1.ModuleContainerToken.ParkingPaymentsRepository)), __param(2, (0, tsyringe_1.inject)(ModuleContainerToken_1.ModuleContainerToken.CachedParkingSourcesRepository)), __metadata("design:paramtypes", [IptOictPaymentsDataSource_1.IptOictPaymentsDataSource, ParkingPaymentsRepository_1.ParkingPaymentsRepository, CachedParkingSourcesRepository_1.CachedParkingSourcesRepository]) ], SaveIptOictPaymentsDataTask); //# sourceMappingURL=SaveIptOictPaymentsDataTask.js.map