Compare commits

...

2 Commits

Author SHA1 Message Date
Fred Boniface a1fdf22805 Run prettier
Signed-off-by: Fred Boniface <fred@fjla.uk>
2024-02-05 20:03:11 +00:00
Fred Boniface 7657085bc3 Fix state where cancelled service is not returned.
Signed-off-by: Fred Boniface <fred@fjla.uk>
2024-02-05 20:03:01 +00:00
4 changed files with 65 additions and 48 deletions

View File

@ -3,7 +3,11 @@ import { NextFunction, Request, Response } from "express";
const utils = require("../utils/auth.utils");
const logger = require("../utils/logger.utils");
module.exports = async function authCheck(req: Request, res: Response, next: NextFunction) {
module.exports = async function authCheck(
req: Request,
res: Response,
next: NextFunction
) {
logger.logger.debug("auth.middleware: Auth check begun");
if (process.env.NODE_ENV === "development") {
req.isAuthed = true;
@ -11,12 +15,12 @@ module.exports = async function authCheck(req: Request, res: Response, next: Nex
next();
} else {
const id: string | string[] | undefined = req.headers.uuid;
if (typeof id === 'undefined') {
if (typeof id === "undefined") {
req.isAuthed = false;
logger.logger.info("auth.middleware: Authentication failed");
next();
} else if (typeof id === 'string') {
const authCheck = await utils.isAuthed(id) || false;
} else if (typeof id === "string") {
const authCheck = (await utils.isAuthed(id)) || false;
if (authCheck) {
req.isAuthed = true;
logger.logger.info("auth.middleware: Authentication Successful");
@ -27,14 +31,18 @@ module.exports = async function authCheck(req: Request, res: Response, next: Nex
next();
}
} else if (Array.isArray(id)) {
const authCheck = await utils.isAuthed(id[0]) || false;
const authCheck = (await utils.isAuthed(id[0])) || false;
if (authCheck) {
req.isAuthed = true;
logger.logger.warn("auth.middleware: UUID Passed as Array - Authentication Successful");
logger.logger.warn(
"auth.middleware: UUID Passed as Array - Authentication Successful"
);
next();
} else {
req.isAuthed = false;
logger.logger.warn("auth.middleware: UUID Passed as Array - Authentication Failed");
logger.logger.warn(
"auth.middleware: UUID Passed as Array - Authentication Failed"
);
next();
}
}

View File

@ -80,10 +80,10 @@ export async function findByTrainUid(
if (supported.includes(services[0]?.operator)) {
pis = await fetchPisCode(services[0]?.stops);
} else {
pis = null
pis = null;
}
// TODO: Format and return data, the function called is not yet complete
return formatTimetableDetail(services[0], pis)
return formatTimetableDetail(services[0], pis);
}
// Internal Functions:
@ -97,15 +97,15 @@ async function fetchPisCode(
if (stops[stop]["isPublic"]) tiplocList.push(stops[stop]["tiploc"]);
}
// Check if no public stops - then it should use an ECS headcode
let pisData: OB_Pis_SimpleObject | null
let pisData: OB_Pis_SimpleObject | null;
if (tiplocList.length) {
pisData = await findByTiplocArray(tiplocList);
} else {
pisData = {
toc: "GW",
skipCount: 0,
code: randomEcsPis()
}
code: randomEcsPis(),
};
}
return pisData;
@ -113,9 +113,9 @@ async function fetchPisCode(
// 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]
const options = ["0015", "9997"];
const randomValue = Math.floor(Math.random() * 2);
return options[randomValue];
}
// Outputs the standard 'shortday' string from a Date.
@ -169,9 +169,8 @@ function filterServices(services: SimpleService[]): SimpleService[] {
const stpIndicator = service["stpIndicator"];
if (stpIndicator === "C") {
continue;
}
if (stpIndicator === "N" && !thisStpIndicators.hasC) {
filteredServices.push(service);
} else if (stpIndicator === "N" && !thisStpIndicators.hasC) {
filteredServices.push(service);
} else if (
stpIndicator === "O" &&

View File

@ -1,7 +1,7 @@
// src/types/express/index.d.ts
// to make the file a module and avoid the TypeScript error
export {}
export {};
declare global {
namespace Express {

View File

@ -1,36 +1,46 @@
import type {Service, OB_TrainTT_service, OB_Pis_SimpleObject, OB_TrainTT_stopDetail, Stop} from "@owlboard/ts-types"
import type {
Service,
OB_TrainTT_service,
OB_Pis_SimpleObject,
OB_TrainTT_stopDetail,
Stop,
} from "@owlboard/ts-types";
import { logger } from "../../logger.utils";
export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject | null): OB_TrainTT_service {
const formattedService: OB_TrainTT_service = {
stpIndicator: service.stpIndicator,
operator: service.operator,
trainUid: service.trainUid,
headcode: service.headcode,
powerType: service.powerType,
planSpeed: convertStringToNumber(service.planSpeed),
scheduleStart: service.scheduleStartDate,
scheduleEnd: service.scheduleEndDate,
daysRun: service.daysRun,
stops: formatStops(service.stops),
vstp: service.vstp,
}
export function formatTimetableDetail(
service: Service,
pis: OB_Pis_SimpleObject | null
): OB_TrainTT_service {
const formattedService: OB_TrainTT_service = {
stpIndicator: service.stpIndicator,
operator: service.operator,
trainUid: service.trainUid,
headcode: service.headcode,
powerType: service.powerType,
planSpeed: convertStringToNumber(service.planSpeed),
scheduleStart: service.scheduleStartDate,
scheduleEnd: service.scheduleEndDate,
daysRun: service.daysRun,
stops: formatStops(service.stops),
vstp: service.vstp,
};
if (pis) {
formattedService.pis = pis;
}
if (pis) {
formattedService.pis = pis;
}
return formattedService
return formattedService;
}
function formatStops(stops: Stop[]): OB_TrainTT_stopDetail[] {
// Cleanly coerce Stop[] to OB_TrainTT_stopDetail[]
const formattedStops: OB_TrainTT_stopDetail[] = []
const formattedStops: OB_TrainTT_stopDetail[] = [];
for (const stop of stops) {
formattedStops.push(formatStopTimes(stop))
formattedStops.push(formatStopTimes(stop));
}
return formattedStops
return formattedStops;
}
function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail {
@ -38,7 +48,7 @@ function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail {
let formattedStop: OB_TrainTT_stopDetail = {
tiploc: stop.tiploc,
isPublic: false,
}
};
if (stop.publicArrival) {
formattedStop.publicArrival = stop.publicArrival;
formattedStop.isPublic = true;
@ -53,14 +63,14 @@ function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail {
if (stop.wttDeparture) {
formattedStop.wttDeparture = stop.wttDeparture;
}
return formattedStop
return formattedStop;
}
function convertStringToNumber(str: string): number {
const number = parseFloat(str)
const number = parseFloat(str);
if (isNaN(number)) {
return 0;
} else {
return number
return number;
}
}