newStaffLDB-API (#48)

Merge newStaffLDB-API into main.

Ready to deploy

Reviewed-on: #48
This commit is contained in:
Fred Boniface
2023-10-03 21:35:00 +01:00
parent 5db9d8e52a
commit 0a25ae85f5
61 changed files with 2498 additions and 520705 deletions

View File

@@ -1,19 +1,18 @@
const logs = require("../utils/logs.utils");
const crypt = require("crypto");
const db = require("../services/dbAccess.services");
const fs = require("fs/promises");
import { minifyMail } from "./minify.utils";
import { logger } from "./logger.utils";
// Checks users registration key against issued keys
async function isAuthed(uuid: string) {
async function isAuthed(uuid: string): Promise<boolean> {
// Needs testing
const q = { uuid: uuid };
const q = {
uuid: uuid,
};
const res = await db.query("users", q);
logs.out(
"authUtils.checkUser: DB Query answer: " + JSON.stringify(res[0]),
"dbug"
);
logger.debug(res, "checkUser: DB Query Result");
const authorized = res && res[0] && res[0].domain;
if (authorized) db.userAtime(uuid);
return authorized;
@@ -21,13 +20,11 @@ async function isAuthed(uuid: string) {
// 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!!
const collection = "registrations";
const query = { uuid: key };
const res = await db.query(collection, query);
logs.out(
"authUtils.checkRequest: DB Query result: " + JSON.stringify(res),
"dbug"
);
logger.debug(res, "checkRequest: DB Lookup result");
const result =
res.length > 0 && res[0].time
? { result: true, domain: res[0].domain }
@@ -37,7 +34,7 @@ async function checkRequest(key: string) {
// Creates an API key for a user
async function generateKey() {
// Needs testing & moving to 'register.utils'
// Needs testing & moving to 'register.utils' ??? Why does it need moving?
return crypt.randomUUID();
}
@@ -54,9 +51,9 @@ async function generateConfirmationEmail(eml: string, uuid: string) {
html: htmlMin,
};
} catch (err) {
logs.out(
"mailServices.generateConfirmationEmail: Error reading template, " + err,
"err"
logger.error(
err,
"generateConfirmationEmail: Error rendering email templates"
);
return false;
}

View File

@@ -18,6 +18,7 @@ async function checkCrs(input = "") {
}
// Needs to be moved to the frontend `ensureArray() func`
// Usage of this function should be migrated to the `translator` utilities.
async function cleanMessages(input) {
log.out("ldbUtils.cleanMessages: Deprecated function has been called", "err");
var out = [];

19
src/utils/logger.utils.ts Normal file
View File

@@ -0,0 +1,19 @@
import pino from "pino";
const runtime = process.env.NODE_ENV;
let level: string;
if (runtime === "production") {
level = "info";
} else {
level = "debug";
}
export const logger = pino({
level: level,
formatters: {
level: (label) => {
return { level: label.toUpperCase() };
},
},
timestamp: pino.stdTimeFunctions.isoTime,
});

View File

@@ -1,7 +1,11 @@
import { logger } from "./logger.utils";
const htmlShrink = require("html-minifier").minify;
const juice = require("juice");
// Inlines styles and minifies the inlined HTML
async function minifyMail(input: string): Promise<string> {
logger.trace("minifyMail: Minifying mail output");
const inlined: string = juice(input);
return htmlShrink(inlined, {
removeComments: true,

View File

@@ -1,4 +1,7 @@
import { logger } from "./logger.utils";
export function removeNewlineAndPTag(input: string): string {
logger.debug("removeNewlineAndPTag: Cleaning string");
const regex = /[\n\r]|<\/?p[^>]*>/g;
return input.replace(regex, function (match) {
if (match === "\n" || match === "\r") {

View File

@@ -1,14 +1,19 @@
//const log = require('../utils/log.utils');
import { logger } from "./logger.utils";
function removeNonAlphanumeric(inputString: string) {
logger.debug("removeNonAlphanumeric: Sanitizing string");
return inputString.replace(/[^a-zA-Z0-9]/g, "");
}
function removeNonAlpha(inputString: string) {
logger.debug("removeNonAlpha: Sanitizing string");
return inputString.replace(/[^a-zA-Z]/g, "");
}
function removeNonNumeric(inputString: string) {
logger.debug("removeNonNumeric: Sanitizing string");
return inputString.replace(/[^0-9]/g, "");
}
@@ -16,15 +21,17 @@ const cleanApiEndpointTxt = removeNonAlpha;
const cleanApiEndpointNum = removeNonAlphanumeric;
function cleanNrcc(input: string) {
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");
// Needs testing
let split = mail.split("@");
return split[1];
return split[1].toLowerCase();
}
module.exports = {

View File

@@ -1,14 +1,19 @@
function unixLocal(unix: number) {
import { logger } from "./logger.utils";
function unixLocal(unix: number): string {
logger.trace(`unixLocal: Converting time: ${unix}`);
var jsTime = unix * 1000;
var dt = new Date(jsTime);
return dt.toLocaleString();
}
function jsUnix(js: number) {
var preRound = js / 1000;
return Math.round(preRound);
function jsUnix(js: number): number {
logger.trace(`jsUnix: Converting time: ${js}`);
return Math.floor(js / 1000);
}
export { jsUnix, unixLocal };
module.exports = {
unixLocal,
jsUnix,

View File

@@ -8,6 +8,8 @@ import type {
import { tz } from "moment-timezone";
import { removeNewlineAndPTag } from "../../newSanitizer";
import { logger } from "../../logger.utils";
/// I do not yet have a type defined for any of the input object
export function transform(input: any): StaffLdb | null {
console.time("StaffLdb Transformation");
@@ -24,20 +26,26 @@ export function transform(input: any): StaffLdb | null {
ferryServices: transformTrainServices(data?.ferryServices) || undefined,
};
console.timeEnd("StaffLdb Transformation");
return output;
if (output.locationName !== "Not Found") {
return output;
}
} catch (err) {
console.log("utils/translators/ldb/staffLdb.transform: Caught Error");
console.log("Unable to parse data, assuming no data: " + err);
logger.error(err, "utils/translators/ldb/staffLdb.transform");
}
console.timeEnd("StaffLdb Transformation");
return null;
}
function transformDateTime(input: string): Date {
logger.trace("utils/translators/ldb/staffLdb.transformDateTime: Running");
return new Date(input);
}
function transformNrcc(input: any): NrccMessage[] | undefined {
logger.trace("utils/translators/ldb/staffLdb.transformNrcc: Running");
if (input === undefined) {
return input;
}
let output: NrccMessage[] = [];
let messages = input;
if (!Array.isArray(input?.message)) {
@@ -57,6 +65,9 @@ function transformNrcc(input: any): NrccMessage[] | undefined {
}
function transformTrainServices(input: any): TrainServices[] {
logger.trace(
"utils/translators/ldb/staffLdb.transformTrainServices: Running"
);
let services: any = input?.service;
let output: TrainServices[] = [];
if (services === undefined) {
@@ -66,6 +77,7 @@ function transformTrainServices(input: any): TrainServices[] {
services = [input.service];
}
for (const service of services) {
const times = parseTimes(service);
const trainService: TrainServices = {
rid: service?.rid,
uid: service?.uid,
@@ -73,7 +85,7 @@ function transformTrainServices(input: any): TrainServices[] {
operatorCode: service?.operatorCode || "UK",
platform: service?.platform || "-",
platformIsHidden: service?.platformIsHidden,
serviceIsSupressed: service?.serviceIsSupressed,
serviceIsSupressed: checkIsSupressed(service),
origin: transformLocation(service?.origin),
destination: transformLocation(service?.destination),
length: calculateLength(service),
@@ -82,12 +94,12 @@ function transformTrainServices(input: any): TrainServices[] {
delayReason: service?.delayReason,
arrivalType: service?.arrivalType,
departureType: service?.departureType,
sta: transformUnspecifiedDateTime(service?.sta),
eta: transformUnspecifiedDateTime(service?.eta),
ata: transformUnspecifiedDateTime(service?.ata),
std: transformUnspecifiedDateTime(service?.std),
etd: transformUnspecifiedDateTime(service?.etd),
atd: transformUnspecifiedDateTime(service?.atd),
sta: times.sta,
eta: times.eta,
ata: times.ata,
std: times.std,
etd: times.etd,
atd: times.atd,
};
Object.keys(trainService).forEach(
(key) => trainService[key] === undefined && delete trainService[key]
@@ -97,7 +109,17 @@ function transformTrainServices(input: any): TrainServices[] {
return output;
}
function checkIsSupressed(service: TrainServices): string | undefined {
logger.trace("utils/translators/ldb/staffStation.checkIsSupressed: Running");
if (service.serviceIsSupressed === "true" || service.isPassengerService === "false") {
return "true";
} else {
return undefined;
}
}
function transformLocation(input: any): ServiceLocation[] {
logger.trace("utils/translators/ldb/staffStation.transformLocation: Running");
let output: ServiceLocation[] = [];
let locations: any[] = input.location;
if (!Array.isArray(input.location)) {
@@ -116,6 +138,7 @@ function transformLocation(input: any): ServiceLocation[] {
}
export function calculateLength(input: any): number | undefined {
logger.trace("utils/translators/ldb/staffStation.calculateLength: Running");
let length: number;
if (input?.length) {
length = input.length;
@@ -129,9 +152,66 @@ export function calculateLength(input: any): number | undefined {
}
function transformUnspecifiedDateTime(input: string): Date | undefined {
logger.trace(
"utils/translators/ldb/staffStation.transformUnspecifiedDateTime: Running"
);
if (!input) {
return undefined;
}
const date = tz(input, "Europe/London");
const date = tz(input, "Europe/London"); // Want to be creating a moment object using moment.tz(...)
return date.toDate();
}
function parseTimes(service: TrainServices) {
logger.trace("utils/translators/ldb/staffStation.parseTimes: Running");
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;
let etdResult: Date | undefined | string = etd;
let atdResult: Date | undefined | string = atd;
if (sta) {
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
) {
ataResult = "RT";
}
}
if (std) {
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
) {
atdResult = "RT";
}
}
return {
sta: sta,
eta: etaResult,
ata: ataResult,
std: std,
etd: etdResult,
atd: atdResult,
};
}