Compare commits
No commits in common. "a1fdf2280563b6975208f307ffcd5f8f91b346e2" and "88cf7d3d159d9336af27391fc86c8d326f8d9b71" have entirely different histories.
a1fdf22805
...
88cf7d3d15
|
@ -3,11 +3,7 @@ import { NextFunction, Request, Response } from "express";
|
||||||
const utils = require("../utils/auth.utils");
|
const utils = require("../utils/auth.utils");
|
||||||
const logger = require("../utils/logger.utils");
|
const logger = require("../utils/logger.utils");
|
||||||
|
|
||||||
module.exports = async function authCheck(
|
module.exports = async function authCheck(req: Request, res: Response, next: NextFunction) {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) {
|
|
||||||
logger.logger.debug("auth.middleware: Auth check begun");
|
logger.logger.debug("auth.middleware: Auth check begun");
|
||||||
if (process.env.NODE_ENV === "development") {
|
if (process.env.NODE_ENV === "development") {
|
||||||
req.isAuthed = true;
|
req.isAuthed = true;
|
||||||
|
@ -15,12 +11,12 @@ module.exports = async function authCheck(
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
const id: string | string[] | undefined = req.headers.uuid;
|
const id: string | string[] | undefined = req.headers.uuid;
|
||||||
if (typeof id === "undefined") {
|
if (typeof id === 'undefined') {
|
||||||
req.isAuthed = false;
|
req.isAuthed = false;
|
||||||
logger.logger.info("auth.middleware: Authentication failed");
|
logger.logger.info("auth.middleware: Authentication failed");
|
||||||
next();
|
next();
|
||||||
} else if (typeof id === "string") {
|
} else if (typeof id === 'string') {
|
||||||
const authCheck = (await utils.isAuthed(id)) || false;
|
const authCheck = await utils.isAuthed(id) || false;
|
||||||
if (authCheck) {
|
if (authCheck) {
|
||||||
req.isAuthed = true;
|
req.isAuthed = true;
|
||||||
logger.logger.info("auth.middleware: Authentication Successful");
|
logger.logger.info("auth.middleware: Authentication Successful");
|
||||||
|
@ -31,18 +27,14 @@ module.exports = async function authCheck(
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
} else if (Array.isArray(id)) {
|
} else if (Array.isArray(id)) {
|
||||||
const authCheck = (await utils.isAuthed(id[0])) || false;
|
const authCheck = await utils.isAuthed(id[0]) || false;
|
||||||
if (authCheck) {
|
if (authCheck) {
|
||||||
req.isAuthed = true;
|
req.isAuthed = true;
|
||||||
logger.logger.warn(
|
logger.logger.warn("auth.middleware: UUID Passed as Array - Authentication Successful");
|
||||||
"auth.middleware: UUID Passed as Array - Authentication Successful"
|
|
||||||
);
|
|
||||||
next();
|
next();
|
||||||
} else {
|
} else {
|
||||||
req.isAuthed = false;
|
req.isAuthed = false;
|
||||||
logger.logger.warn(
|
logger.logger.warn("auth.middleware: UUID Passed as Array - Authentication Failed");
|
||||||
"auth.middleware: UUID Passed as Array - Authentication Failed"
|
|
||||||
);
|
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,10 +80,10 @@ export async function findByTrainUid(
|
||||||
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 {
|
} else {
|
||||||
pis = null;
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Internal Functions:
|
// Internal Functions:
|
||||||
|
@ -97,25 +97,25 @@ async function fetchPisCode(
|
||||||
if (stops[stop]["isPublic"]) tiplocList.push(stops[stop]["tiploc"]);
|
if (stops[stop]["isPublic"]) tiplocList.push(stops[stop]["tiploc"]);
|
||||||
}
|
}
|
||||||
// Check if no public stops - then it should use an ECS headcode
|
// 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) {
|
if (tiplocList.length) {
|
||||||
pisData = await findByTiplocArray(tiplocList);
|
pisData = await findByTiplocArray(tiplocList);
|
||||||
} else {
|
} else {
|
||||||
pisData = {
|
pisData = {
|
||||||
toc: "GW",
|
toc: "GW",
|
||||||
skipCount: 0,
|
skipCount: 0,
|
||||||
code: randomEcsPis(),
|
code: randomEcsPis()
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return pisData;
|
return pisData;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Picks a random choice of the ECS PIS Codes
|
// Picks a random choice of the ECS PIS Codes
|
||||||
function randomEcsPis(): string {
|
function randomEcsPis(): string {
|
||||||
const options = ["0015", "9997"];
|
const options = ["0015", "9997"]
|
||||||
const randomValue = Math.floor(Math.random() * 2);
|
const randomValue = Math.floor(Math.random() * 2 )
|
||||||
return options[randomValue];
|
return options[randomValue]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Outputs the standard 'shortday' string from a Date.
|
// Outputs the standard 'shortday' string from a Date.
|
||||||
|
@ -169,8 +169,9 @@ function filterServices(services: SimpleService[]): SimpleService[] {
|
||||||
const stpIndicator = service["stpIndicator"];
|
const stpIndicator = service["stpIndicator"];
|
||||||
|
|
||||||
if (stpIndicator === "C") {
|
if (stpIndicator === "C") {
|
||||||
filteredServices.push(service);
|
continue;
|
||||||
} else if (stpIndicator === "N" && !thisStpIndicators.hasC) {
|
}
|
||||||
|
if (stpIndicator === "N" && !thisStpIndicators.hasC) {
|
||||||
filteredServices.push(service);
|
filteredServices.push(service);
|
||||||
} else if (
|
} else if (
|
||||||
stpIndicator === "O" &&
|
stpIndicator === "O" &&
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// src/types/express/index.d.ts
|
// src/types/express/index.d.ts
|
||||||
|
|
||||||
// to make the file a module and avoid the TypeScript error
|
// to make the file a module and avoid the TypeScript error
|
||||||
export {};
|
export {}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
namespace Express {
|
namespace Express {
|
||||||
|
@ -9,4 +9,4 @@ declare global {
|
||||||
isAuthed: boolean;
|
isAuthed: boolean;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,46 +1,36 @@
|
||||||
import type {
|
import type {Service, OB_TrainTT_service, OB_Pis_SimpleObject, OB_TrainTT_stopDetail, Stop} from "@owlboard/ts-types"
|
||||||
Service,
|
|
||||||
OB_TrainTT_service,
|
|
||||||
OB_Pis_SimpleObject,
|
|
||||||
OB_TrainTT_stopDetail,
|
|
||||||
Stop,
|
|
||||||
} from "@owlboard/ts-types";
|
|
||||||
import { logger } from "../../logger.utils";
|
|
||||||
|
|
||||||
export function formatTimetableDetail(
|
export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject | null): OB_TrainTT_service {
|
||||||
service: Service,
|
const formattedService: OB_TrainTT_service = {
|
||||||
pis: OB_Pis_SimpleObject | null
|
stpIndicator: service.stpIndicator,
|
||||||
): OB_TrainTT_service {
|
operator: service.operator,
|
||||||
const formattedService: OB_TrainTT_service = {
|
trainUid: service.trainUid,
|
||||||
stpIndicator: service.stpIndicator,
|
headcode: service.headcode,
|
||||||
operator: service.operator,
|
powerType: service.powerType,
|
||||||
trainUid: service.trainUid,
|
planSpeed: convertStringToNumber(service.planSpeed),
|
||||||
headcode: service.headcode,
|
scheduleStart: service.scheduleStartDate,
|
||||||
powerType: service.powerType,
|
scheduleEnd: service.scheduleEndDate,
|
||||||
planSpeed: convertStringToNumber(service.planSpeed),
|
daysRun: service.daysRun,
|
||||||
scheduleStart: service.scheduleStartDate,
|
stops: formatStops(service.stops),
|
||||||
scheduleEnd: service.scheduleEndDate,
|
vstp: service.vstp,
|
||||||
daysRun: service.daysRun,
|
}
|
||||||
stops: formatStops(service.stops),
|
|
||||||
vstp: service.vstp,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (pis) {
|
if (pis) {
|
||||||
formattedService.pis = pis;
|
formattedService.pis = pis;
|
||||||
}
|
}
|
||||||
|
|
||||||
return formattedService;
|
return formattedService
|
||||||
}
|
}
|
||||||
|
|
||||||
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[] = [];
|
const formattedStops: OB_TrainTT_stopDetail[] = []
|
||||||
|
|
||||||
for (const stop of stops) {
|
for (const stop of stops) {
|
||||||
formattedStops.push(formatStopTimes(stop));
|
formattedStops.push(formatStopTimes(stop))
|
||||||
}
|
}
|
||||||
|
|
||||||
return formattedStops;
|
return formattedStops
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail {
|
function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail {
|
||||||
|
@ -48,7 +38,7 @@ function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail {
|
||||||
let formattedStop: OB_TrainTT_stopDetail = {
|
let formattedStop: OB_TrainTT_stopDetail = {
|
||||||
tiploc: stop.tiploc,
|
tiploc: stop.tiploc,
|
||||||
isPublic: false,
|
isPublic: false,
|
||||||
};
|
}
|
||||||
if (stop.publicArrival) {
|
if (stop.publicArrival) {
|
||||||
formattedStop.publicArrival = stop.publicArrival;
|
formattedStop.publicArrival = stop.publicArrival;
|
||||||
formattedStop.isPublic = true;
|
formattedStop.isPublic = true;
|
||||||
|
@ -63,14 +53,14 @@ function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail {
|
||||||
if (stop.wttDeparture) {
|
if (stop.wttDeparture) {
|
||||||
formattedStop.wttDeparture = stop.wttDeparture;
|
formattedStop.wttDeparture = stop.wttDeparture;
|
||||||
}
|
}
|
||||||
return formattedStop;
|
return formattedStop
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertStringToNumber(str: string): number {
|
function convertStringToNumber(str: string): number {
|
||||||
const number = parseFloat(str);
|
const number = parseFloat(str)
|
||||||
if (isNaN(number)) {
|
if (isNaN(number)) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return number;
|
return number
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue