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"
},
"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"
},

View File

@ -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",

View File

@ -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));

View File

@ -105,7 +105,7 @@ async function findPartialEndMatchByTiploc(array: string[]): Promise<Document> {
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,

View File

@ -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"];

View File

@ -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 {