UNPKG

@golemio/parkings

Version:
132 lines 6.79 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParkingsTransformationFabric = void 0; const SourceEnum_1 = require("../../helpers/constants/SourceEnum"); const ICentroidPg_1 = __importDefault(require("../models/interfaces/ICentroidPg")); const helpers_1 = require("@golemio/core/dist/helpers"); const output_gateway_1 = require("@golemio/core/dist/output-gateway"); const turf = __importStar(require("@turf/turf")); class ParkingsTransformationFabric { constructor(paymentSources, logger) { this.paymentSources = paymentSources; this.logger = logger; this.buildFeatureItem = (locationFromDb) => { return (0, output_gateway_1.buildGeojsonFeatureType)("location", { id: locationFromDb.id, source: locationFromDb.source, source_id: locationFromDb.source_id_mapped, data_provider: locationFromDb.data_provider, name: locationFromDb.name, category: locationFromDb.category, date_modified: locationFromDb.date_modified ? new Date(Number(locationFromDb.date_modified)).toISOString() : null, address_formatted: locationFromDb.address ? locationFromDb.address.address_formatted : null, address: locationFromDb.address, location: locationFromDb.location, area_served: locationFromDb.area_served, ...this.transformPayment(locationFromDb.source, locationFromDb), total_spot_number: locationFromDb.total_spot_number, tariff_id: locationFromDb.tariff_id, valid_from: locationFromDb.valid_from, valid_to: locationFromDb.valid_to, parking_type: locationFromDb.parking_type, zone_type: locationFromDb.zone_type, centroid: this.getCentroid(locationFromDb), available_spots_last_updated: this.transformAvailableSpotsLastUpdated(locationFromDb.available_spots_last_updated), available_spots_number: locationFromDb.available_spots_number || null, }); }; this.transformPayment = (source, element) => { const sourcePayment = this.paymentSources[source]?.payment ?? { web_url: null, android_url: null, ios_url: null, }; return { web_app_payment_url: element?.payment_web_url ? element.payment_web_url : this.getDynamicPaymentLink(sourcePayment.web_url, element), android_app_payment_url: element?.payment_android_url ? element.payment_android_url : this.getDynamicPaymentLink(sourcePayment.android_url, element), ios_app_payment_url: element?.payment_ios_url ? element.payment_ios_url : this.getDynamicPaymentLink(sourcePayment.ios_url, element), }; }; this.getDynamicPaymentLink = (link, el) => { const placeholder = link?.match(/[^{\}]+(?=})/); if (placeholder && placeholder[0]) { const value = el.source === SourceEnum_1.SourceEnum.TSK_V2 ? el["source_id_mapped"] : el[placeholder[0]]; if (!value) { this.logger.error(`Invalid payment link shortcode (${placeholder[0]}) for ${el.source}: ${link} in api v2`); return null; } return link?.replace(`{${placeholder}}`, value); } return link; }; this.transformAvailableSpotsLastUpdated = (lastUpdatedTime) => { if (!lastUpdatedTime) return null; return (0, helpers_1.dateTime)(new Date(+lastUpdatedTime)).setTimeZone("Europe/Prague").toISOString({ includeMillis: false }); }; this.buildFeatureCollection = (locations) => { const allLocations = locations.map((location) => { const { measurements, parking_payments, parking_tariffs_relation, ...rest } = location; return this.buildFeatureItem({ ...rest, ...measurements, ...parking_payments, ...parking_tariffs_relation }); }); return (0, output_gateway_1.buildGeojsonFeatureCollection)(allLocations); }; this.getChargeData = (data) => ({ charge: +data.charge, charge_type: data.charge_type || "other", charge_order_index: data.charge_order_index, charge_interval: data.charge_interval, max_iterations_of_charge: data.max_iterations_of_charge, min_iterations_of_charge: data.min_iterations_of_charge, start_time_of_period: data.start_time_of_period, end_time_of_period: data.end_time_of_period, }); } getCentroid(locationFromDb) { let centroidTransformed; if (locationFromDb.centroid) { centroidTransformed = { type: locationFromDb.centroid.type, coordinates: locationFromDb.centroid.coordinates }; } else if (locationFromDb.location.type === "Point") { centroidTransformed = { type: locationFromDb.location.type, coordinates: locationFromDb.location.coordinates }; } else { centroidTransformed = turf.centroid(locationFromDb.location).geometry; } return centroidTransformed; } } exports.ParkingsTransformationFabric = ParkingsTransformationFabric; //# sourceMappingURL=ParkingsTransformationFabric.js.map