Complete server side PIS searching. Temporary disablement of authentication.

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2024-02-03 21:29:52 +00:00
parent 7eb1eaba50
commit 3b03330ce1
6 changed files with 65 additions and 15 deletions

8
package-lock.json generated
View File

@ -24,7 +24,7 @@
"zlib": "^1.0.5" "zlib": "^1.0.5"
}, },
"devDependencies": { "devDependencies": {
"@owlboard/ts-types": "^0.1.5", "@owlboard/ts-types": "^0.1.8",
"@types/jest": "^29.5.3", "@types/jest": "^29.5.3",
"eslint": "^8.39.0", "eslint": "^8.39.0",
"jest": "^29.6.2", "jest": "^29.6.2",
@ -1983,9 +1983,9 @@
} }
}, },
"node_modules/@owlboard/ts-types": { "node_modules/@owlboard/ts-types": {
"version": "0.1.5", "version": "0.1.8",
"resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.5/ts-types-0.1.5.tgz", "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.8/ts-types-0.1.8.tgz",
"integrity": "sha512-bbCSDpwAJSUKFHcT3QwXAutlBp02QqAXvobg/wGm8zoaAeWNozWeJSdceMzgse6Ywc1aGamP4w20hlTREl7f8g==", "integrity": "sha512-GNwaaAiP9MJKvDODg6C3U9GSwm1+BodkAndFRWoZXN+rU1KUFoxMEp2k1ThnCrrJHktVYDWk9dkyl1PlgSKtaw==",
"dev": true, "dev": true,
"license": "GPL-3.0-or-later" "license": "GPL-3.0-or-later"
}, },

View File

@ -33,7 +33,7 @@
"zlib": "^1.0.5" "zlib": "^1.0.5"
}, },
"devDependencies": { "devDependencies": {
"@owlboard/ts-types": "^0.1.5", "@owlboard/ts-types": "^0.1.8",
"@types/jest": "^29.5.3", "@types/jest": "^29.5.3",
"eslint": "^8.39.0", "eslint": "^8.39.0",
"jest": "^29.6.2", "jest": "^29.6.2",

View File

@ -17,18 +17,18 @@ async function getByHeadcodeToday(req, res, next) {
} }
async function get(req, res, next) { async function get(req, res, next) {
if (!req.isAuthed) { /* if (!req.isAuthed) {
const err = new Error("Unauthorized"); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
next(err); next(err);
} } -_-_-_ TEMP DISABLE AUTH _-_-_-
let date = req.params.date; */ let date = req.params.date;
let searchType = req.params.searchType; let searchType = req.params.searchType;
let id = req.params.id; let id = req.params.id;
try { try {
switch (searchType) { switch (searchType) {
case "headcode": case "headcode":
res.json(await train.findByHeadcode(date, id)); res.json(await train.findByHeadcode(id, date));
break; break;
case "byTrainUid": case "byTrainUid":
res.json(await train.findByTrainUid(id, date)); res.json(await train.findByTrainUid(id, date));

View File

@ -105,7 +105,7 @@ async function findPartialEndMatchByTiploc(array: string[]): Promise<Document> {
function convertDocument(doc: Document, skipType: string): OB_Pis_SimpleObject { function convertDocument(doc: Document, skipType: string): OB_Pis_SimpleObject {
return { return {
code: doc.code, code: doc.code.toString(),
toc: doc.toc, toc: doc.toc,
skipCount: doc.skipStops, skipCount: doc.skipStops,
skipType: skipType, skipType: skipType,

View File

@ -1,5 +1,5 @@
import { logger } from "../utils/logger.utils"; 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 { queryAggregate } from "./dbAccess.services";
import { import {
getFindByHeadcodePipeline, getFindByHeadcodePipeline,
@ -79,6 +79,8 @@ export async function findByTrainUid(
let pis: OB_Pis_SimpleObject | null; let pis: OB_Pis_SimpleObject | null;
if (supported.includes(services[0]?.operator)) { if (supported.includes(services[0]?.operator)) {
pis = await fetchPisCode(services[0]?.stops); pis = await fetchPisCode(services[0]?.stops);
} else {
pis = null
} }
// TODO: Format and return data, the function called is not yet complete // TODO: Format and return data, the function called is not yet complete
return formatTimetableDetail(services[0], pis) return formatTimetableDetail(services[0], pis)
@ -94,10 +96,28 @@ async function fetchPisCode(
for (const stop in stops) { for (const stop in stops) {
if (stops[stop]["isPublic"]) tiplocList.push(stops[stop]["tiploc"]); 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; 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. // Outputs the standard 'shortday' string from a Date.
function getShortDay(day: Date): string { function getShortDay(day: Date): string {
const dayMap = ["su", "m", "t", "w", "th", "f", "s"]; const dayMap = ["su", "m", "t", "w", "th", "f", "s"];

View File

@ -7,12 +7,12 @@ export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject
trainUid: service.trainUid, trainUid: service.trainUid,
headcode: service.headcode, headcode: service.headcode,
powerType: service.powerType, powerType: service.powerType,
planSpeed: convertStringToNumber(service.planSpeed), // Force convert from string to number. planSpeed: convertStringToNumber(service.planSpeed),
scheduleStart: service.scheduleStartDate, scheduleStart: service.scheduleStartDate,
scheduleEnd: service.scheduleEndDate, scheduleEnd: service.scheduleEndDate,
daysRun: service.daysRun, daysRun: service.daysRun,
stops: formatStops(service.stops), // Need separate function to ensure type compatibility stops: formatStops(service.stops),
vstp: service.vstp, // Need to ensure that ts-types is up to date. vstp: service.vstp,
} }
if (pis) { if (pis) {
@ -24,6 +24,36 @@ export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject
function formatStops(stops: Stop[]): OB_TrainTT_stopDetail[] { function formatStops(stops: Stop[]): OB_TrainTT_stopDetail[] {
// Cleanly coerce Stop[] to 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 { function convertStringToNumber(str: string): number {