newStaffLDB-API #48
4
app.js
4
app.js
@ -52,12 +52,12 @@ const limiter = rateLimit({
|
||||
logger.logger.info(`Starting version ${version.app} in ${mode} mode`);
|
||||
|
||||
// Remove X-Powered-By header:
|
||||
app.disable('x-powered-by');
|
||||
app.disable("x-powered-by");
|
||||
|
||||
// Express Error Handling:
|
||||
app.use((err, req, res, next) => {
|
||||
const statusCode = err.statuscode || 500;
|
||||
logger.logger.error(err, "Express Error")
|
||||
logger.logger.error(err, "Express Error");
|
||||
res.status(statusCode).json({ message: err.message });
|
||||
return;
|
||||
});
|
||||
|
@ -54,7 +54,8 @@
|
||||
<h1>Register for OwlBoard</h1>
|
||||
<br />
|
||||
<p>
|
||||
Tap the button to register this device, or scan the barcode with another device.
|
||||
Tap the button to register this device, or scan the barcode with
|
||||
another device.
|
||||
</p>
|
||||
<br />
|
||||
<a
|
||||
|
@ -14,12 +14,18 @@ export const statusCodes = {
|
||||
951: "unknown server error",
|
||||
};
|
||||
|
||||
export const msgCodes = new Map <string, string>([
|
||||
["LOC_NOT_FOUND", "Location not found. If you are sure that the location exists, there may be a fault with the data provider."],
|
||||
["USR_NOT_FOUND", "User is not registered, consider regeristering for access to this resource"],
|
||||
export const msgCodes = new Map<string, string>([
|
||||
[
|
||||
"LOC_NOT_FOUND",
|
||||
"Location not found. If you are sure that the location exists, there may be a fault with the data provider.",
|
||||
],
|
||||
[
|
||||
"USR_NOT_FOUND",
|
||||
"User is not registered, consider regeristering for access to this resource",
|
||||
],
|
||||
["AUTH_ERR", "Authentication Error"],
|
||||
["OK", "OK"]
|
||||
])
|
||||
["OK", "OK"],
|
||||
]);
|
||||
|
||||
module.exports = statusCodes;
|
||||
//export { statusCodes };
|
||||
|
@ -3,11 +3,11 @@ const logger = require("../utils/logger.utils");
|
||||
|
||||
module.exports = async function authCheck(req, res, next) {
|
||||
//log.out("authMiddlewares: Checking authentication", "dbug");
|
||||
logger.logger.debug("Auth check starting")
|
||||
logger.logger.debug("Auth check starting");
|
||||
try {
|
||||
var uuid = req.headers.uuid;
|
||||
} catch (err) {
|
||||
logger.logger.warn("Unable to read UUID header - Not authenticated")
|
||||
logger.logger.warn("Unable to read UUID header - Not authenticated");
|
||||
req.isAuthed = false;
|
||||
return next();
|
||||
}
|
||||
@ -16,11 +16,11 @@ module.exports = async function authCheck(req, res, next) {
|
||||
if (!result) {
|
||||
req.isAuthed = false;
|
||||
//log.out("authMiddlewares: User !isAuthed", "dbug");
|
||||
logger.logger.debug("Auth denied")
|
||||
logger.logger.debug("Auth denied");
|
||||
} else {
|
||||
req.isAuthed = true;
|
||||
//log.out("authMiddlewares: User isAuthed", "dbug");
|
||||
logger.logger.debug("Auth successful")
|
||||
logger.logger.debug("Auth successful");
|
||||
}
|
||||
return next();
|
||||
} catch (err) {
|
||||
@ -28,7 +28,7 @@ module.exports = async function authCheck(req, res, next) {
|
||||
"authMiddlewares: Unable to check auth, default to !isAuthed",
|
||||
"warn"
|
||||
);*/
|
||||
logger.logger.error(err, `Auth check failed`)
|
||||
logger.logger.error(err, `Auth check failed`);
|
||||
req.isAuthed = false;
|
||||
return next(err);
|
||||
}
|
||||
|
@ -16,13 +16,15 @@ const db = client.db(dbName);
|
||||
|
||||
async function query(collection, query, returnId = false) {
|
||||
await client.connect();
|
||||
logger.logger.trace(`dbAccess.query: Connecting to collection: '${collection}'`)
|
||||
logger.logger.trace(
|
||||
`dbAccess.query: Connecting to collection: '${collection}'`
|
||||
);
|
||||
var qcoll = db.collection(collection);
|
||||
var qcursor = qcoll.find(query);
|
||||
if (!returnId) {
|
||||
qcursor.project({ _id: 0 });
|
||||
}
|
||||
logger.logger.trace(query, "dbAccess.query: Runnung Query")
|
||||
logger.logger.trace(query, "dbAccess.query: Runnung Query");
|
||||
increment(collection);
|
||||
let result = await qcursor.toArray();
|
||||
logger.logger.trace(result, "dbAccess.query: Response");
|
||||
@ -56,7 +58,9 @@ async function queryAggregate(collection, pipeline) {
|
||||
}
|
||||
|
||||
async function increment(target) {
|
||||
logger.logger.debug(`dbAccess.increment: Incrementing counter for: ${target}`)
|
||||
logger.logger.debug(
|
||||
`dbAccess.increment: Incrementing counter for: ${target}`
|
||||
);
|
||||
await client.connect();
|
||||
let col = db.collection("meta");
|
||||
let update = {};
|
||||
|
@ -18,7 +18,7 @@ async function get(id, staff = false) {
|
||||
const obj = await util.checkCrs(cleanId);
|
||||
try {
|
||||
const crs = obj[0]["3ALPHA"];
|
||||
logger.debug(`ldbService.get: Determined CRS for lookup to be: ${crs}`)
|
||||
logger.debug(`ldbService.get: Determined CRS for lookup to be: ${crs}`);
|
||||
if (staff) {
|
||||
const data = arrDepBoardStaff(crs);
|
||||
db.increment("ldbsvws");
|
||||
@ -29,7 +29,7 @@ async function get(id, staff = false) {
|
||||
return await data;
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error(err, "ldbService.get: Error, Unable to find CRS")
|
||||
logger.error(err, "ldbService.get: Error, Unable to find CRS");
|
||||
return {
|
||||
obStatus: "LOC_NOT_FOUND",
|
||||
obMsg: "UNABLE TO FIND MESSAGE",
|
||||
@ -38,7 +38,7 @@ async function get(id, staff = false) {
|
||||
}
|
||||
|
||||
async function arrDepBoard(CRS) {
|
||||
logger.trace(`ldbService.arrDepBoard: Trying to fetch board for ${CRS}`)
|
||||
logger.trace(`ldbService.arrDepBoard: Trying to fetch board for ${CRS}`);
|
||||
try {
|
||||
const options = {
|
||||
numRows: 10,
|
||||
@ -48,7 +48,7 @@ async function arrDepBoard(CRS) {
|
||||
let d = await api.call("GetArrDepBoardWithDetails", options, false, false);
|
||||
return await util.cleanData(d);
|
||||
} catch (err) {
|
||||
logger.error(err, "ldbService.arrDepBoard: Lookup Failed")
|
||||
logger.error(err, "ldbService.arrDepBoard: Lookup Failed");
|
||||
return {
|
||||
GetStationBoardResult: "not available",
|
||||
Reason: `The CRS code ${CRS} is not valid`,
|
||||
@ -57,7 +57,7 @@ async function arrDepBoard(CRS) {
|
||||
}
|
||||
|
||||
async function arrDepBoardStaff(CRS) {
|
||||
logger.debug(`ldbService.arrDepBoardStaff: Try to fetch board for ${CRS}`)
|
||||
logger.debug(`ldbService.arrDepBoardStaff: Try to fetch board for ${CRS}`);
|
||||
try {
|
||||
const options = {
|
||||
numRows: 40,
|
||||
@ -75,24 +75,24 @@ async function arrDepBoardStaff(CRS) {
|
||||
false,
|
||||
false
|
||||
);
|
||||
console.log("\n\n\nORIGINAL DATA")
|
||||
console.log("\n" + JSON.stringify(result) + "\n\n\n")
|
||||
console.log("\n\n\nORIGINAL DATA");
|
||||
console.log("\n" + JSON.stringify(result) + "\n\n\n");
|
||||
console.timeEnd(`Fetch Staff LDB for ${CRS.toUpperCase()}`);
|
||||
try {
|
||||
const _staffLdb = staffStationTransform(result);
|
||||
logger.debug("StaffLDB Transformed")
|
||||
logger.trace(_staffLdb, "StaffLDB Transformed")
|
||||
logger.debug("StaffLDB Transformed");
|
||||
logger.trace(_staffLdb, "StaffLDB Transformed");
|
||||
return {
|
||||
obStatus: "OK",
|
||||
obMsg: "OK",
|
||||
data: _staffLdb,
|
||||
};
|
||||
} catch (err) {
|
||||
logger.error(err, "Transformation Error")
|
||||
logger.error(err, "Transformation Error");
|
||||
}
|
||||
return result;
|
||||
} catch (err) {
|
||||
logger.error(err, "ldbService.arrDepBoardStaff error")
|
||||
logger.error(err, "ldbService.arrDepBoardStaff error");
|
||||
return {
|
||||
GetStationBoardResult: "not available",
|
||||
Reason: `The CRS code ${CRS} is not valid`,
|
||||
@ -123,18 +123,18 @@ async function getServicesByOther(id) {
|
||||
const api = new ldb(ldbsvKey, true);
|
||||
return await api.call("QueryServices", options, false, false);
|
||||
} catch (err) {
|
||||
logger.error(err, "ldbService.getServiceByOther")
|
||||
logger.error(err, "ldbService.getServiceByOther");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function getReasonCodeList() {
|
||||
logger.debug("ldbService.getReasonCodeList: Fetching reason code list")
|
||||
logger.debug("ldbService.getReasonCodeList: Fetching reason code list");
|
||||
try {
|
||||
const dbFilter = {};
|
||||
return await db.query("reasonCodes", dbFilter, false);
|
||||
} catch (err) {
|
||||
logger.error(err, "ldbService.getReasonCodeList")
|
||||
logger.error(err, "ldbService.getReasonCodeList");
|
||||
}
|
||||
}
|
||||
|
||||
@ -146,7 +146,7 @@ async function getReasonCode(code) {
|
||||
};
|
||||
return await db.query("reasonCodes", dbFilter, false);
|
||||
} catch (err) {
|
||||
logger.error(err, "ldbService.getReasonCode")
|
||||
logger.error(err, "ldbService.getReasonCode");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,21 +9,22 @@ import { logger } from "./logger.utils";
|
||||
async function isAuthed(uuid: string): Promise<boolean> {
|
||||
// Needs testing
|
||||
const q = {
|
||||
uuid: uuid
|
||||
uuid: uuid,
|
||||
};
|
||||
const res = await db.query("users", q);
|
||||
logger.debug(res, "checkUser: DB Query Result")
|
||||
logger.debug(res, "checkUser: DB Query Result");
|
||||
const authorized = res && res[0] && res[0].domain;
|
||||
if (authorized) db.userAtime(uuid);
|
||||
return authorized;
|
||||
}
|
||||
|
||||
// Checks whether a registration request key is valid
|
||||
async function checkRequest(key: string) { // For some reason db.query seems to return correctly, but the second logs.out statement prints []??? so registration fails!!
|
||||
async function checkRequest(key: string) {
|
||||
// For some reason db.query seems to return correctly, but the second logs.out statement prints []??? so registration fails!!
|
||||
const collection = "registrations";
|
||||
const query = { uuid: key };
|
||||
const res = await db.query(collection, query);
|
||||
logger.debug(res, "checkRequest: DB Lookup result")
|
||||
logger.debug(res, "checkRequest: DB Lookup result");
|
||||
const result =
|
||||
res.length > 0 && res[0].time
|
||||
? { result: true, domain: res[0].domain }
|
||||
@ -50,7 +51,10 @@ async function generateConfirmationEmail(eml: string, uuid: string) {
|
||||
html: htmlMin,
|
||||
};
|
||||
} catch (err) {
|
||||
logger.error(err, "generateConfirmationEmail: Error rendering email templates")
|
||||
logger.error(
|
||||
err,
|
||||
"generateConfirmationEmail: Error rendering email templates"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,19 @@
|
||||
import pino from "pino";
|
||||
|
||||
const runtime = process.env.NODE_ENV
|
||||
let level: string
|
||||
if (runtime === "production") {
|
||||
level = "info"
|
||||
} else {
|
||||
level = "debug"
|
||||
}
|
||||
const runtime = process.env.NODE_ENV;
|
||||
let level: string;
|
||||
if (runtime === "production") {
|
||||
level = "info";
|
||||
} else {
|
||||
level = "debug";
|
||||
}
|
||||
|
||||
export const logger = pino({
|
||||
export const logger = pino({
|
||||
level: level,
|
||||
formatters: {
|
||||
level: (label) => {
|
||||
return { level: label.toUpperCase()}
|
||||
}
|
||||
return { level: label.toUpperCase() };
|
||||
},
|
||||
},
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
});
|
||||
});
|
||||
|
@ -5,7 +5,7 @@ const juice = require("juice");
|
||||
|
||||
// Inlines styles and minifies the inlined HTML
|
||||
async function minifyMail(input: string): Promise<string> {
|
||||
logger.trace("minifyMail: Minifying mail output")
|
||||
logger.trace("minifyMail: Minifying mail output");
|
||||
const inlined: string = juice(input);
|
||||
return htmlShrink(inlined, {
|
||||
removeComments: true,
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { logger } from "./logger.utils";
|
||||
|
||||
export function removeNewlineAndPTag(input: string): string {
|
||||
logger.debug("removeNewlineAndPTag: Cleaning string")
|
||||
logger.debug("removeNewlineAndPTag: Cleaning string");
|
||||
const regex = /[\n\r]|<\/?p[^>]*>/g;
|
||||
return input.replace(regex, function (match) {
|
||||
if (match === "\n" || match === "\r") {
|
||||
|
@ -3,17 +3,17 @@
|
||||
import { logger } from "./logger.utils";
|
||||
|
||||
function removeNonAlphanumeric(inputString: string) {
|
||||
logger.debug("removeNonAlphanumeric: Sanitizing string")
|
||||
logger.debug("removeNonAlphanumeric: Sanitizing string");
|
||||
return inputString.replace(/[^a-zA-Z0-9]/g, "");
|
||||
}
|
||||
|
||||
function removeNonAlpha(inputString: string) {
|
||||
logger.debug("removeNonAlpha: Sanitizing string")
|
||||
logger.debug("removeNonAlpha: Sanitizing string");
|
||||
return inputString.replace(/[^a-zA-Z]/g, "");
|
||||
}
|
||||
|
||||
function removeNonNumeric(inputString: string) {
|
||||
logger.debug("removeNonNumeric: Sanitizing string")
|
||||
logger.debug("removeNonNumeric: Sanitizing string");
|
||||
return inputString.replace(/[^0-9]/g, "");
|
||||
}
|
||||
|
||||
@ -21,14 +21,14 @@ const cleanApiEndpointTxt = removeNonAlpha;
|
||||
const cleanApiEndpointNum = removeNonAlphanumeric;
|
||||
|
||||
function cleanNrcc(input: string) {
|
||||
logger.error("DEPRECATED FUNCTION","cleanNrcc: Converting NRCC Data")
|
||||
logger.error("DEPRECATED FUNCTION", "cleanNrcc: Converting NRCC Data");
|
||||
// Remove newlines and then <p> tags from input
|
||||
const cleanInput = input.replace(/[\n\r]/g, "").replace(/<\/?p[^>]*>/g, "");
|
||||
return cleanInput;
|
||||
}
|
||||
|
||||
function getDomainFromEmail(mail: string) {
|
||||
logger.debug("getDomainFromEmail: Obtaining domain from email address")
|
||||
logger.debug("getDomainFromEmail: Obtaining domain from email address");
|
||||
// Needs testing
|
||||
let split = mail.split("@");
|
||||
return split[1].toLowerCase();
|
||||
|
@ -1,18 +1,18 @@
|
||||
import { logger } from "./logger.utils";
|
||||
|
||||
function unixLocal(unix: number): string {
|
||||
logger.trace(`unixLocal: Converting time: ${unix}`)
|
||||
logger.trace(`unixLocal: Converting time: ${unix}`);
|
||||
var jsTime = unix * 1000;
|
||||
var dt = new Date(jsTime);
|
||||
return dt.toLocaleString();
|
||||
}
|
||||
|
||||
function jsUnix(js: number): number {
|
||||
logger.trace(`jsUnix: Converting time: ${js}`)
|
||||
logger.trace(`jsUnix: Converting time: ${js}`);
|
||||
return Math.floor(js / 1000);
|
||||
}
|
||||
|
||||
export { jsUnix, unixLocal }
|
||||
export { jsUnix, unixLocal };
|
||||
|
||||
module.exports = {
|
||||
unixLocal,
|
||||
|
@ -16,7 +16,7 @@ export function transform(input: any): StaffLdb | null {
|
||||
try {
|
||||
output = {
|
||||
generatedAt: transformDateTime(data?.generatedAt) || new Date(),
|
||||
locationName: data?.locationName || 'Not Found',
|
||||
locationName: data?.locationName || "Not Found",
|
||||
stationManagerCode: data?.stationManagerCode || "UK",
|
||||
nrccMessages: transformNrcc(data?.nrccMessages) || undefined,
|
||||
trainServices: transformTrainServices(data?.trainServices) || undefined,
|
||||
@ -24,7 +24,7 @@ export function transform(input: any): StaffLdb | null {
|
||||
ferryServices: transformTrainServices(data?.ferryServices) || undefined,
|
||||
};
|
||||
console.timeEnd("StaffLdb Transformation");
|
||||
if (output.locationName !== 'Not Found') {
|
||||
if (output.locationName !== "Not Found") {
|
||||
return output;
|
||||
}
|
||||
} catch (err) {
|
||||
@ -43,7 +43,7 @@ function transformDateTime(input: string): Date {
|
||||
function transformNrcc(input: any): NrccMessage[] | undefined {
|
||||
//console.log("Transform Nrcc Running")
|
||||
if (input === undefined) {
|
||||
return input
|
||||
return input;
|
||||
}
|
||||
let output: NrccMessage[] = [];
|
||||
let messages = input;
|
||||
@ -74,7 +74,7 @@ function transformTrainServices(input: any): TrainServices[] {
|
||||
services = [input.service];
|
||||
}
|
||||
for (const service of services) {
|
||||
const times = parseTimes(service)
|
||||
const times = parseTimes(service);
|
||||
const trainService: TrainServices = {
|
||||
rid: service?.rid,
|
||||
uid: service?.uid,
|
||||
@ -157,9 +157,12 @@ function transformUnspecifiedDateTime(input: string): Date | undefined {
|
||||
}
|
||||
|
||||
function parseTimes(service: TrainServices) {
|
||||
let {sta, eta, ata, std, etd, atd} = Object.fromEntries(
|
||||
Object.entries(service).map(([key, value]) => [key, transformUnspecifiedDateTime(value)])
|
||||
)
|
||||
let { sta, eta, ata, std, etd, atd } = Object.fromEntries(
|
||||
Object.entries(service).map(([key, value]) => [
|
||||
key,
|
||||
transformUnspecifiedDateTime(value),
|
||||
])
|
||||
);
|
||||
|
||||
let etaResult: Date | undefined | string = eta;
|
||||
let ataResult: Date | undefined | string = ata;
|
||||
@ -167,19 +170,31 @@ function parseTimes(service: TrainServices) {
|
||||
let atdResult: Date | undefined | string = atd;
|
||||
|
||||
if (sta) {
|
||||
if (eta !== undefined && Math.abs(eta.getTime() - sta.getTime()) / 60000 <= 1.5) {
|
||||
if (
|
||||
eta !== undefined &&
|
||||
Math.abs(eta.getTime() - sta.getTime()) / 60000 <= 1.5
|
||||
) {
|
||||
etaResult = "RT";
|
||||
}
|
||||
if (ata !== undefined && Math.abs(ata.getTime() - sta.getTime()) / 60000 <= 1.5) {
|
||||
if (
|
||||
ata !== undefined &&
|
||||
Math.abs(ata.getTime() - sta.getTime()) / 60000 <= 1.5
|
||||
) {
|
||||
ataResult = "RT";
|
||||
}
|
||||
}
|
||||
|
||||
if (std) {
|
||||
if (etd !== undefined && Math.abs(etd.getTime() - std.getTime()) / 60000 <= 1.5) {
|
||||
if (
|
||||
etd !== undefined &&
|
||||
Math.abs(etd.getTime() - std.getTime()) / 60000 <= 1.5
|
||||
) {
|
||||
etdResult = "RT";
|
||||
}
|
||||
if (atd !== undefined && Math.abs(atd.getTime() - std.getTime()) / 60000 <= 1.5) {
|
||||
if (
|
||||
atd !== undefined &&
|
||||
Math.abs(atd.getTime() - std.getTime()) / 60000 <= 1.5
|
||||
) {
|
||||
atdResult = "RT";
|
||||
}
|
||||
}
|
||||
@ -191,5 +206,5 @@ function parseTimes(service: TrainServices) {
|
||||
std: std,
|
||||
etd: etdResult,
|
||||
atd: atdResult,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,17 +1,17 @@
|
||||
import { jsUnix, unixLocal } from "../../src/utils/timeConvert.utils";
|
||||
|
||||
describe("Time Conversion", () => {
|
||||
test('Should return unix time (seconds)', () => {
|
||||
test("Should return unix time (seconds)", () => {
|
||||
const now = new Date();
|
||||
const nowJs = now.getTime();
|
||||
const nowUnix = Math.floor(now.getTime() / 1000);
|
||||
expect(jsUnix(nowJs)).toEqual(nowUnix);
|
||||
})
|
||||
});
|
||||
|
||||
test('Should return locale date string', () => {
|
||||
test("Should return locale date string", () => {
|
||||
const now = new Date();
|
||||
const nowUnix = Math.floor(now.getTime() / 1000);
|
||||
const result = now.toLocaleString();
|
||||
expect(unixLocal(nowUnix)).toEqual(result);
|
||||
})
|
||||
});
|
||||
});
|
@ -52,11 +52,11 @@
|
||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
"sourceMap": true /* Create source map files for emitted JavaScript files. */,
|
||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
||||
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
|
||||
"removeComments": true, /* Disable emitting comments. */
|
||||
"removeComments": true /* Disable emitting comments. */,
|
||||
// "noEmit": true, /* Disable emitting files from a compilation. */
|
||||
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
||||
|
Loading…
Reference in New Issue
Block a user