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 { console.time("StaffLdb Transformation"); 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, }; console.timeEnd("StaffLdb Transformation"); if (output.locationName !== 'Not Found') { return output; } } catch (err) { console.log("utils/translators/ldb/staffLdb.transform: Caught Error"); console.log("Unable to parse data, assuming no data: " + err); } console.timeEnd("StaffLdb Transformation"); return null; } function transformDateTime(input: string): Date { console.log("Transform Date Time Running") return new Date(input); } function transformNrcc(input: any): NrccMessage[] | undefined { console.log("Transform Nrcc Running") if (input === undefined) { return input } let output: NrccMessage[] = []; let messages = input; if (!Array.isArray(input?.message)) { messages = [input?.message]; } if (messages.length) { for (const item of messages) { let message: NrccMessage = { severity: item?.severity, xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage), }; output.push(message); } return output; } return undefined; } function transformTrainServices(input: any): TrainServices[] { console.log("Transform Train Services 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.log("Transform Location 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, }; if (item?.via) { location.via = item.via; } output.push(location); } return output; } export function calculateLength(input: any): number | undefined { console.log("Calculate Length Running") let length: number; if (input?.length) { length = input.length; return Number(length); } if (input?.formation?.coaches?.coach) { length = input.formation.coaches.coach.length; return Number(length); } return undefined; } function transformUnspecifiedDateTime(input: string): Date | undefined { console.log("Transform Unspecified Date Time Running") if (!input) { return undefined; } const date = tz(input, "Europe/London"); return date.toDate(); }