import type { StaffLdb, NrccMessage, TrainServices, ServiceLocation } from '@owlboard/ts-types'; import { tz } from 'moment-timezone'; import { removeNewlineAndPTag } from '../../newSanitizer'; /// I do not yet have a type defined for any of the input object export function transform(input: any): StaffLdb | null { const data = input.GetBoardResult let output: StaffLdb try { output = { generatedAt: transformDateTime(data?.generatedAt) || new Date(), locationName: data?.locationName || "Not Found", stationManagerCode: data?.stationManagerCode || "UK", nrccMessages: transformNrcc(data?.nrccMessages) || undefined, trainServices: transformTrainServices(data?.trainServices) || undefined, busServices: transformTrainServices(data?.busServices) || undefined, ferryServices: transformTrainServices(data?.ferryServices) || undefined } return output } catch (err) { console.log('Unable to parse data, assuming no data: ' + err) } return null } function transformDateTime(input: string): Date { console.debug(`staffStation.transformDateTime Input: ${input}`) return new Date(input) } function transformNrcc(input: any): NrccMessage[] { console.debug(`staffStations.transformNrcc: Running`) let output: NrccMessage[] = [] let messages = input if (!Array.isArray(input?.message)) { messages = [input?.message] } for (const item of messages) { let message: NrccMessage = { severity: item?.severity, xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage) } output.push(message) } return output } function transformTrainServices(input: any): TrainServices[] { console.debug(`staffStation.transformTrainServices running`) let services: any = input?.service let output: TrainServices[] = [] if (services === undefined) { return output } if (!Array.isArray(input.service)) { services = [input.service] } for (const service of services) { const trainService: TrainServices = { rid: service?.rid, uid: service?.uid, trainid: service?.trainid, operatorCode: service?.operatorCode || 'UK', platform: service?.platform || '-', platformIsHidden: service?.platformIsHidden, serviceIsSupressed: service?.serviceIsSupressed, origin: transformLocation(service?.origin), destination: transformLocation(service?.destination), length: calculateLength(service), isCancelled: service?.isCancelled, cancelReason: service?.cancelReason, delayReason: service?.delayReason, arrivalType: service?.arrivalType, departureType: service?.departureType, sta: transformUnspecifiedDateTime(service?.sta), eta: transformUnspecifiedDateTime(service?.eta), ata: transformUnspecifiedDateTime(service?.ata), std: transformUnspecifiedDateTime(service?.std), etd: transformUnspecifiedDateTime(service?.etd), atd: transformUnspecifiedDateTime(service?.atd), } Object.keys(trainService).forEach(key => trainService[key] === undefined && delete trainService[key]); output.push(trainService) } return output } function transformLocation(input: any): ServiceLocation[] { console.debug(`staffStation.transformLocation running`) let output: ServiceLocation[] = [] let locations: any[] = input.location if (!Array.isArray(input.location)) { locations = [input.location] } for (const item of locations) { const location: ServiceLocation = { tiploc: item?.tiploc, name: item?.locationName } output.push(location) } return output } function calculateLength(input: TrainServices): number | undefined { console.log(input); return undefined } function transformUnspecifiedDateTime(input: string): Date | undefined { console.debug(`staffStation.transformUnspecifiedDateTime running`) if (!input) { return undefined } const date = tz(input, "Europe/London"); return date.toDate() }