diff --git a/package-lock.json b/package-lock.json index bdf5461..b1c016d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.5", + "@owlboard/ts-types": "^0.1.8", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", @@ -1983,9 +1983,9 @@ } }, "node_modules/@owlboard/ts-types": { - "version": "0.1.5", - "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.5/ts-types-0.1.5.tgz", - "integrity": "sha512-bbCSDpwAJSUKFHcT3QwXAutlBp02QqAXvobg/wGm8zoaAeWNozWeJSdceMzgse6Ywc1aGamP4w20hlTREl7f8g==", + "version": "0.1.8", + "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.8/ts-types-0.1.8.tgz", + "integrity": "sha512-GNwaaAiP9MJKvDODg6C3U9GSwm1+BodkAndFRWoZXN+rU1KUFoxMEp2k1ThnCrrJHktVYDWk9dkyl1PlgSKtaw==", "dev": true, "license": "GPL-3.0-or-later" }, diff --git a/package.json b/package.json index 03ed62c..fe5b269 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.5", + "@owlboard/ts-types": "^0.1.8", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", diff --git a/src/controllers/train.controllers.js b/src/controllers/train.controllers.js index cfd7a21..dc65e80 100644 --- a/src/controllers/train.controllers.js +++ b/src/controllers/train.controllers.js @@ -17,18 +17,18 @@ async function getByHeadcodeToday(req, res, next) { } async function get(req, res, next) { - if (!req.isAuthed) { +/* if (!req.isAuthed) { const err = new Error("Unauthorized"); err.status = 401; next(err); - } - let date = req.params.date; + } -_-_-_ TEMP DISABLE AUTH _-_-_- +*/ let date = req.params.date; let searchType = req.params.searchType; let id = req.params.id; try { switch (searchType) { case "headcode": - res.json(await train.findByHeadcode(date, id)); + res.json(await train.findByHeadcode(id, date)); break; case "byTrainUid": res.json(await train.findByTrainUid(id, date)); diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index 88b8d40..eb2b3a2 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -105,7 +105,7 @@ async function findPartialEndMatchByTiploc(array: string[]): Promise { function convertDocument(doc: Document, skipType: string): OB_Pis_SimpleObject { return { - code: doc.code, + code: doc.code.toString(), toc: doc.toc, skipCount: doc.skipStops, skipType: skipType, diff --git a/src/services/trainService.services2.ts b/src/services/trainService.services.ts similarity index 89% rename from src/services/trainService.services2.ts rename to src/services/trainService.services.ts index 6152a37..59c7598 100644 --- a/src/services/trainService.services2.ts +++ b/src/services/trainService.services.ts @@ -1,5 +1,5 @@ import { logger } from "../utils/logger.utils"; -import { findByTiplocArray, supported } from "../services/pis.services"; +import { findByTiplocArray, supported } from "./pis.services"; import { queryAggregate } from "./dbAccess.services"; import { getFindByHeadcodePipeline, @@ -79,6 +79,8 @@ export async function findByTrainUid( let pis: OB_Pis_SimpleObject | null; if (supported.includes(services[0]?.operator)) { pis = await fetchPisCode(services[0]?.stops); + } else { + pis = null } // TODO: Format and return data, the function called is not yet complete return formatTimetableDetail(services[0], pis) @@ -94,10 +96,28 @@ async function fetchPisCode( for (const stop in stops) { if (stops[stop]["isPublic"]) tiplocList.push(stops[stop]["tiploc"]); } - const pisData = await findByTiplocArray(tiplocList); + // Check if no public stops - then it should use an ECS headcode + let pisData: OB_Pis_SimpleObject | null + if (tiplocList) { + pisData = await findByTiplocArray(tiplocList); + } else { + pisData = { + toc: "GW", + skipCount: 0, + code: randomEcsPis() + } + } + return pisData; } +// Picks a random choice of the ECS PIS Codes +function randomEcsPis(): string { + const options = ["0015", "9997"] + const randomValue = Math.floor(Math.random() * 2 ) + return options[randomValue] +} + // Outputs the standard 'shortday' string from a Date. function getShortDay(day: Date): string { const dayMap = ["su", "m", "t", "w", "th", "f", "s"]; diff --git a/src/utils/processors/timetable/timetableProcessor.utils.ts b/src/utils/processors/timetable/timetableProcessor.utils.ts index 921178b..ecbabe2 100644 --- a/src/utils/processors/timetable/timetableProcessor.utils.ts +++ b/src/utils/processors/timetable/timetableProcessor.utils.ts @@ -7,12 +7,12 @@ export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject trainUid: service.trainUid, headcode: service.headcode, powerType: service.powerType, - planSpeed: convertStringToNumber(service.planSpeed), // Force convert from string to number. + planSpeed: convertStringToNumber(service.planSpeed), scheduleStart: service.scheduleStartDate, scheduleEnd: service.scheduleEndDate, daysRun: service.daysRun, - stops: formatStops(service.stops), // Need separate function to ensure type compatibility - vstp: service.vstp, // Need to ensure that ts-types is up to date. + stops: formatStops(service.stops), + vstp: service.vstp, } if (pis) { @@ -24,6 +24,36 @@ export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject function formatStops(stops: Stop[]): OB_TrainTT_stopDetail[] { // Cleanly coerce Stop[] to OB_TrainTT_stopDetail[] + const formattedStops: OB_TrainTT_stopDetail[] = [] + + for (const stop of stops) { + formattedStops.push(formatStopTimes(stop)) + } + + return formattedStops +} + +function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail { + // Cleanly converts a single stop to a stopdetail object + let formattedStop: OB_TrainTT_stopDetail = { + tiploc: stop.tiploc, + isPublic: false, + } + if (stop.publicArrival) { + formattedStop.publicArrival = stop.publicArrival; + formattedStop.isPublic = true; + } + if (stop.publicDeparture) { + formattedStop.publicDeparture = stop.publicDeparture; + formattedStop.isPublic = true; + } + if (stop.wttArrival) { + formattedStop.wttArrival = stop.wttArrival; + } + if (stop.wttDeparture) { + formattedStop.wttDeparture = stop.wttDeparture; + } + return formattedStop } function convertStringToNumber(str: string): number {