UNPKG

@golemio/parkings

Version:
156 lines 7.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TskParkingZonesTariffsTransformation = void 0; const SourceEnum_1 = require("../../helpers/constants/SourceEnum"); const helpers_1 = require("@golemio/core/dist/helpers"); const AbstractTransformation_1 = require("@golemio/core/dist/helpers/transformation/AbstractTransformation"); var DaysInWeekEnum; (function (DaysInWeekEnum) { DaysInWeekEnum["monday"] = "Mo"; DaysInWeekEnum["tuesday"] = "Tu"; DaysInWeekEnum["wednesday"] = "We"; DaysInWeekEnum["thursday"] = "Th"; DaysInWeekEnum["friday"] = "Fr"; DaysInWeekEnum["saturday"] = "Sa"; DaysInWeekEnum["sunday"] = "Su"; })(DaysInWeekEnum || (DaysInWeekEnum = {})); class TskParkingZonesTariffsTransformation extends AbstractTransformation_1.AbstractTransformation { constructor(transformationDate) { super(); this.transformationDate = transformationDate; this.name = "TskParkingZonesTariffsTransformation"; this.orderIdx = 0; this.transformInternal = (item) => { this.orderIdx = 0; let result = []; const tariffCommonInfo = { tariff_id: item.id, source: SourceEnum_1.SourceEnum.TSK_V2, last_updated: this.transformationDate.toISOString(), payment_mode: "pre_paid", free_of_charge: false, charge_band_name: item.name || "", accepts_mobile_payment: true, accepts_litacka: true, charge_currency: "czk", }; for (const tg of item.tariffGroups || []) { for (const td of tg.tariffDefinitions || []) { const validityFrom = helpers_1.DateTime.max(helpers_1.DateTime.fromISO(td.activeFrom, { timeZone: "Europe/Prague" }).toISOString(), helpers_1.DateTime.fromISO(tg.activeFrom, { timeZone: "Europe/Prague" }).toISOString(), helpers_1.DateTime.fromISO(item.activeFrom, { timeZone: "Europe/Prague" }).toISOString()); const validityTo = helpers_1.DateTime.min(helpers_1.DateTime.fromISO(td.activeTo, { timeZone: "Europe/Prague" }).toISOString(), helpers_1.DateTime.fromISO(tg.activeTo, { timeZone: "Europe/Prague" }).toISOString(), helpers_1.DateTime.fromISO(item.activeTo, { timeZone: "Europe/Prague" }).toISOString()); tariffCommonInfo.valid_from = validityFrom.setTimeZone("Europe/Prague").toISOString(); tariffCommonInfo.valid_to = validityTo.setTimeZone("Europe/Prague").toISOString(); const periodsOfTime = this.getTariffPeriods(tg, td, item.maxPriceHoliday !== null); if (validityTo > (0, helpers_1.dateTime)(this.transformationDate)) { if (!!td.minPriceItem) { const chargeInfoMinimum = this.getChargeMinimums(td.minPriceItem, periodsOfTime, tariffCommonInfo); result.push(...chargeInfoMinimum); } const charges = this.getChargeOther(td, item.maxHour, periodsOfTime, tariffCommonInfo); result.push(...charges); if (!!td.maxPriceItem) { const chargeInfoMaximum = this.getChargeMaximums(td.maxPriceItem, periodsOfTime, tariffCommonInfo); result.push(...chargeInfoMaximum); } } } } result = this.addGlobalMinMax(item, result, tariffCommonInfo); return result; }; this.getChargeOther = (definition, maxHour, periods, tariffCommonInfo) => periods.map((period) => ({ ...tariffCommonInfo, charge_order_index: this.orderIdx++, charge: definition.pricePerHour / 60, charge_type: "other", charge_interval: 60, max_iterations_of_charge: definition.minuteTo - definition.minuteFrom, min_iterations_of_charge: 1, ...(maxHour && { maximum_duration_seconds: maxHour * 3600 }), periods_of_time: period, })); this.getChargeMinimums = (price, periods, tariffCommonInfo) => periods.map((period) => ({ ...tariffCommonInfo, charge_order_index: this.orderIdx++, charge: price, charge_type: "minimum", periods_of_time: period, })); this.getChargeMaximums = (price, periods, tariffCommonInfo) => periods.map((period) => ({ ...tariffCommonInfo, charge_order_index: this.orderIdx++, charge: price, charge_type: "maximum", periods_of_time: period, })); this.getTariffPeriods = (group, definition, hasHolidayTariff) => { const result = []; for (const day of Object.keys(DaysInWeekEnum)) { if (group[day]) { result.push({ day_in_week: DaysInWeekEnum[day], start: definition.timeFrom, end: helpers_1.DateTime.fromFormat(definition.timeTo, "HH:mm:ss", { timeZone: "Europe/Prague" }) .subtract(1, "minutes") .format("HH:mm:ss"), ph: "PH_off", }); } } if (hasHolidayTariff) { const holidayPeriods = result.map((el) => ({ ...el, ph: "PH_only" })); result.push(...holidayPeriods); } return [result]; }; } getAllPeriods(ph) { return Object.keys(DaysInWeekEnum).map((day) => ({ day_in_week: DaysInWeekEnum[day], start: "00:00:00", end: "23:59:00", ph: ph, })); } addGlobalMinMax(item, result, tariffCommonInfo) { if (!!item.maxPrice) { result.push({ ...tariffCommonInfo, charge_order_index: this.orderIdx++, charge: item.maxPrice, charge_type: "maximum", periods_of_time: this.getAllPeriods("PH_off"), }); } if (!!item.minPrice) { result.push({ ...tariffCommonInfo, charge_order_index: this.orderIdx++, charge: item.minPrice, charge_type: "minimum", periods_of_time: this.getAllPeriods("PH_off"), }); } if (!!item.maxPriceHoliday) { result.push({ ...tariffCommonInfo, charge_order_index: this.orderIdx++, charge: item.maxPriceHoliday, charge_type: "maximum", periods_of_time: this.getAllPeriods("PH_only"), }); if (!!item.minPrice) { result.push({ ...tariffCommonInfo, charge_order_index: this.orderIdx++, charge: item.minPrice, charge_type: "minimum", periods_of_time: this.getAllPeriods("PH_only"), }); } } return result; } } exports.TskParkingZonesTariffsTransformation = TskParkingZonesTariffsTransformation; //# sourceMappingURL=TskParkingZonesTariffsTransformation.js.map