Run prettier

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface
2023-08-26 00:38:26 +01:00
parent c7b1c547b0
commit 31f104f51b
74 changed files with 520386 additions and 1121 deletions

View File

@@ -1,16 +1,19 @@
const logs = require('../utils/logs.utils');
const crypt = require('crypto');
const db = require('../services/dbAccess.services');
const fs = require('fs/promises');
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";
// Checks users registration key against issued keys
async function isAuthed(uuid: string) { // Needs testing
const q = {uuid: uuid};
const res = await db.query('users', q);
logs.out('authUtils.checkUser: DB Query answer: ' +
JSON.stringify(res[0]), 'dbug');
async function isAuthed(uuid: string) {
// Needs testing
const q = { uuid: uuid };
const res = await db.query("users", q);
logs.out(
"authUtils.checkUser: DB Query answer: " + JSON.stringify(res[0]),
"dbug"
);
const authorized = res && res[0] && res[0].domain;
if (authorized) db.userAtime(uuid);
return authorized;
@@ -18,37 +21,43 @@ async function isAuthed(uuid: string) { // Needs testing
// Checks whether a registration request key is valid
async function checkRequest(key: string) {
const collection = 'registrations';
const query = {uuid: key};
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');
const result = res.length > 0 && res[0].time
? { result: true, domain: res[0].domain }
: { result: false };
logs.out(
"authUtils.checkRequest: DB Query result: " + JSON.stringify(res),
"dbug"
);
const result =
res.length > 0 && res[0].time
? { result: true, domain: res[0].domain }
: { result: false };
return result;
}
// Creates an API key for a user
async function generateKey() { // Needs testing & moving to 'register.utils'
async function generateKey() {
// Needs testing & moving to 'register.utils'
return crypt.randomUUID();
}
async function generateConfirmationEmail(eml: string, uuid: string) {
try {
const htmlTpl = await fs.readFile('mail-templates/register.html', 'utf-8');
const htmlTpl = await fs.readFile("mail-templates/register.html", "utf-8");
const htmlStr = htmlTpl.replace(/>>ACCESSCODE<</g, uuid);
const htmlMin = await minifyMail(htmlStr);
const txtTpl = fs.readFile('mail-templates/register.txt', 'utf-8');
const txtTpl = fs.readFile("mail-templates/register.txt", "utf-8");
return {
to: eml,
subject: 'OwlBoard Registration',
subject: "OwlBoard Registration",
text: (await txtTpl).replace(/>>ACCESSCODE<</g, uuid),
html: htmlMin
html: htmlMin,
};
} catch(err) {
logs.out('mailServices.generateConfirmationEmail: Error reading template, ' +
err, 'err');
} catch (err) {
logs.out(
"mailServices.generateConfirmationEmail: Error reading template, " + err,
"err"
);
return false;
}
}
@@ -57,12 +66,7 @@ module.exports = {
isAuthed,
generateKey,
generateConfirmationEmail,
checkRequest
checkRequest,
};
export {
isAuthed,
generateKey,
generateConfirmationEmail,
checkRequest
}
export { isAuthed, generateKey, generateConfirmationEmail, checkRequest };

View File

@@ -1,32 +1,30 @@
const log = require('./logs.utils'); // Log Helper
const db = require('../services/dbAccess.services'); // DB Access
const log = require("./logs.utils"); // Log Helper
const db = require("../services/dbAccess.services"); // DB Access
//const san = require('../utils/sanitizer.utils'); // Sanitiser
import * as san from '../utils/sanitizer.utils'
import * as san from "../utils/sanitizer.utils";
async function checkCrs(input = ""){
async function checkCrs(input = "") {
var INPUT = input.toUpperCase();
var query = {
'$or':[
{'3ALPHA':INPUT},
{'TIPLOC':INPUT},
{'STANOX':INPUT}
]
$or: [{ "3ALPHA": INPUT }, { TIPLOC: INPUT }, { STANOX: INPUT }],
};
var result = await db.query('stations', query);
log.out('ldbUtils.checkCrs: Query results: ' +
JSON.stringify(result), 'dbug');
var result = await db.query("stations", query);
log.out(
"ldbUtils.checkCrs: Query results: " + JSON.stringify(result),
"dbug"
);
return result;
}
// Needs to be moved to the frontend `ensureArray() func`
async function cleanMessages(input){
log.out('ldbUtils.cleanMessages: Deprecated function has been called', 'err');
async function cleanMessages(input) {
log.out("ldbUtils.cleanMessages: Deprecated function has been called", "err");
var out = [];
if (typeof input.message == 'string') {
if (typeof input.message == "string") {
out.push(san.cleanNrcc(input.message));
} else if (typeof input.message == 'object') {
for(var i = 0; i < input.message.length; i++) {
} else if (typeof input.message == "object") {
for (var i = 0; i < input.message.length; i++) {
out.push(san.cleanNrcc(input.message[i]));
}
}
@@ -34,13 +32,13 @@ async function cleanMessages(input){
}
// Accepts an object but not an Array and returns it wrapped in an array.
async function cleanServices(input){
log.out('ldbUtils.cleanServices: Deprecated function has been called', 'err');
async function cleanServices(input) {
log.out("ldbUtils.cleanServices: Deprecated function has been called", "err");
var out = [];
if (!Array.isArray(input)) {
log.out(`ldbUtils.cleanServices: Transforming input: ${input}`, 'dbug');
log.out(`ldbUtils.cleanServices: Transforming input: ${input}`, "dbug");
out.push(input);
log.out(`ldbUtils.cleanServices: Returning output: ${out}`, 'dbug');
log.out(`ldbUtils.cleanServices: Returning output: ${out}`, "dbug");
return out;
} else {
return input;
@@ -50,19 +48,31 @@ async function cleanServices(input){
async function cleanData(input) {
try {
if (input?.GetStationBoardResult?.trainServices) {
log.out('ldbUtils.cleanData: Changing train service data to array','dbug');
input.GetStationBoardResult.trainServices.service = await ensureArray(input.GetStationBoardResult.trainServices.service);
log.out(
"ldbUtils.cleanData: Changing train service data to array",
"dbug"
);
input.GetStationBoardResult.trainServices.service = await ensureArray(
input.GetStationBoardResult.trainServices.service
);
}
if (input?.GetStationBoardResult?.busServices) {
log.out('ldbUtils.cleanData: Changing bus service data to array','dbug');
input.GetStationBoardResult.busServices.service = await ensureArray(input.GetStationBoardResult.busServices.service);
log.out("ldbUtils.cleanData: Changing bus service data to array", "dbug");
input.GetStationBoardResult.busServices.service = await ensureArray(
input.GetStationBoardResult.busServices.service
);
}
if (input?.GetStationBoardResult?.ferryServices) {
log.out('ldbUtils.cleanData: Changing ferry service data to array','dbug');
input.GetStationBoardResult.ferryServices.service = await ensureArray(input.GetStationBoardResult.ferryServices.service);
log.out(
"ldbUtils.cleanData: Changing ferry service data to array",
"dbug"
);
input.GetStationBoardResult.ferryServices.service = await ensureArray(
input.GetStationBoardResult.ferryServices.service
);
}
} catch (err) {
log.out(`ldbUtils.cleanData: Error: ${err}`, 'eror');
log.out(`ldbUtils.cleanData: Error: ${err}`, "eror");
}
return input;
}
@@ -78,5 +88,5 @@ module.exports = {
checkCrs,
cleanMessages,
cleanServices,
cleanData
};
cleanData,
};

View File

@@ -1,10 +1,12 @@
const environment: string = process.env.NODE_ENV || "Unknown";
const hideInProduction: string[] = ['info', 'dbug'];
const hideInProduction: string[] = ["info", "dbug"];
async function out(msg: string, level = 'othr') {
if (environment === 'production' &&
hideInProduction.includes(level.toLowerCase())) {
async function out(msg: string, level = "othr") {
if (
environment === "production" &&
hideInProduction.includes(level.toLowerCase())
) {
return;
} else {
const time = new Date().toISOString();
@@ -13,7 +15,7 @@ async function out(msg: string, level = 'othr') {
}
module.exports = {
out
out,
};
export { out }
export { out };

View File

@@ -1,13 +1,13 @@
const htmlShrink = require('html-minifier').minify;
const juice = require('juice');
const htmlShrink = require("html-minifier").minify;
const juice = require("juice");
async function minifyMail(input: string): Promise<string> {
const inlined: string = juice(input);
return htmlShrink(inlined, {
removeComments: true,
collapseWhitespace: true
collapseWhitespace: true,
});
};
}
module.exports = {minifyMail}
export {minifyMail}
module.exports = { minifyMail };
export { minifyMail };

View File

@@ -1,10 +1,10 @@
export function removeNewlineAndPTag(input: string): string {
const regex = /[\n\r]|<\/?p[^>]*>/g;
return input.replace(regex, function(match) {
return input.replace(regex, function (match) {
if (match === "\n" || match === "\r") {
return "";
} else {
return "";
}
});
}
}

View File

@@ -1,27 +1,29 @@
//const log = require('../utils/log.utils');
function removeNonAlphanumeric(inputString: string) {
return inputString.replace(/[^a-zA-Z0-9]/g, '');
return inputString.replace(/[^a-zA-Z0-9]/g, "");
}
function removeNonAlpha(inputString: string) {
return inputString.replace(/[^a-zA-Z]/g, '');
function removeNonAlpha(inputString: string) {
return inputString.replace(/[^a-zA-Z]/g, "");
}
function removeNonNumeric(inputString: string) {
return inputString.replace(/[^0-9]/g, '');
return inputString.replace(/[^0-9]/g, "");
}
const cleanApiEndpointTxt = removeNonAlpha;
const cleanApiEndpointNum = removeNonAlphanumeric;
function cleanNrcc(input: string) { // Remove newlines and then <p> tags from input
const cleanInput = input.replace(/[\n\r]/g, '').replace(/<\/?p[^>]*>/g, '');
function cleanNrcc(input: string) {
// Remove newlines and then <p> tags from input
const cleanInput = input.replace(/[\n\r]/g, "").replace(/<\/?p[^>]*>/g, "");
return cleanInput;
}
function getDomainFromEmail(mail: string) { // Needs testing
let split = mail.split('@');
function getDomainFromEmail(mail: string) {
// Needs testing
let split = mail.split("@");
return split[1];
}
@@ -43,4 +45,4 @@ export {
removeNonNumeric,
cleanNrcc,
getDomainFromEmail,
}
};

View File

@@ -1,5 +1,5 @@
function unixLocal(unix: number) {
var jsTime = unix*1000;
var jsTime = unix * 1000;
var dt = new Date(jsTime);
return dt.toLocaleString();
}
@@ -12,4 +12,4 @@ function jsUnix(js: number) {
module.exports = {
unixLocal,
jsUnix,
};
};

View File

@@ -4,4 +4,4 @@ The utilities in the `translators` folder translate the upstream API into the do
The aim of the translators is to ensure a consistent data format while removing any unused data to keep the response sizes as small as possible.
Translators are kept in separate files so changes can be made in one place. Each translator exports a single function 'transform()'. This function accepts data from the upstream API and uses other functions in the file to build the API response object before returning that object to the caller.
Translators are kept in separate files so changes can be made in one place. Each translator exports a single function 'transform()'. This function accepts data from the upstream API and uses other functions in the file to build the API response object before returning that object to the caller.

View File

@@ -1,14 +1,18 @@
import type { StaffLdb, NrccMessage, TrainServices,
ServiceLocation } from '@owlboard/ts-types';
import type {
StaffLdb,
NrccMessage,
TrainServices,
ServiceLocation,
} from "@owlboard/ts-types";
import { tz } from 'moment-timezone';
import { removeNewlineAndPTag } from '../../newSanitizer';
import { tz } from "moment-timezone";
import { removeNewlineAndPTag } from "../../newSanitizer";
/// 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")
const data = input.GetBoardResult
let output: StaffLdb
console.time("StaffLdb Transformation");
const data = input.GetBoardResult;
let output: StaffLdb;
try {
output = {
generatedAt: transformDateTime(data?.generatedAt) || new Date(),
@@ -17,57 +21,57 @@ export function transform(input: any): StaffLdb | null {
nrccMessages: transformNrcc(data?.nrccMessages) || undefined,
trainServices: transformTrainServices(data?.trainServices) || undefined,
busServices: transformTrainServices(data?.busServices) || undefined,
ferryServices: transformTrainServices(data?.ferryServices) || undefined
}
console.timeEnd("StaffLdb Transformation")
return output
ferryServices: transformTrainServices(data?.ferryServices) || undefined,
};
console.timeEnd("StaffLdb Transformation");
return output;
} catch (err) {
console.log("utils/translators/ldb/staffLdb.transform: Caught Error")
console.log('Unable to parse data, assuming no data: ' + err)
console.log("utils/translators/ldb/staffLdb.transform: Caught Error");
console.log("Unable to parse data, assuming no data: " + err);
}
console.timeEnd("StaffLdb Transformation")
return null
console.timeEnd("StaffLdb Transformation");
return null;
}
function transformDateTime(input: string): Date {
return new Date(input)
return new Date(input);
}
function transformNrcc(input: any): NrccMessage[] | undefined {
let output: NrccMessage[] = []
let messages = input
let output: NrccMessage[] = [];
let messages = input;
if (!Array.isArray(input?.message)) {
messages = [input?.message]
messages = [input?.message];
}
if (messages.length) {
for (const item of messages) {
let message: NrccMessage = {
severity: item?.severity,
xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage)
}
output.push(message)
xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage),
};
output.push(message);
}
return output
}
return undefined
return output;
}
return undefined;
}
function transformTrainServices(input: any): TrainServices[] {
let services: any = input?.service
let output: TrainServices[] = []
let services: any = input?.service;
let output: TrainServices[] = [];
if (services === undefined) {
return output
return output;
}
if (!Array.isArray(input.service)) {
services = [input.service]
services = [input.service];
}
for (const service of services) {
const trainService: TrainServices = {
rid: service?.rid,
uid: service?.uid,
trainid: service?.trainid,
operatorCode: service?.operatorCode || 'UK',
platform: service?.platform || '-',
operatorCode: service?.operatorCode || "UK",
platform: service?.platform || "-",
platformIsHidden: service?.platformIsHidden,
serviceIsSupressed: service?.serviceIsSupressed,
origin: transformLocation(service?.origin),
@@ -84,48 +88,50 @@ function transformTrainServices(input: any): TrainServices[] {
std: transformUnspecifiedDateTime(service?.std),
etd: transformUnspecifiedDateTime(service?.etd),
atd: transformUnspecifiedDateTime(service?.atd),
}
Object.keys(trainService).forEach(key => trainService[key] === undefined && delete trainService[key]);
output.push(trainService)
};
Object.keys(trainService).forEach(
(key) => trainService[key] === undefined && delete trainService[key]
);
output.push(trainService);
}
return output
return output;
}
function transformLocation(input: any): ServiceLocation[] {
let output: ServiceLocation[] = []
let locations: any[] = input.location
let output: ServiceLocation[] = [];
let locations: any[] = input.location;
if (!Array.isArray(input.location)) {
locations = [input.location]
locations = [input.location];
}
for (const item of locations) {
const location: ServiceLocation = {
tiploc: item?.tiploc
}
tiploc: item?.tiploc,
};
if (item?.via) {
location.via = item.via
location.via = item.via;
}
output.push(location)
output.push(location);
}
return output
return output;
}
export function calculateLength(input: any): number | undefined {
let length: number;
if (input?.length) {
length = input.length
return Number(length)
length = input.length;
return Number(length);
}
if (input?.formation?.coaches?.coach) {
length = input.formation.coaches.coach.length
return Number(length)
length = input.formation.coaches.coach.length;
return Number(length);
}
return undefined
return undefined;
}
function transformUnspecifiedDateTime(input: string): Date | undefined {
if (!input) {
return undefined
return undefined;
}
const date = tz(input, "Europe/London");
return date.toDate()
return date.toDate();
}