|
| 1 | +import * as OJP from 'ojp-sdk'; |
| 2 | +import * as OJP_Types from 'ojp-shared-types'; |
| 3 | + |
| 4 | +import { PlaceholderContext, PlaceholderHandler } from './placeholder'; |
| 5 | +import { AppHelpers } from '../../helpers/app.helpers'; |
| 6 | +import OJP_TR_Shared_PlaceholderHandle from './ojp-tr-shared'; |
| 7 | + |
| 8 | +export default class OJP_TRR_1_PlaceholderHandle extends OJP_TR_Shared_PlaceholderHandle implements PlaceholderHandler { |
| 9 | + private async buildRequest(sdk: OJP.SDK<'2.0'>): Promise<OJP.TripRefineRequest> { |
| 10 | + const trResponse = await this.fetchTripRequest(sdk) as OJP.TripRequestResponse; |
| 11 | + |
| 12 | + if (!trResponse.ok) { |
| 13 | + throw new Error('<Error>Invalid TR XML response</Error>'); |
| 14 | + } |
| 15 | + |
| 16 | + if (trResponse.value.tripResult.length === 0) { |
| 17 | + throw new Error('<Error>No trips found for TR XML response</Error>'); |
| 18 | + } |
| 19 | + |
| 20 | + const trips = trResponse.value.tripResult.map(el => el.trip); |
| 21 | + const firstTrip = trips[0]; |
| 22 | + |
| 23 | + // HACK - remove expectedDepartureOccupancy nodes due to backend issue |
| 24 | + // - see https://github.com/openTdataCH/ojp-sdk/issues/387 |
| 25 | + firstTrip.leg.forEach(leg => { |
| 26 | + if (leg.timedLeg) { |
| 27 | + const timedLeg = leg.timedLeg as OJP_Types.TimedLegSchema; |
| 28 | + timedLeg.legBoard.expectedDepartureOccupancy = []; |
| 29 | + timedLeg.legAlight.expectedDepartureOccupancy = []; |
| 30 | + timedLeg.legIntermediate.forEach(legIntermediate => { |
| 31 | + legIntermediate.expectedDepartureOccupancy = []; |
| 32 | + }) |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + const trrRequest = OJP.TripRefineRequest.initWithTrip(firstTrip); |
| 37 | + |
| 38 | + return trrRequest; |
| 39 | + } |
| 40 | + |
| 41 | + public async resolve(context: PlaceholderContext): Promise<string | string[]> { |
| 42 | + const sdk = this.buildSDK(context) as OJP.SDK<'2.0'>; |
| 43 | + |
| 44 | + let trrRequest: OJP.TripRefineRequest | null = null; |
| 45 | + try { |
| 46 | + trrRequest = await this.buildRequest(sdk); |
| 47 | + } catch (e) { |
| 48 | + return '<Error>Invalid XML response</Error>'; |
| 49 | + } |
| 50 | + |
| 51 | + const xmlConfig = OJP.DefaultXML_Config; |
| 52 | + const requestXML = trrRequest.buildRequestXML('de', this.requestorRef, xmlConfig); |
| 53 | + const responseXML_F = AppHelpers.prettyPrintXML(requestXML); |
| 54 | + const responseXML_Lines = responseXML_F.split('\n'); |
| 55 | + |
| 56 | + return responseXML_Lines; |
| 57 | + } |
| 58 | +} |
0 commit comments