From 51bae70d6cfefc8cbcd0f4d2177b6ac49ffa319a Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 27 Sep 2023 20:49:21 +0100 Subject: [PATCH] Run prettier Signed-off-by: Fred Boniface --- app.js | 4 +-- mail-templates/register.html | 3 +- src/configs/errorCodes.configs.ts | 16 ++++++--- src/middlewares/auth.middlewares.js | 10 +++--- src/services/dbAccess.services.js | 10 ++++-- src/services/ldb.services.js | 30 ++++++++-------- src/utils/auth.utils.ts | 14 +++++--- src/utils/logger.utils.ts | 30 ++++++++-------- src/utils/minify.utils.ts | 2 +- src/utils/newSanitizer.ts | 2 +- src/utils/sanitizer.utils.ts | 10 +++--- src/utils/timeConvert.utils.ts | 6 ++-- src/utils/translators/ldb/staffStation.ts | 43 +++++++++++++++-------- test/utils/timeConvert.utils.test.ts | 10 +++--- tsconfig.json | 4 +-- 15 files changed, 112 insertions(+), 82 deletions(-) diff --git a/app.js b/app.js index a7820e2..344a8e8 100644 --- a/app.js +++ b/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; }); diff --git a/mail-templates/register.html b/mail-templates/register.html index 430a847..8baae9e 100644 --- a/mail-templates/register.html +++ b/mail-templates/register.html @@ -54,7 +54,8 @@

Register for OwlBoard


- 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.


([ - ["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([ + [ + "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 }; diff --git a/src/middlewares/auth.middlewares.js b/src/middlewares/auth.middlewares.js index 306912d..9d021c8 100644 --- a/src/middlewares/auth.middlewares.js +++ b/src/middlewares/auth.middlewares.js @@ -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); } diff --git a/src/services/dbAccess.services.js b/src/services/dbAccess.services.js index 74c7f27..1dcf2ad 100644 --- a/src/services/dbAccess.services.js +++ b/src/services/dbAccess.services.js @@ -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 = {}; diff --git a/src/services/ldb.services.js b/src/services/ldb.services.js index 4d53d5c..5b3e417 100644 --- a/src/services/ldb.services.js +++ b/src/services/ldb.services.js @@ -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"); } } diff --git a/src/utils/auth.utils.ts b/src/utils/auth.utils.ts index 082d726..cd287ea 100644 --- a/src/utils/auth.utils.ts +++ b/src/utils/auth.utils.ts @@ -9,21 +9,22 @@ import { logger } from "./logger.utils"; async function isAuthed(uuid: string): Promise { // 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; } } diff --git a/src/utils/logger.utils.ts b/src/utils/logger.utils.ts index 6d9a6a6..a4daaf8 100644 --- a/src/utils/logger.utils.ts +++ b/src/utils/logger.utils.ts @@ -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({ - level: level, - formatters: { - level: (label) => { - return { level: label.toUpperCase()} - } +export const logger = pino({ + level: level, + formatters: { + level: (label) => { + return { level: label.toUpperCase() }; }, - timestamp: pino.stdTimeFunctions.isoTime, - }); + }, + timestamp: pino.stdTimeFunctions.isoTime, +}); diff --git a/src/utils/minify.utils.ts b/src/utils/minify.utils.ts index 83c7821..915a195 100644 --- a/src/utils/minify.utils.ts +++ b/src/utils/minify.utils.ts @@ -5,7 +5,7 @@ const juice = require("juice"); // Inlines styles and minifies the inlined HTML async function minifyMail(input: string): Promise { - logger.trace("minifyMail: Minifying mail output") + logger.trace("minifyMail: Minifying mail output"); const inlined: string = juice(input); return htmlShrink(inlined, { removeComments: true, diff --git a/src/utils/newSanitizer.ts b/src/utils/newSanitizer.ts index 4597856..1d0eb3f 100644 --- a/src/utils/newSanitizer.ts +++ b/src/utils/newSanitizer.ts @@ -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") { diff --git a/src/utils/sanitizer.utils.ts b/src/utils/sanitizer.utils.ts index 604fc8a..f7ebee8 100644 --- a/src/utils/sanitizer.utils.ts +++ b/src/utils/sanitizer.utils.ts @@ -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

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(); diff --git a/src/utils/timeConvert.utils.ts b/src/utils/timeConvert.utils.ts index 68c6d6d..781e0af 100644 --- a/src/utils/timeConvert.utils.ts +++ b/src/utils/timeConvert.utils.ts @@ -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, diff --git a/src/utils/translators/ldb/staffStation.ts b/src/utils/translators/ldb/staffStation.ts index 936c0bd..73865d3 100644 --- a/src/utils/translators/ldb/staffStation.ts +++ b/src/utils/translators/ldb/staffStation.ts @@ -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,29 +157,44 @@ 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; 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) { + 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, - } + }; } diff --git a/test/utils/timeConvert.utils.test.ts b/test/utils/timeConvert.utils.test.ts index 520221f..cfa3f93 100644 --- a/test/utils/timeConvert.utils.test.ts +++ b/test/utils/timeConvert.utils.test.ts @@ -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); - }) -}); \ No newline at end of file + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 2aff194..8488067 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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. */