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,44 +1,35 @@
module.exports = { module.exports = {
'env': { env: {
'browser': false, browser: false,
'node': true, node: true,
'commonjs': true, commonjs: true,
'es2021': true es2021: true,
}, },
'extends': 'eslint:recommended', extends: "eslint:recommended",
'overrides': [ overrides: [
{ {
files: ['**/*.ts','**/*.js'], files: ["**/*.ts", "**/*.js"],
parser: '@typescript-eslint/parser', parser: "@typescript-eslint/parser",
plugins: ['@typescript-eslint'], plugins: ["@typescript-eslint"],
extends: ['plugin:@typescript-eslint/eslint-recommended', 'plugin:@typescript-eslint/recommended'] extends: [
} "plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
], ],
'parserOptions': {
'ecmaVersion': 'latest'
}, },
'rules': {
'indent': [
'error',
2
], ],
'linebreak-style': [ parserOptions: {
'error', ecmaVersion: "latest",
'unix' },
], rules: {
'quotes': [ indent: ["error", 2],
'error', "linebreak-style": ["error", "unix"],
'single' quotes: ["error", "single"],
], semi: ["error", "always"],
'semi': [ "max-len": [
'error', "warn",
'always'
],
'max-len': [
'warn',
{ {
'code': 80 code: 80,
} },
] ],
} },
}; };

File diff suppressed because one or more lines are too long

View File

@ -1 +1,11 @@
{"GetStationBoardResult":{"generatedAt":"2023-01-14T11:23:12.6558466+00:00","locationName":"Pilning","crs":"PIL","nrccMessages":{"message":"\nPoor weather affecting services in Wales due to flooding on the railway More details can be found in <a href=\"https://t.co/uBU966PUmX\">Latest Travel News</a>."},"platformAvailable":"true"}} {
"GetStationBoardResult": {
"generatedAt": "2023-01-14T11:23:12.6558466+00:00",
"locationName": "Pilning",
"crs": "PIL",
"nrccMessages": {
"message": "\nPoor weather affecting services in Wales due to flooding on the railway More details can be found in <a href=\"https://t.co/uBU966PUmX\">Latest Travel News</a>."
},
"platformAvailable": "true"
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
version: '3.1' version: "3.1"
services: services:
mongo: mongo:

View File

@ -1,27 +1,22 @@
{"service": {
[ "service": [
{"sta":"16:07", {
"sta": "16:07",
"eta": "On time", "eta": "On time",
"operator": "South Western Railway", "operator": "South Western Railway",
"operatorCode": "SW", "operatorCode": "SW",
"serviceType": "ferry", "serviceType": "ferry",
"serviceID": "37782PHBR____", "serviceID": "37782PHBR____",
"origin": "origin": {
{"location": "location": { "locationName": "Ryde Pier Head", "crs": "RYP" }
{"locationName":
"Ryde Pier Head","crs":"RYP"
}
}, },
"destination": "destination": {
{"location": "location": { "locationName": "Portsmouth Harbour", "crs": "PMH" }
{"locationName":"Portsmouth Harbour",
"crs":"PMH"
}
}, },
"previousCallingPoints": "previousCallingPoints": {
{"callingPointList": "callingPointList": {
{"callingPoint": "callingPoint": {
{"locationName":"Ryde Pier Head", "locationName": "Ryde Pier Head",
"crs": "RYP", "crs": "RYP",
"st": "15:45", "st": "15:45",
"et": "On time" "et": "On time"
@ -29,5 +24,29 @@
} }
} }
}, },
{"std":"16:15","etd":"On time","operator":"South Western Railway","operatorCode":"SW","serviceType":"ferry","serviceID":"37746PHBR____","origin":{"location":{"locationName":"Portsmouth Harbour","crs":"PMH"}},"destination":{"location":{"locationName":"Ryde Pier Head","crs":"RYP"}},"subsequentCallingPoints":{"callingPointList":{"callingPoint": {
{"locationName":"Ryde Pier Head","crs":"RYP","st":"16:37","et":"On time"}}}}]} "std": "16:15",
"etd": "On time",
"operator": "South Western Railway",
"operatorCode": "SW",
"serviceType": "ferry",
"serviceID": "37746PHBR____",
"origin": {
"location": { "locationName": "Portsmouth Harbour", "crs": "PMH" }
},
"destination": {
"location": { "locationName": "Ryde Pier Head", "crs": "RYP" }
},
"subsequentCallingPoints": {
"callingPointList": {
"callingPoint": {
"locationName": "Ryde Pier Head",
"crs": "RYP",
"st": "16:37",
"et": "On time"
}
}
}
}
]
}

View File

@ -18,7 +18,7 @@ Configuration options are set through environment variables.
These configuration options are shared with other programs in the OwlBoard ecosystem. These configuration options are shared with other programs in the OwlBoard ecosystem.
| VAR | DEFAULT | REQUIRED | PURPOSE | | VAR | DEFAULT | REQUIRED | PURPOSE |
|:-:|:-----:|:------:|:-----:| | :-----------------: | :-------: | :------: | :-------------------------------------------------------: |
| OWL_SRV_PORT | 8460 | NO | Web Server Port | | OWL_SRV_PORT | 8460 | NO | Web Server Port |
| OWL_SRV_LISTEN | 0.0.0.0 | NO | Web Server Listen Address | | OWL_SRV_LISTEN | 0.0.0.0 | NO | Web Server Listen Address |
| OWL_DB_USER | owl | NO | Database Username | | OWL_DB_USER | owl | NO | Database Username |

128
app.js
View File

@ -4,41 +4,41 @@
// licensed separately, each folder contains a license file where a // licensed separately, each folder contains a license file where a
// different license applies. // different license applies.
console.log('Initialising OwlBoard'); console.log("Initialising OwlBoard");
const mode = process.env.NODE_ENV || 'development'; const mode = process.env.NODE_ENV || "development";
// External Requires // External Requires
const express = require('express'); const express = require("express");
const app = express(); const app = express();
// Middleware // Middleware
const compression = require('compression'); const compression = require("compression");
const rateLimit = require('express-rate-limit'); const rateLimit = require("express-rate-limit");
const cors = require('cors'); const cors = require("cors");
const authenticate= require('./src/middlewares/auth.middlewares'); const authenticate = require("./src/middlewares/auth.middlewares");
// Internal Requires // Internal Requires
const log = require('./src/utils/logs.utils'); const log = require("./src/utils/logs.utils");
const version = require('./src/configs/version.configs'); const version = require("./src/configs/version.configs");
const listRtr = require('./src/routes/list.routes'); const listRtr = require("./src/routes/list.routes");
const ldbRtr = require('./src/routes/ldb.routes'); const ldbRtr = require("./src/routes/ldb.routes");
const ldbsRtr = require('./src/routes/ldbs.routes'); const ldbsRtr = require("./src/routes/ldbs.routes");
const kubeRtr = require('./src/routes/kube.routes'); const kubeRtr = require("./src/routes/kube.routes");
const findRtr = require('./src/routes/find.routes'); const findRtr = require("./src/routes/find.routes");
const issueRtr = require('./src/routes/issue.routes'); const issueRtr = require("./src/routes/issue.routes");
const statRtr = require('./src/routes/stats.routes'); const statRtr = require("./src/routes/stats.routes");
const regRtr = require('./src/routes/registration.routes'); const regRtr = require("./src/routes/registration.routes");
const pisRtr = require('./src/routes/pis.routes'); const pisRtr = require("./src/routes/pis.routes");
const trainRtr = require('./src/routes/train.routes'); const trainRtr = require("./src/routes/train.routes");
const pis2Rtr = require('./src/routes/pis2.routes'); // API Version 2 Routes const pis2Rtr = require("./src/routes/pis2.routes"); // API Version 2 Routes
const ref2Rtr = require('./src/routes/ref2.routes'); // API Version 2 Routes const ref2Rtr = require("./src/routes/ref2.routes"); // API Version 2 Routes
const live2Rtr = require('./src/routes/live2.routes'); // API Version 2 Routes const live2Rtr = require("./src/routes/live2.routes"); // API Version 2 Routes
const tt2Rtr = require('./src/routes/timetable2.routes'); // API Version 2 const tt2Rtr = require("./src/routes/timetable2.routes"); // API Version 2
const user2Rtr = require('./src/routes/user2.routes'); // API Version 2 Routes const user2Rtr = require("./src/routes/user2.routes"); // API Version 2 Routes
const miscRtr = require('./src/routes/misc.routes'); // Non-Public API Routes const miscRtr = require("./src/routes/misc.routes"); // Non-Public API Routes
// Set Server Configurations // Set Server Configurations
const srvListen = process.env.OWL_SRV_LISTEN || '0.0.0.0'; const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0";
const srvPort = process.env.OWL_SRV_PORT || 8460; const srvPort = process.env.OWL_SRV_PORT || 8460;
const limiter = rateLimit({ const limiter = rateLimit({
@ -49,65 +49,75 @@ const limiter = rateLimit({
}); });
// Print version number: // Print version number:
log.out(`app: Starting OwlBoard in ${mode} mode`, 'init'); log.out(`app: Starting OwlBoard in ${mode} mode`, "init");
log.out(`app: Starting OwlBoard - Backend Version: ${version.app} - ` + log.out(
`API versions: ${version.api}`, 'init'); `app: Starting OwlBoard - Backend Version: ${version.app} - ` +
`API versions: ${version.api}`,
"init"
);
// Express Error Handling: // Express Error Handling:
app.use((err, req, res, next) => { app.use((err, req, res, next) => {
const statusCode = err.statuscode || 500; const statusCode = err.statuscode || 500;
console.error(err.message, err.stack); console.error(err.message, err.stack);
res.status(statusCode).json({'message': err.message}); res.status(statusCode).json({ message: err.message });
return; return;
}); });
// Global Middleware: // Global Middleware:
app.use(cors({ app.use(
origin: '*'//[/\.owlboard\.info$/, 'localhost:5173', 'localhost:4173'] cors({
})); origin: "*", //[/\.owlboard\.info$/, 'localhost:5173', 'localhost:4173']
})
);
app.use(express.json()); //JSON Parsing for POST Requests app.use(express.json()); //JSON Parsing for POST Requests
app.use(compression()); // Compress API Data if supported by client app.use(compression()); // Compress API Data if supported by client
app.use(limiter); app.use(limiter);
app.use(authenticate); app.use(authenticate);
// 2023 Rationalisation Routes (/api/v2, /misc) // 2023 Rationalisation Routes (/api/v2, /misc)
app.use('/api/v2/pis', pis2Rtr); // API Version 2 app.use("/api/v2/pis", pis2Rtr); // API Version 2
app.use('/api/v2/live', live2Rtr); // API Version 2 app.use("/api/v2/live", live2Rtr); // API Version 2
app.use('/api/v2/ref', ref2Rtr); // API Version 2 app.use("/api/v2/ref", ref2Rtr); // API Version 2
app.use('/api/v2/timetable', tt2Rtr); // API Version 2 app.use("/api/v2/timetable", tt2Rtr); // API Version 2
app.use('/api/v2/user', user2Rtr); // API Version 2 app.use("/api/v2/user", user2Rtr); // API Version 2
app.use('/misc', miscRtr); // Non public-api endpoints (Stats, Issue, etc.) app.use("/misc", miscRtr); // Non public-api endpoints (Stats, Issue, etc.)
// Unauthenticated Routes // Unauthenticated Routes
app.use('/api/v1/list', listRtr); app.use("/api/v1/list", listRtr);
app.use('/api/v1/ldb', ldbRtr); app.use("/api/v1/ldb", ldbRtr);
app.use('/api/v1/kube', kubeRtr); app.use("/api/v1/kube", kubeRtr);
app.use('/api/v1/find', findRtr); app.use("/api/v1/find", findRtr);
app.use('/api/v1/issue', issueRtr); app.use("/api/v1/issue", issueRtr);
app.use('/api/v1/stats', statRtr); app.use("/api/v1/stats", statRtr);
app.use('/api/v1/register', regRtr); app.use("/api/v1/register", regRtr);
// Authented Routes // Authented Routes
app.use('/api/v1/ldbs', authenticate, ldbsRtr); app.use("/api/v1/ldbs", authenticate, ldbsRtr);
app.use('/api/v1/pis', authenticate, pisRtr); app.use("/api/v1/pis", authenticate, pisRtr);
app.use('/api/v1/auth/test', authenticate, (req, res) => res.status(200).json({ app.use("/api/v1/auth/test", authenticate, (req, res) =>
status: 'ok', res.status(200).json({
message: 'Authentication successful' status: "ok",
})); // Returns 401 if auth failed, 200 if successful. message: "Authentication successful",
app.use('/api/v1/train', authenticate, trainRtr); })
); // Returns 401 if auth failed, 200 if successful.
app.use("/api/v1/train", authenticate, trainRtr);
// Number of proxies: // Number of proxies:
app.set('trust proxy', 4); app.set("trust proxy", 4);
mode === 'development' mode === "development"
? app.get('/api/v1/ip', (req, res) => res.send(req.ip)) ? app.get("/api/v1/ip", (req, res) => res.send(req.ip))
: null; : null;
// Start Express // Start Express
app.listen(srvPort, srvListen, (error) => { app.listen(srvPort, srvListen, (error) => {
if (!error) { if (!error) {
log.out(`app.listen: Listening on http://${srvListen}:${srvPort}`, 'init'); log.out(`app.listen: Listening on http://${srvListen}:${srvPort}`, "init");
log.out('app.listen: State - alive', 'init'); log.out("app.listen: State - alive", "init");
} else { } else {
log.out(`app.listen: Error occurred, server can't start ${error.message}`, 'err'); log.out(
`app.listen: Error occurred, server can't start ${error.message}`,
"err"
);
} }
}); });

View File

@ -1,5 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */ /** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = { module.exports = {
preset: 'ts-jest', preset: "ts-jest",
testEnvironment: 'node', testEnvironment: "node",
}; };

View File

@ -1,7 +1,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<title>OwlBoard - Register</title> <title>OwlBoard - Register</title>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1" />
<style> <style>
html { html {
background-color: #404c55; background-color: #404c55;
@ -39,32 +39,45 @@
</style> </style>
</head> </head>
<body> <body>
<br><br> <br /><br />
<table> <table>
<tr> <tr>
<td> <td>
<img src="https://owlboard.info/images/logo/wide_logo.svg" id="title"> <img
src="https://owlboard.info/images/logo/wide_logo.svg"
id="title"
/>
</td> </td>
</tr> </tr>
<tr> <tr>
<td> <td>
<h1>Register for OwlBoard</h1> <h1>Register for OwlBoard</h1>
<br> <br />
<p>Use the link below to register for OwlBoard (Staff Version)</p> <p>Use the link below to register for OwlBoard (Staff Version)</p>
<br> <br />
<a href="https://owlboard.info/more/reg/submit?key=>>ACCESSCODE<<" id="button">Register</a> <a
<br><br><br> href="https://owlboard.info/more/reg/submit?key=>>ACCESSCODE<<"
<p>Alternatively copy and paste the link:<br>https://owlboard.info/more/reg/submit?key=>>ACCESSCODE<<</p> id="button"
<p>This registration is for one device only, you can register again using the >Register</a
same email address for other devices and access OwlBoard from elsewhere. >
<br /><br /><br />
<p>
Alternatively copy and paste the link:<br />https://owlboard.info/more/reg/submit?key=>>ACCESSCODE<<
</p> </p>
<p>If you did not request to sign up to OwlBoard (Staff Version), you can <p>
safely ignore this email. Your email address has not been stored by us. This registration is for one device only, you can register again
using the same email address for other devices and access OwlBoard
from elsewhere.
</p>
<p>
If you did not request to sign up to OwlBoard (Staff Version), you
can safely ignore this email. Your email address has not been stored
by us.
</p> </p>
<p>The registration link will expire after 30 minutes.</p> <p>The registration link will expire after 30 minutes.</p>
</td> </td>
</tr> </tr>
</table> </table>
<br> <br />
</body> </body>
</html> </html>

View File

@ -1,37 +1,37 @@
const valid: string[] = [ const valid: string[] = [
'owlboard.info', "owlboard.info",
'avantiwestcoast.co.uk', "avantiwestcoast.co.uk",
'btp.police.uk', "btp.police.uk",
'c2crail.net', "c2crail.net",
'chilternrailways.co.uk', "chilternrailways.co.uk",
'crosscountrytrains.co.uk', "crosscountrytrains.co.uk",
'eastmidlandsrailway.co.uk', "eastmidlandsrailway.co.uk",
'abellio.co.uk', "abellio.co.uk",
'tfl.gov.uk', "tfl.gov.uk",
'mtrel.co.uk', "mtrel.co.uk",
'eurostar.com', "eurostar.com",
'eurotunnel.com', "eurotunnel.com",
'ffwhr.com', "ffwhr.com",
'gwr.com', "gwr.com",
'hitachirail-eu.com', "hitachirail-eu.com",
'greateranglia.co.uk', "greateranglia.co.uk",
'heathrow.com', "heathrow.com",
'swrailway.com', "swrailway.com",
'lsltoc.co.uk', "lsltoc.co.uk",
'lner.co.uk', "lner.co.uk",
'arrivarl.co.uk', "arrivarl.co.uk",
'tube.tfl.gov.uk', "tube.tfl.gov.uk",
'lumo.co.uk', "lumo.co.uk",
'merseyrail.org', "merseyrail.org",
'nrcommcentre.com', "nrcommcentre.com",
'networkrail.co.uk', "networkrail.co.uk",
'northernrailway.co.uk', "northernrailway.co.uk",
'scotrail.co.uk', "scotrail.co.uk",
'southeasternrailway.co.uk', "southeasternrailway.co.uk",
'tpeexpress.co.uk', "tpeexpress.co.uk",
'tfwrail.wales', "tfwrail.wales",
'wmtrains.co.uk', "wmtrains.co.uk",
]; ];
module.exports = valid; module.exports = valid;
export { valid } export { valid };

View File

@ -1,18 +1,18 @@
// statusCodes should be a map, not an object // statusCodes should be a map, not an object
const statusCodes = { const statusCodes = {
400: 'data not found', 400: "data not found",
700: 'no authentication attempt', 700: "no authentication attempt",
701: 'invalid credentials', 701: "invalid credentials",
702: 'domain not whitelisted', 702: "domain not whitelisted",
703: 'registration request not found, maybe expired', 703: "registration request not found, maybe expired",
800: 'location code not found', 800: "location code not found",
801: 'unable to fetch location data', 801: "unable to fetch location data",
900: 'invalid request format', 900: "invalid request format",
901: 'email not provided', 901: "email not provided",
950: 'upstream server error', 950: "upstream server error",
951: 'unknown server error' 951: "unknown server error",
}; };
module.exports = statusCodes; module.exports = statusCodes;
export { statusCodes } export { statusCodes };

View File

@ -1,12 +1,12 @@
interface versions { interface versions {
api: string[], api: string[];
app: string app: string;
} }
const version: versions = { const version: versions = {
api: ['/api/v1/','/api/v2'], api: ["/api/v1/", "/api/v2"],
app: '2023.8.2' app: "2023.8.2",
}; };
module.exports = version; module.exports = version;
export { version } export { version };

View File

@ -1,11 +1,11 @@
const find = require('../services/find.services'); const find = require("../services/find.services");
async function findName(req, res, next) { async function findName(req, res, next) {
try { try {
var id = req.params.id; var id = req.params.id;
res.json(await find.name(id)); res.json(await find.name(id));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
next(err); next(err);
} }
} }
@ -15,7 +15,7 @@ async function findCrs(req, res, next){
var id = req.params.id; var id = req.params.id;
res.json(await find.crs(id)); res.json(await find.crs(id));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
next(err); next(err);
} }
} }
@ -25,7 +25,7 @@ async function findNlc(req, res, next){
var id = req.params.id; var id = req.params.id;
res.json(await find.nlc(id)); res.json(await find.nlc(id));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
next(err); next(err);
} }
} }
@ -35,7 +35,7 @@ async function findTiploc(req, res, next){
var id = req.params.id; var id = req.params.id;
res.json(await find.tiploc(id)); res.json(await find.tiploc(id));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
next(err); next(err);
} }
} }
@ -45,7 +45,7 @@ async function findStanox(req, res, next){
var id = req.params.id; var id = req.params.id;
res.json(await find.stanox(id)); res.json(await find.stanox(id));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
next(err); next(err);
} }
} }
@ -54,5 +54,5 @@ module.exports = {
findCrs, findCrs,
findNlc, findNlc,
findTiploc, findTiploc,
findStanox findStanox,
}; };

View File

@ -1,16 +1,16 @@
const issue = require('../services/issue.services'); const issue = require("../services/issue.services");
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
async function post(req, res, next) { async function post(req, res, next) {
try { try {
log.out(`issueControllers.post: Request Body: ${JSON.stringify(req.body)}`); log.out(`issueControllers.post: Request Body: ${JSON.stringify(req.body)}`);
res.json(await issue.processor(req.body)); res.json(await issue.processor(req.body));
} catch (err) { } catch (err) {
console.error('Controller Error', err.message); console.error("Controller Error", err.message);
next(err); next(err);
} }
} }
module.exports = { module.exports = {
post post,
}; };

View File

@ -1,11 +1,11 @@
const kube = require('../services/kube.services'); const kube = require("../services/kube.services");
async function getAlive(req, res, next) { async function getAlive(req, res, next) {
try { try {
var state = kube.getAlive(); var state = kube.getAlive();
res.status((await state).code).send((await state).state); res.status((await state).code).send((await state).state);
} catch (err) { } catch (err) {
res.status('503').send({state: 'error'}); res.status("503").send({ state: "error" });
} }
} }
@ -13,7 +13,7 @@ async function getReady(req, res, next){
try { try {
res.json(await kube.getReady(req.body)); res.json(await kube.getReady(req.body));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
next(err); next(err);
} }
} }
@ -22,7 +22,7 @@ async function getTime(req, res, next){
try { try {
res.json(await kube.getTime(req.body)); res.json(await kube.getTime(req.body));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
err.status = 503; err.status = 503;
next(err); next(err);
} }
@ -31,5 +31,5 @@ async function getTime(req, res, next){
module.exports = { module.exports = {
getAlive, getAlive,
getReady, getReady,
getTime getTime,
}; };

View File

@ -1,19 +1,21 @@
const ldb = require('../services/ldb.services'); const ldb = require("../services/ldb.services");
async function get(req, res, next){ // API v1 only async function get(req, res, next) {
// API v1 only
try { try {
var id = req.params.id; var id = req.params.id;
res.json(await ldb.get(id)); res.json(await ldb.get(id));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
} }
async function getTrain(req, res, next) { // API v2 Only async function getTrain(req, res, next) {
// API v2 Only
if (!req.isAuthed) { if (!req.isAuthed) {
const err = new Error('Unauthorized'); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
throw err; throw err;
} }
@ -21,31 +23,34 @@ async function getTrain(req, res, next) { // API v2 Only
let id = req.params.id; let id = req.params.id;
try { try {
switch (type.toLowerCase()) { switch (type.toLowerCase()) {
case 'rid': case "rid":
res.json(await ldb.getServiceByRID(id)); res.json(await ldb.getServiceByRID(id));
break; break;
case 'uid': case "uid":
case 'headcode': case "headcode":
case 'rsid': case "rsid":
res.json(await ldb.getServicesByOther(id)); res.json(await ldb.getServicesByOther(id));
break; break;
default: default:
res.status(400).json({status: 'error', message:'Invalid search type'}); res
.status(400)
.json({ status: "error", message: "Invalid search type" });
} }
} catch (err) { } catch (err) {
err.status = 500; err.status = 500;
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
next(err); next(err);
} }
} }
async function getStation(req, res, next) { // API v2 Only async function getStation(req, res, next) {
// API v2 Only
let type = req.params.type; let type = req.params.type;
let id = req.params.id; let id = req.params.id;
try { try {
if (type == 'staff') { if (type == "staff") {
if (!req.isAuthed) { if (!req.isAuthed) {
const err = new Error('Unauthorized'); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
throw err; throw err;
} }
@ -54,7 +59,7 @@ async function getStation(req, res, next) { // API v2 Only
res.json(await ldb.get(id, false)); res.json(await ldb.get(id, false));
} }
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -63,5 +68,5 @@ async function getStation(req, res, next) { // API v2 Only
module.exports = { module.exports = {
get, get,
getTrain, getTrain,
getStation getStation,
}; };

View File

@ -1,11 +1,11 @@
const ldb = require('../services/ldb.services'); const ldb = require("../services/ldb.services");
async function get(req, res, next) { async function get(req, res, next) {
try { try {
var id = req.params.id; var id = req.params.id;
res.json(await ldb.get(id, true)); res.json(await ldb.get(id, true));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -15,7 +15,7 @@ async function getReasonCodeList(req, res, next) {
try { try {
res.json(await ldb.getReasonCodeList()); res.json(await ldb.getReasonCodeList());
} catch (err) { } catch (err) {
console.error('ERROR', err.message); console.error("ERROR", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -26,7 +26,7 @@ async function getReasonCode(req, res, next) {
const code = req.params.code; const code = req.params.code;
res.json(await ldb.getReasonCode(code)); res.json(await ldb.getReasonCode(code));
} catch (err) { } catch (err) {
console.error('ERROR', err.message); console.error("ERROR", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -37,7 +37,7 @@ async function getTrainByRID(req, res, next) {
const rid = req.params.rid; const rid = req.params.rid;
res.json(await ldb.getServiceByRID(rid)); res.json(await ldb.getServiceByRID(rid));
} catch (err) { } catch (err) {
console.error('ERROR', err); console.error("ERROR", err);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -47,5 +47,5 @@ module.exports = {
get, get,
getReasonCodeList, getReasonCodeList,
getReasonCode, getReasonCode,
getTrainByRID getTrainByRID,
}; };

View File

@ -1,10 +1,10 @@
const list = require('../services/list.services'); const list = require("../services/list.services");
async function getStations(req, res, next) { async function getStations(req, res, next) {
try { try {
res.json(await list.getStations(req.body)); res.json(await list.getStations(req.body));
} catch (err) { } catch (err) {
console.error('Controller Error', err.message); console.error("Controller Error", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -14,7 +14,7 @@ async function getCorpus(req, res, next){
try { try {
res.json(await list.getCorpus(req.body)); res.json(await list.getCorpus(req.body));
} catch (err) { } catch (err) {
console.error('Controller Error', err.message); console.error("Controller Error", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -24,7 +24,7 @@ async function hits(req, res, next) {
try { try {
res.json(await list.hits()); res.json(await list.hits());
} catch (err) { } catch (err) {
console.error('Controller Error', err); console.error("Controller Error", err);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -33,5 +33,5 @@ async function hits(req, res, next) {
module.exports = { module.exports = {
getStations, getStations,
getCorpus, getCorpus,
hits hits,
}; };

View File

@ -1,8 +1,8 @@
const pis = require('../services/pis.services'); const pis = require("../services/pis.services");
async function byOrigDest(req, res, next) { async function byOrigDest(req, res, next) {
if (!req.isAuthed) { if (!req.isAuthed) {
const err = new Error('Unauthorized'); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
throw err; throw err;
} }
@ -11,7 +11,7 @@ async function byOrigDest(req, res, next){
let end = req.params.end; let end = req.params.end;
res.json(await pis.findPisByOrigDest(start, end)); res.json(await pis.findPisByOrigDest(start, end));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
next(err); next(err);
} }
} }
@ -19,7 +19,7 @@ async function byOrigDest(req, res, next){
/* Used in /api/v2 */ /* Used in /api/v2 */
async function byStartEndCRS(req, res, next) { async function byStartEndCRS(req, res, next) {
if (!req.isAuthed) { if (!req.isAuthed) {
const err = new Error('Unauthorized'); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
return next(err); return next(err);
} }
@ -28,7 +28,7 @@ async function byStartEndCRS(req, res, next){
let endCrs = req.params.endCrs; let endCrs = req.params.endCrs;
res.json(await pis.findPisByOrigDest(startCrs, endCrs)); res.json(await pis.findPisByOrigDest(startCrs, endCrs));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
return next(err); return next(err);
} }
} }
@ -36,7 +36,7 @@ async function byStartEndCRS(req, res, next){
/* Used in /api/v2 */ /* Used in /api/v2 */
async function byCode(req, res, next) { async function byCode(req, res, next) {
if (!req.isAuthed) { if (!req.isAuthed) {
const err = new Error('Unauthorized'); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
return next(err); return next(err);
} }
@ -44,29 +44,28 @@ async function byCode(req, res, next){
let code = req.params.code; let code = req.params.code;
res.json(await pis.findPisByCode(code)); res.json(await pis.findPisByCode(code));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
return next(err); return next(err);
} }
} }
async function random(req, res, next) { async function random(req, res, next) {
if (!req.isAuthed) { if (!req.isAuthed) {
const err = new Error('Unauthorized'); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
return next(err); return next(err);
} }
try { try {
res.json(await pis.findRandom()); res.json(await pis.findRandom());
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
return next(err); return next(err);
} }
} }
module.exports = { module.exports = {
byOrigDest, byOrigDest,
byStartEndCRS, byStartEndCRS,
byCode, byCode,
random random,
}; };

View File

@ -1,19 +1,19 @@
/* API V2 Exclusive Controller */ /* API V2 Exclusive Controller */
const ldb = require('../services/ldb.services'); const ldb = require("../services/ldb.services");
const find = require('../services/find.services'); const find = require("../services/find.services");
async function getReasonCode(req, res, next) { async function getReasonCode(req, res, next) {
try { try {
const code = req.params.code; const code = req.params.code;
if (code === 'all') { if (code === "all") {
res.json(await ldb.getReasonCodeList()); res.json(await ldb.getReasonCodeList());
next; next;
} }
res.json(await ldb.getReasonCode(code)); res.json(await ldb.getReasonCode(code));
next; next;
} catch (err) { } catch (err) {
console.error('ERROR', err.message); console.error("ERROR", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -24,25 +24,25 @@ async function getLocationReference(req, res, next) {
const searchType = req.params.searchType; const searchType = req.params.searchType;
const id = req.params.id; const id = req.params.id;
switch (searchType) { switch (searchType) {
case 'name': case "name":
res.json(await find.name(id)); res.json(await find.name(id));
break; break;
case 'crs': // Same as 3alpha case "crs": // Same as 3alpha
case '3alpha': case "3alpha":
res.json(await find.crs(id)); res.json(await find.crs(id));
break; break;
case 'nlc': case "nlc":
res.json(await find.nlc(id)); res.json(await find.nlc(id));
break; break;
case 'tiploc': case "tiploc":
res.json(await find.tiploc(id)); res.json(await find.tiploc(id));
break; break;
case 'stanox': case "stanox":
res.json(await find.stanox(id)); res.json(await find.stanox(id));
break; break;
} }
} catch (err) { } catch (err) {
console.error('ERROR', err.message); console.error("ERROR", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -50,5 +50,5 @@ async function getLocationReference(req, res, next) {
module.exports = { module.exports = {
getReasonCode, getReasonCode,
getLocationReference getLocationReference,
}; };

View File

@ -1,11 +1,11 @@
const reg = require('../services/registration.services'); const reg = require("../services/registration.services");
async function register(req, res, next) { async function register(req, res, next) {
try { try {
let response = await reg.regUser(req.body); let response = await reg.regUser(req.body);
res.status(response.status).json(response); res.status(response.status).json(response);
} catch (err) { } catch (err) {
console.error('Controller Error', err.message); console.error("Controller Error", err.message);
next(err); next(err);
} }
} }
@ -37,15 +37,16 @@ async function getUser(req, res, next) {
async function checkAuth(req, res, next) { async function checkAuth(req, res, next) {
if (!req.isAuthed) { if (!req.isAuthed) {
res.status(401).body("Not Authorised") res.status(401).body("Not Authorised");
} else { } else {
res.status(200).body("Authorised Successfully") res.status(200).body("Authorised Successfully");
}
next();
} }
next()}
module.exports = { module.exports = {
register, register,
request, request,
getUser, getUser,
checkAuth checkAuth,
}; };

View File

@ -1,30 +1,33 @@
const stat = require('../services/stats.services'); const stat = require("../services/stats.services");
async function get(req, res, next) { // API V1 async function get(req, res, next) {
// API V1
try { try {
res.json(await stat.hits()); res.json(await stat.hits());
} catch (err) { } catch (err) {
console.error('Controller Error', err); console.error("Controller Error", err);
err.status = 500; err.status = 500;
next(err); next(err);
} }
} }
async function versions(req, res, next) { // API v2 async function versions(req, res, next) {
// API v2
try { try {
res.json(await stat.getVersions()); res.json(await stat.getVersions());
} catch (err) { } catch (err) {
console.error('Controller Error', err); console.error("Controller Error", err);
err.status = 500; err.status = 500;
next(err); next(err);
} }
} }
async function statistics(req, res, next) { // Api v2 async function statistics(req, res, next) {
// Api v2
try { try {
res.json(await stat.statistics()); res.json(await stat.statistics());
} catch (err) { } catch (err) {
console.error('Controller Error', err); console.error("Controller Error", err);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -33,5 +36,5 @@ async function statistics(req, res, next) { // Api v2
module.exports = { module.exports = {
get, get,
versions, versions,
statistics statistics,
}; };

View File

@ -1,16 +1,16 @@
const train = require('../services/trainService.services'); const train = require("../services/trainService.services");
async function getByHeadcodeToday(req, res, next) { async function getByHeadcodeToday(req, res, next) {
if (!req.isAuthed) { if (!req.isAuthed) {
const err = new Error('Unauthorized'); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
next(err) next(err);
} }
try { try {
var searchHeadcode = req.params.id; var searchHeadcode = req.params.id;
res.json(await train.findByHeadcodeToday(searchHeadcode)); res.json(await train.findByHeadcodeToday(searchHeadcode));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error("Unknown Error", err.message);
err.status = 500; err.status = 500;
next(err); next(err);
} }
@ -18,7 +18,7 @@ async function getByHeadcodeToday(req, res, next){
async function get(req, res, next) { async function get(req, res, next) {
if (!req.isAuthed) { if (!req.isAuthed) {
const err = new Error('Unauthorized'); const err = new Error("Unauthorized");
err.status = 401; err.status = 401;
next(err); next(err);
} }
@ -27,18 +27,17 @@ async function get(req, res, next) {
let id = req.params.id; let id = req.params.id;
try { try {
switch (searchType) { switch (searchType) {
case 'headcode': case "headcode":
res.json(await train.findByHeadcode(date, id)); res.json(await train.findByHeadcode(date, id));
break; break;
case 'byTrainUid': case "byTrainUid":
res.json(await train.findByTrainUid(id, date)); res.json(await train.findByTrainUid(id, date));
break; break;
default: default:
res.status(404).json({ res.status(404).json({
status:'error', status: "error",
message:`Invalid search type "${searchType}"` message: `Invalid search type "${searchType}"`,
} });
);
} }
} catch (err) { } catch (err) {
console.error(err.message); console.error(err.message);
@ -49,5 +48,5 @@ async function get(req, res, next) {
module.exports = { module.exports = {
getByHeadcodeToday, getByHeadcodeToday,
get get,
}; };

View File

@ -1,24 +1,23 @@
const utils = require('../utils/auth.utils'); const utils = require("../utils/auth.utils");
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
module.exports = async function authCheck(req, res, next) { module.exports = async function authCheck(req, res, next) {
log.out('authMiddlewares: Checking authentication', 'dbug'); log.out("authMiddlewares: Checking authentication", "dbug");
try { try {
var uuid = req.headers.uuid; var uuid = req.headers.uuid;
} catch (err) { } catch (err) {
log.out('authMiddlewares: User !isAuthed', 'dbug'); log.out("authMiddlewares: User !isAuthed", "dbug");
req.isAuthed = false; req.isAuthed = false;
return next(); return next();
} }
try { try {
var result = await utils.isAuthed(uuid) || false; var result = (await utils.isAuthed(uuid)) || false;
if (!result) { if (!result) {
req.isAuthed = false; req.isAuthed = false;
log.out('authMiddlewares: User !isAuthed', log.out("authMiddlewares: User !isAuthed", "warn");
'warn');
} else { } else {
req.isAuthed = true; req.isAuthed = true;
log.out('authMiddlewares: User isAuthed', 'dbug'); log.out("authMiddlewares: User isAuthed", "dbug");
} }
return next(); return next();
} catch (err) { } catch (err) {

View File

@ -1,9 +1,9 @@
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
module.exports = async function requireJson(req, res, next) { module.exports = async function requireJson(req, res, next) {
if (req.headers['content-type'] !== 'application/json') { if (req.headers["content-type"] !== "application/json") {
log.out('requireJson.middlewares: Bad Request: Not in JSON format'); log.out("requireJson.middlewares: Bad Request: Not in JSON format");
res.status(400).send({status: 400, message: 'Server requires JSON'}); res.status(400).send({ status: 400, message: "Server requires JSON" });
} else { } else {
next(); next();
} }

View File

@ -1,9 +1,8 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
// Controller Imports // Controller Imports
// Routes // Routes
module.exports = router; module.exports = router;

View File

@ -1,4 +1,4 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
/* Controller Imports */ /* Controller Imports */
@ -7,8 +7,6 @@ const router = express.Router();
// Timetable // Timetable
// Ref // Ref
// User // User
@ -16,17 +14,15 @@ const router = express.Router();
/* Routes */ /* Routes */
// Live // Live
router.get('/live/station/:id/:type'); router.get("/live/station/:id/:type");
router.get('/live/train/:searchType/:id'); router.get("/live/train/:searchType/:id");
// Timetable // Timetable
router.get('/timetable/train/:date/:searchType/:id'); router.get("/timetable/train/:date/:searchType/:id");
// User // User
router.post('/user'); router.post("/user");
router.get('/user/:uuid'); router.get("/user/:uuid");
router.delete('/user/:uuid'); router.delete("/user/:uuid");
module.exports = router; module.exports = router;

View File

@ -1,6 +1,6 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const findController = require('../controllers/find.controllers'); const findController = require("../controllers/find.controllers");
/* GET programming languages. */ /* GET programming languages. */
//router.get('/', programmingLanguagesController.get); //router.get('/', programmingLanguagesController.get);
@ -14,10 +14,10 @@ const findController = require('../controllers/find.controllers');
/* DELETE programming language */ /* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove); //router.delete('/:id', programmingLanguagesController.remove);
router.get('/name/:id', findController.findName); router.get("/name/:id", findController.findName);
router.get('/crs/:id', findController.findCrs); router.get("/crs/:id", findController.findCrs);
router.get('/nlc/:id', findController.findNlc); router.get("/nlc/:id", findController.findNlc);
router.get('/tiploc/:id', findController.findTiploc); router.get("/tiploc/:id", findController.findTiploc);
router.get('/stanox/:id', findController.findStanox); router.get("/stanox/:id", findController.findStanox);
module.exports = router; module.exports = router;

View File

@ -1,7 +1,7 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const issueController = require('../controllers/issue.controllers'); const issueController = require("../controllers/issue.controllers");
router.post('/', issueController.post); router.post("/", issueController.post);
module.exports = router; module.exports = router;

View File

@ -1,9 +1,9 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const kubeController = require('../controllers/kube.controllers'); const kubeController = require("../controllers/kube.controllers");
router.get('/alive', kubeController.getAlive); router.get("/alive", kubeController.getAlive);
router.get('/ready', kubeController.getReady); router.get("/ready", kubeController.getReady);
router.get('/time', kubeController.getTime); router.get("/time", kubeController.getTime);
module.exports = router; module.exports = router;

View File

@ -1,6 +1,6 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const ldbController = require('../controllers/ldb.controllers'); const ldbController = require("../controllers/ldb.controllers");
/* GET programming languages. */ /* GET programming languages. */
//router.get('/', programmingLanguagesController.get); //router.get('/', programmingLanguagesController.get);
@ -14,6 +14,6 @@ const ldbController = require('../controllers/ldb.controllers');
/* DELETE programming language */ /* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove); //router.delete('/:id', programmingLanguagesController.remove);
router.get('/:id', ldbController.get); router.get("/:id", ldbController.get);
module.exports = router; module.exports = router;

View File

@ -1,6 +1,6 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const ldbsController = require('../controllers/ldbs.controllers'); const ldbsController = require("../controllers/ldbs.controllers");
/* GET programming languages. */ /* GET programming languages. */
//router.get('/', programmingLanguagesController.get); //router.get('/', programmingLanguagesController.get);
@ -14,9 +14,9 @@ const ldbsController = require('../controllers/ldbs.controllers');
/* DELETE programming language */ /* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove); //router.delete('/:id', programmingLanguagesController.remove);
router.get('/arrdep/:id', ldbsController.get); router.get("/arrdep/:id", ldbsController.get);
router.get('/reasonCode', ldbsController.getReasonCodeList); router.get("/reasonCode", ldbsController.getReasonCodeList);
router.get('/reasonCode/:code', ldbsController.getReasonCode); router.get("/reasonCode/:code", ldbsController.getReasonCode);
router.get('/service/rid/:rid', ldbsController.getTrainByRID); router.get("/service/rid/:rid", ldbsController.getTrainByRID);
module.exports = router; module.exports = router;

View File

@ -1,6 +1,6 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const listController = require('../controllers/list.controllers'); const listController = require("../controllers/list.controllers");
/* GET programming languages. */ /* GET programming languages. */
//router.get('/', programmingLanguagesController.get); //router.get('/', programmingLanguagesController.get);
@ -14,7 +14,7 @@ const listController = require('../controllers/list.controllers');
/* DELETE programming language */ /* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove); //router.delete('/:id', programmingLanguagesController.remove);
router.get('/stations', listController.getStations); router.get("/stations", listController.getStations);
router.get('/corpus', listController.getCorpus); router.get("/corpus", listController.getCorpus);
module.exports = router; module.exports = router;

View File

@ -1,9 +1,9 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const ldbCtr = require('../controllers/ldb.controllers'); const ldbCtr = require("../controllers/ldb.controllers");
// PIS // PIS
router.get('/station/:id/:type', ldbCtr.getStation); router.get("/station/:id/:type", ldbCtr.getStation);
router.get('/train/:searchType/:id', ldbCtr.getTrain); router.get("/train/:searchType/:id", ldbCtr.getTrain);
module.exports = router; module.exports = router;

View File

@ -1,16 +1,15 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
// Controller Imports // Controller Imports
const issueCtr = require('../controllers/issue.controllers'); const issueCtr = require("../controllers/issue.controllers");
const statCtr = require('../controllers/stats.controllers'); const statCtr = require("../controllers/stats.controllers");
// Routes // Routes
router.get('/server/stats', statCtr.statistics); router.get("/server/stats", statCtr.statistics);
router.get('/server/versions', statCtr.versions); router.get("/server/versions", statCtr.versions);
router.post('/issue', issueCtr.post); router.post("/issue", issueCtr.post);
module.exports = router; module.exports = router;

View File

@ -1,9 +1,9 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const pisController = require('../controllers/pis.controllers'); const pisController = require("../controllers/pis.controllers");
router.get('/origdest/:start/:end', pisController.byOrigDest); router.get("/origdest/:start/:end", pisController.byOrigDest);
router.get('/code/:code', pisController.byCode); router.get("/code/:code", pisController.byCode);
router.get('/code/random', pisController.random); router.get("/code/random", pisController.random);
module.exports = router; module.exports = router;

View File

@ -1,9 +1,9 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const pisCtr = require('../controllers/pis.controllers'); const pisCtr = require("../controllers/pis.controllers");
// PIS // PIS
router.get('/byCode/:code', pisCtr.byCode); router.get("/byCode/:code", pisCtr.byCode);
router.get('/byStartEnd/:startCrs/:endCrs', pisCtr.byStartEndCRS); router.get("/byStartEnd/:startCrs/:endCrs", pisCtr.byStartEndCRS);
module.exports = router; module.exports = router;

View File

@ -1,9 +1,9 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const refCtr = require('../controllers/ref.controllers'); const refCtr = require("../controllers/ref.controllers");
// Ref // Ref
router.get('/reasonCode/:code', refCtr.getReasonCode); router.get("/reasonCode/:code", refCtr.getReasonCode);
router.get('/locationCode/:searchType/:id', refCtr.getLocationReference); router.get("/locationCode/:searchType/:id", refCtr.getLocationReference);
module.exports = router; module.exports = router;

View File

@ -1,8 +1,8 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const regController = require('../controllers/registration.controllers'); const regController = require("../controllers/registration.controllers");
router.post('/request', regController.request); router.post("/request", regController.request);
router.post('/register', regController.register); router.post("/register", regController.register);
module.exports = router; module.exports = router;

View File

@ -1,8 +1,7 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const statsController = require('../controllers/stats.controllers'); const statsController = require("../controllers/stats.controllers");
router.get("/", statsController.get);
router.get('/', statsController.get);
module.exports = router; module.exports = router;

View File

@ -1,8 +1,8 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const ttCtr = require('../controllers/train.controllers'); const ttCtr = require("../controllers/train.controllers");
// PIS // PIS
router.get('/train/:date/:searchType/:id', ttCtr.get); router.get("/train/:date/:searchType/:id", ttCtr.get);
module.exports = router; module.exports = router;

View File

@ -1,6 +1,6 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const trainController = require('../controllers/train.controllers'); const trainController = require("../controllers/train.controllers");
/* GET programming languages. */ /* GET programming languages. */
//router.get('/', programmingLanguagesController.get); //router.get('/', programmingLanguagesController.get);
@ -14,6 +14,6 @@ const trainController = require('../controllers/train.controllers');
/* DELETE programming language */ /* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove); //router.delete('/:id', programmingLanguagesController.remove);
router.get('/headcode/today/:id', trainController.getByHeadcodeToday); router.get("/headcode/today/:id", trainController.getByHeadcodeToday);
module.exports = router; module.exports = router;

View File

@ -1,12 +1,12 @@
const express = require('express'); const express = require("express");
const router = express.Router(); const router = express.Router();
const regCtr = require('../controllers/registration.controllers'); const regCtr = require("../controllers/registration.controllers");
// User // User
router.get('/:uuid', regCtr.getUser); router.get("/:uuid", regCtr.getUser);
router.get('/checkAuth', regCtr.checkAuth); router.get("/checkAuth", regCtr.checkAuth);
// Not Implemented router.delete('/:uuid', regCtr.deleteUser); // Not Implemented router.delete('/:uuid', regCtr.deleteUser);
router.post('/request', regCtr.request); router.post("/request", regCtr.request);
router.post('/register', regCtr.register); router.post("/register", regCtr.register);
module.exports = router; module.exports = router;

View File

@ -1,63 +1,72 @@
/* global process */ /* global process */
const log = require('../utils/logs.utils'); // Log Helper const log = require("../utils/logs.utils"); // Log Helper
const dbUser = process.env.OWL_DB_USER || 'owl'; const dbUser = process.env.OWL_DB_USER || "owl";
const dbPass = process.env.OWL_DB_PASS || 'twittwoo'; const dbPass = process.env.OWL_DB_PASS || "twittwoo";
const dbName = process.env.OWL_DB_NAME || 'owlboard'; const dbName = process.env.OWL_DB_NAME || "owlboard";
const dbPort = process.env.OWL_DB_PORT || 27017; const dbPort = process.env.OWL_DB_PORT || 27017;
const dbHost = process.env.OWL_DB_HOST || 'localhost'; const dbHost = process.env.OWL_DB_HOST || "localhost";
const uri = `mongodb://${dbUser}:${dbPass}@${dbHost}:${dbPort}`; const uri = `mongodb://${dbUser}:${dbPass}@${dbHost}:${dbPort}`;
const { MongoClient } = require('mongodb'); const { MongoClient } = require("mongodb");
const client = new MongoClient(uri); const client = new MongoClient(uri);
const db = client.db(dbName); const db = client.db(dbName);
async function query(collection, query, returnId = false) { async function query(collection, query, returnId = false) {
await client.connect(); await client.connect();
log.out(`dbAccess.query: Connecting to collection: '${collection}'`, 'info'); log.out(`dbAccess.query: Connecting to collection: '${collection}'`, "info");
var qcoll = db.collection(collection); var qcoll = db.collection(collection);
var qcursor = qcoll.find(query); var qcursor = qcoll.find(query);
if (!returnId) { if (!returnId) {
qcursor.project({ _id: 0 }); qcursor.project({ _id: 0 });
} }
log.out(`dbAccess.query: Running Query: ${JSON.stringify(query)}`, 'info'); log.out(`dbAccess.query: Running Query: ${JSON.stringify(query)}`, "info");
increment(collection); increment(collection);
return (await qcursor.toArray()); return await qcursor.toArray();
} }
async function queryProject(collection, query, projection) { async function queryProject(collection, query, projection) {
await client.connect(); await client.connect();
log.out(`dbAccess.queryProject: Connecting to col: '${collection}'`, 'info'); log.out(`dbAccess.queryProject: Connecting to col: '${collection}'`, "info");
const qcoll = db.collection(collection); const qcoll = db.collection(collection);
const qcursor = qcoll.find(query).project(projection); const qcursor = qcoll.find(query).project(projection);
log.out(`dbAccess.query: Running Query: ${JSON.stringify(query)}, Projection: ${JSON.stringify(projection)}`, 'dbug'); log.out(
`dbAccess.query: Running Query: ${JSON.stringify(
query
)}, Projection: ${JSON.stringify(projection)}`,
"dbug"
);
increment(collection); increment(collection);
return await qcursor.toArray(); return await qcursor.toArray();
} }
async function queryAggregate(collection, pipeline) { async function queryAggregate(collection, pipeline) {
await client.connect(); await client.connect();
log.out(`dbAccess.queryProject: Connecting to col: '${collection}'`, 'info'); log.out(`dbAccess.queryProject: Connecting to col: '${collection}'`, "info");
log.out(`dbAccess.query: Running Aggregation: ${JSON.stringify(pipeline)}`, 'dbug'); log.out(
`dbAccess.query: Running Aggregation: ${JSON.stringify(pipeline)}`,
"dbug"
);
increment(collection); increment(collection);
return await db.collection(collection).aggregate(pipeline).toArray(); return await db.collection(collection).aggregate(pipeline).toArray();
} }
async function increment(target) { async function increment(target) {
log.out(`dbAccess.increment: Incrementing counter for: ${target}`, 'info'); log.out(`dbAccess.increment: Incrementing counter for: ${target}`, "info");
await client.connect(); await client.connect();
let col = db.collection('meta'); let col = db.collection("meta");
let update = {}; let update = {};
update[target] = 1; update[target] = 1;
col.updateOne({target: 'counters'}, {$inc:update}); col.updateOne({ target: "counters" }, { $inc: update });
} }
async function addUser(uuid, domain) { // Needs testing async function addUser(uuid, domain) {
log.out('dbAccess.addUser: Adding user to database'); // Needs testing
let doc = {uuid: uuid, domain: domain, atime: new Date}; log.out("dbAccess.addUser: Adding user to database");
let doc = { uuid: uuid, domain: domain, atime: new Date() };
await client.connect(); await client.connect();
let col = db.collection('users'); let col = db.collection("users");
let res = await col.insertOne(doc); let res = await col.insertOne(doc);
if (res.insertedId) { if (res.insertedId) {
return true; return true;
@ -65,29 +74,31 @@ async function addUser(uuid, domain) { // Needs testing
return false; return false;
} }
async function addRegReq(uuid, domain) { // Needs testing async function addRegReq(uuid, domain) {
log.out('dbAccess.addRegReq: Adding registration request'); // Needs testing
let doc = {uuid: uuid, time: new Date, domain: domain}; log.out("dbAccess.addRegReq: Adding registration request");
let doc = { uuid: uuid, time: new Date(), domain: domain };
await client.connect(); await client.connect();
let col = db.collection('registrations'); let col = db.collection("registrations");
let res = col.insertOne(doc); let res = col.insertOne(doc);
return res; return res;
} }
async function userAtime(uuid) { // Needs testing async function userAtime(uuid) {
log.out('dbAccess.userAtime: Updating access time for user'); // Needs testing
log.out("dbAccess.userAtime: Updating access time for user");
let q = { uuid: uuid }; let q = { uuid: uuid };
let n = {$set: {uuid: uuid, atime: new Date}}; let n = { $set: { uuid: uuid, atime: new Date() } };
await client.connect(); await client.connect();
let col = db.collection('users'); let col = db.collection("users");
let res = col.updateOne(q, n, { upsert: true }); let res = col.updateOne(q, n, { upsert: true });
return res; return res;
} }
// Deletes one single registration request entry from the DB // Deletes one single registration request entry from the DB
async function delRegReq(uuid) { async function delRegReq(uuid) {
log.out('dbAccess.delRegReq: Deleting a Registration Request'); log.out("dbAccess.delRegReq: Deleting a Registration Request");
let collection = 'registrations'; let collection = "registrations";
await client.connect(); await client.connect();
let col = db.collection(collection); let col = db.collection(collection);
col.deleteOne({ uuid: uuid }); col.deleteOne({ uuid: uuid });
@ -98,8 +109,10 @@ async function colCount(collection) {
await client.connect(); await client.connect();
let col = db.collection(collection); let col = db.collection(collection);
let count = col.countDocuments(); let count = col.countDocuments();
log.out(`dbAccess.colCount: Collection: ${collection} contains ${count}` + log.out(
' documents'); `dbAccess.colCount: Collection: ${collection} contains ${count}` +
" documents"
);
return await count; return await count;
} }
@ -112,5 +125,5 @@ module.exports = {
userAtime, userAtime,
addRegReq, addRegReq,
delRegReq, delRegReq,
colCount colCount,
}; };

View File

@ -1,16 +1,16 @@
// Parse and return a find request // Parse and return a find request
const log = require('../utils/logs.utils'); // Log Helper const log = require("../utils/logs.utils"); // Log Helper
const db = require('../services/dbAccess.services'); const db = require("../services/dbAccess.services");
const san = require('../utils/sanitizer.utils'); const san = require("../utils/sanitizer.utils");
// DB Query: query(collection, query) // DB Query: query(collection, query)
// Define collection as all queries are for the "corpus" collection. // Define collection as all queries are for the "corpus" collection.
const col = 'corpus'; const col = "corpus";
async function name(id) { async function name(id) {
log.out(`findServices.name: Finding station name: ${id}`, 'info'); log.out(`findServices.name: Finding station name: ${id}`, "info");
var name = san.cleanApiEndpointTxt(id.toUpperCase()); var name = san.cleanApiEndpointTxt(id.toUpperCase());
let query = { NLCDESC: name }; let query = { NLCDESC: name };
//var data = await db.query(col,query) //var data = await db.query(col,query)
@ -18,24 +18,24 @@ async function name(id){
} }
async function crs(id) { async function crs(id) {
log.out(`findServices.crs: Finding crs: ${id}`, 'info'); log.out(`findServices.crs: Finding crs: ${id}`, "info");
var crs = san.cleanApiEndpointTxt(id.toUpperCase()); var crs = san.cleanApiEndpointTxt(id.toUpperCase());
let query = {'3ALPHA': crs}; let query = { "3ALPHA": crs };
//var data = await db.query(col,query) //var data = await db.query(col,query)
return await db.query(col, query); return await db.query(col, query);
} }
async function nlc(id) { async function nlc(id) {
log.out(`findServices.nlc: Finding nlc: ${id}`, 'info'); log.out(`findServices.nlc: Finding nlc: ${id}`, "info");
var nlc = san.cleanApiEndpointNum(id); var nlc = san.cleanApiEndpointNum(id);
let query = { NLC: parseInt(nlc) }; let query = { NLC: parseInt(nlc) };
log.out(`findServices.nlc: NLC Converted to int: ${query}`, 'info'); log.out(`findServices.nlc: NLC Converted to int: ${query}`, "info");
//var data = await db.query(col,query) //var data = await db.query(col,query)
return await db.query(col, query); return await db.query(col, query);
} }
async function tiploc(id) { async function tiploc(id) {
log.out(`findServices.tiploc: Finding tiploc: ${id}`, 'info'); log.out(`findServices.tiploc: Finding tiploc: ${id}`, "info");
var tiploc = san.cleanApiEndpointTxt(id.toUpperCase()); var tiploc = san.cleanApiEndpointTxt(id.toUpperCase());
let query = { TIPLOC: tiploc }; let query = { TIPLOC: tiploc };
//var data = await db.query(col,query) //var data = await db.query(col,query)
@ -43,7 +43,7 @@ async function tiploc(id){
} }
async function stanox(id) { async function stanox(id) {
log.out(`findServices.stanox: Finding stanox: ${id}`, 'info'); log.out(`findServices.stanox: Finding stanox: ${id}`, "info");
var stanox = san.cleanApiEndpointNum(id); var stanox = san.cleanApiEndpointNum(id);
let query = { STANOX: String(stanox) }; let query = { STANOX: String(stanox) };
//var data = await db.query(col,query) //var data = await db.query(col,query)
@ -55,5 +55,5 @@ module.exports = {
crs, crs,
nlc, nlc,
tiploc, tiploc,
stanox stanox,
}; };

View File

@ -1,22 +1,22 @@
/* eslint-disable no-useless-escape */ /* eslint-disable no-useless-escape */
const axios = require('axios'); const axios = require("axios");
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
const issueLabels = { const issueLabels = {
bug: 120, bug: 120,
enhancement: 122, enhancement: 122,
question: 125, question: 125,
'user-support': 152, "user-support": 152,
'web-user': 153 "web-user": 153,
}; };
async function processor(data) { async function processor(data) {
log.out('issueService.processor: Issue received', 'info'); log.out("issueService.processor: Issue received", "info");
console.log(data); // TEMPORARY MEASURE console.log(data); // TEMPORARY MEASURE
let out = {}; let out = {};
out.labels = [(issueLabels[data?.label] || 0), issueLabels['web-user']]; out.labels = [issueLabels[data?.label] || 0, issueLabels["web-user"]];
out.title = data?.subject.replace(/<[^>]+>|[\*\$]/g, ''); out.title = data?.subject.replace(/<[^>]+>|[\*\$]/g, "");
out.body = data?.msg.replace(/<[^>]+>|[\*\$]/g, ''); out.body = data?.msg.replace(/<[^>]+>|[\*\$]/g, "");
return await sendToGitea(out); return await sendToGitea(out);
} }
@ -25,21 +25,21 @@ async function sendToGitea(body) {
let url = process.env.OWL_GIT_APIENDPOINT; let url = process.env.OWL_GIT_APIENDPOINT;
let opts = { let opts = {
headers: { headers: {
Authorization: key Authorization: key,
} },
}; };
var res = await axios.post(url, body, opts); var res = await axios.post(url, body, opts);
/* Need to read the output from the POST and pass the result upwards to the /* Need to read the output from the POST and pass the result upwards to the
client.*/ client.*/
if (res.status == 201) { if (res.status == 201) {
log.out('issueService.sendToGitea: Issue sent to Gitea', 'info'); log.out("issueService.sendToGitea: Issue sent to Gitea", "info");
return {status: res.status,message:'issue created'}; return { status: res.status, message: "issue created" };
} else { } else {
log.out(`issueService.sendToGitea: Fail to send issue: ${res.body}`, 'err'); log.out(`issueService.sendToGitea: Fail to send issue: ${res.body}`, "err");
return {status: res.status,message:'issue not created'}; return { status: res.status, message: "issue not created" };
} }
} }
module.exports = { module.exports = {
processor processor,
}; };

View File

@ -1,19 +1,19 @@
const testing = require('../services/mail.services'); const testing = require("../services/mail.services");
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
async function getAlive() { async function getAlive() {
log.out('kubeServices.getAlive: alive hook checked', 'info'); log.out("kubeServices.getAlive: alive hook checked", "info");
return {code: 200, state: {state: 'alive',noise: 'twit-twoo'}}; return { code: 200, state: { state: "alive", noise: "twit-twoo" } };
} }
async function getReady() { async function getReady() {
log.out('kubeServices.getReady: ready hook checked', 'info'); log.out("kubeServices.getReady: ready hook checked", "info");
testing.send({ testing.send({
to: 'fred@fjla.uk', to: "fred@fjla.uk",
subject: 'OwlBoard Test', subject: "OwlBoard Test",
txt: 'This is a test message from OwlBoard (testing)' txt: "This is a test message from OwlBoard (testing)",
}); });
return 'not_implemented'; return "not_implemented";
} }
async function getTime() { async function getTime() {
@ -24,5 +24,5 @@ async function getTime(){
module.exports = { module.exports = {
getAlive, getAlive,
getReady, getReady,
getTime getTime,
}; };

View File

@ -1,12 +1,12 @@
// Parse and return an LDB Request // Parse and return an LDB Request
const log = require('../utils/logs.utils'); // Log Helper const log = require("../utils/logs.utils"); // Log Helper
const ldb = require('ldbs-json'); const ldb = require("ldbs-json");
const util = require('../utils/ldb.utils'); const util = require("../utils/ldb.utils");
const san = require('../utils/sanitizer.utils'); const san = require("../utils/sanitizer.utils");
const db = require('../services/dbAccess.services'); const db = require("../services/dbAccess.services");
import { transform as staffStationTransform } from '../utils/translators/ldb/staffStation'; import { transform as staffStationTransform } from "../utils/translators/ldb/staffStation";
const ldbKey = process.env.OWL_LDB_KEY; const ldbKey = process.env.OWL_LDB_KEY;
const ldbsvKey = process.env.OWL_LDB_SVKEY; const ldbsvKey = process.env.OWL_LDB_SVKEY;
@ -15,143 +15,148 @@ async function get(id, staff=false){
const cleanId = san.cleanApiEndpointTxt(id); const cleanId = san.cleanApiEndpointTxt(id);
const obj = await util.checkCrs(cleanId); const obj = await util.checkCrs(cleanId);
try { try {
const crs = obj[0]['3ALPHA']; const crs = obj[0]["3ALPHA"];
log.out(`ldbService.get: Determined CRS for lookup to be: ${crs}`, 'info'); log.out(`ldbService.get: Determined CRS for lookup to be: ${crs}`, "info");
if (staff) { if (staff) {
const data = arrDepBoardStaff(crs); const data = arrDepBoardStaff(crs);
db.increment('ldbsvws'); db.increment("ldbsvws");
return await data; return await data;
} else { } else {
const data = arrDepBoard(crs); const data = arrDepBoard(crs);
db.increment('ldbws'); db.increment("ldbws");
return await data; return await data;
} }
} catch (err) { } catch (err) {
log.out(`ldbService.get: Error, Unable to find CRS: ${err}`, 'info'); log.out(`ldbService.get: Error, Unable to find CRS: ${err}`, "info");
return { return {
ERROR:'NOT_FOUND', ERROR: "NOT_FOUND",
description:'The entered station was not found.'}; description: "The entered station was not found.",
};
} }
} }
async function arrDepBoard(CRS) { async function arrDepBoard(CRS) {
log.out(`ldbService.arrDepBoard: Trying to fetch board for ${CRS}`, 'info'); log.out(`ldbService.arrDepBoard: Trying to fetch board for ${CRS}`, "info");
try { try {
const options = { const options = {
numRows: 10, numRows: 10,
crs: CRS.toUpperCase() crs: CRS.toUpperCase(),
}; };
const api = new ldb(ldbKey, false); const api = new ldb(ldbKey, false);
let d = await api.call('GetArrDepBoardWithDetails', options, false, false); let d = await api.call("GetArrDepBoardWithDetails", options, false, false);
return await util.cleanData(d); return await util.cleanData(d);
} catch (err) { } catch (err) {
log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`, 'warn'); log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`, "warn");
return { return {
GetStationBoardResult: 'not available', GetStationBoardResult: "not available",
Reason: `The CRS code ${CRS} is not valid` Reason: `The CRS code ${CRS} is not valid`,
}; };
} }
} }
async function arrDepBoardStaff(CRS) { async function arrDepBoardStaff(CRS) {
log.out(`ldbService.arrDepBoardStaff: Try to fetch board for ${CRS}`, 'dbug'); log.out(`ldbService.arrDepBoardStaff: Try to fetch board for ${CRS}`, "dbug");
try { try {
const options = { const options = {
numRows: 40, numRows: 40,
crs: CRS.toUpperCase(), crs: CRS.toUpperCase(),
getNonPassengerServices: true, getNonPassengerServices: true,
time: await getDateTimeString(new Date), time: await getDateTimeString(new Date()),
timeWindow: 120, timeWindow: 120,
services: 'PBS' services: "PBS",
}; };
const api = new ldb(ldbsvKey, true); const api = new ldb(ldbsvKey, true);
console.time(`Fetch Staff LDB for ${CRS.toUpperCase()}`); console.time(`Fetch Staff LDB for ${CRS.toUpperCase()}`);
const result = await api.call('GetArrivalDepartureBoardByCRS',options,false,false); const result = await api.call(
console.timeEnd(`Fetch Staff LDB for ${CRS.toUpperCase()}`) "GetArrivalDepartureBoardByCRS",
options,
false,
false
);
console.timeEnd(`Fetch Staff LDB for ${CRS.toUpperCase()}`);
try { try {
const _staffLdb = staffStationTransform(result) const _staffLdb = staffStationTransform(result);
console.log("Transformation Test Successful") console.log("Transformation Test Successful");
console.log(JSON.stringify(_staffLdb)) console.log(JSON.stringify(_staffLdb));
} catch (err) { } catch (err) {
console.log("Transformation Test Failed: " + err) console.log("Transformation Test Failed: " + err);
} }
return result return result;
} catch (err) { } catch (err) {
log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}`, 'warn'); log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}`, "warn");
log.out(`ldbService.arrDepBoardStaff: ${err}`); log.out(`ldbService.arrDepBoardStaff: ${err}`);
return { return {
GetStationBoardResult: 'not available', GetStationBoardResult: "not available",
Reason: `The CRS code ${CRS} is not valid` Reason: `The CRS code ${CRS} is not valid`,
}; };
} }
} }
async function getServiceByRID(rid) { async function getServiceByRID(rid) {
log.out(`ldbService.getServiceByRID: Finding RID: ${rid}`, 'dbug'); log.out(`ldbService.getServiceByRID: Finding RID: ${rid}`, "dbug");
try { try {
const options = { const options = {
rid: String(rid) rid: String(rid),
}; };
const api = new ldb(ldbsvKey, true); const api = new ldb(ldbsvKey, true);
return await api.call('GetServiceDetailsByRID', options,false,false); return await api.call("GetServiceDetailsByRID", options, false, false);
} catch (err) { } catch (err) {
log.out(`ldbService.queryService: ${err}`, 'EROR'); log.out(`ldbService.queryService: ${err}`, "EROR");
} }
} }
async function getServicesByOther(id) { async function getServicesByOther(id) {
log.out(`ldbService.getServiceByOther: Finding services: ${id}`, 'dbug'); log.out(`ldbService.getServiceByOther: Finding services: ${id}`, "dbug");
try { try {
const options = { const options = {
serviceID: id, serviceID: id,
sdd: getDateString(new Date) sdd: getDateString(new Date()),
}; };
const api = new ldb(ldbsvKey, true); const api = new ldb(ldbsvKey, true);
return await api.call('QueryServices', options, false, false); return await api.call("QueryServices", options, false, false);
} catch (err) { } catch (err) {
log.out(`ldbService.getServiceByOther: Error: ${err}`, 'EROR'); log.out(`ldbService.getServiceByOther: Error: ${err}`, "EROR");
return false; return false;
} }
} }
async function getReasonCodeList() { async function getReasonCodeList() {
log.out('ldbService.getReasonCodeList: Fetching reason code list', 'eror'); log.out("ldbService.getReasonCodeList: Fetching reason code list", "eror");
try { try {
const dbFilter = {}; const dbFilter = {};
return await db.query('reasonCodes', dbFilter, false); return await db.query("reasonCodes", dbFilter, false);
} catch (err) { } catch (err) {
log.out(`ldbService.getReasonCodeList: ${err}`, 'eror'); log.out(`ldbService.getReasonCodeList: ${err}`, "eror");
} }
} }
async function getReasonCode(code) { async function getReasonCode(code) {
log.out(`ldbService.getReasonCode: Fetching reason code ${code}`, 'dbug'); log.out(`ldbService.getReasonCode: Fetching reason code ${code}`, "dbug");
try { try {
const dbFilter = { const dbFilter = {
code: code code: code,
}; };
return await db.query('reasonCodes', dbFilter, false); return await db.query("reasonCodes", dbFilter, false);
} catch (err) { } catch (err) {
log.out(`ldbService.getReasonCode: ${err}`, 'eror'); log.out(`ldbService.getReasonCode: ${err}`, "eror");
} }
} }
async function getDateTimeString(date) { async function getDateTimeString(date) {
const year = date.getFullYear(), const year = date.getFullYear(),
month = String(date.getMonth() + 1).padStart(2,'0'), month = String(date.getMonth() + 1).padStart(2, "0"),
day = String(date.getDate()).padStart(2,'0'), day = String(date.getDate()).padStart(2, "0"),
hour = String(date.getHours()).padStart(2,'0'), hour = String(date.getHours()).padStart(2, "0"),
minute = String(date.getMinutes()).padStart(2,'0'), minute = String(date.getMinutes()).padStart(2, "0"),
second = String(date.getSeconds()).padStart(2,'0'); second = String(date.getSeconds()).padStart(2, "0");
const format = `${year}-${month}-${day}T${hour}:${minute}:${second}`; const format = `${year}-${month}-${day}T${hour}:${minute}:${second}`;
return format; return format;
} }
async function getDateString(date) { async function getDateString(date) {
const year = date.getFullYear(), const year = date.getFullYear(),
month = String(date.getMonth() + 1).padStart(2,'0'), month = String(date.getMonth() + 1).padStart(2, "0"),
day = String(date.getDate()).padStart(2,'0'); day = String(date.getDate()).padStart(2, "0");
const format = `${year}-${month}-${day}`; const format = `${year}-${month}-${day}`;
return format; return format;
} }
@ -161,5 +166,5 @@ module.exports = {
getServiceByRID, getServiceByRID,
getServicesByOther, getServicesByOther,
getReasonCodeList, getReasonCodeList,
getReasonCode getReasonCode,
}; };

View File

@ -1,19 +1,19 @@
const log = require('../utils/logs.utils'); // Log Helper const log = require("../utils/logs.utils"); // Log Helper
const db = require('../services/dbAccess.services'); const db = require("../services/dbAccess.services");
async function getStations() { async function getStations() {
var out = db.query('stations'); var out = db.query("stations");
log.out('listServices.getStations: Fetching stations list', 'info'); log.out("listServices.getStations: Fetching stations list", "info");
return await out; return await out;
} }
async function getCorpus() { async function getCorpus() {
var out = db.query('corpus'); var out = db.query("corpus");
log.out('listServices.getCorpus: Fetching CORPUS list', 'info'); log.out("listServices.getCorpus: Fetching CORPUS list", "info");
return await out; return await out;
} }
module.exports = { module.exports = {
getStations, getStations,
getCorpus getCorpus,
}; };

View File

@ -1,5 +1,5 @@
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
const mail = require('nodemailer'); //>> Probs wrong const mail = require("nodemailer"); //>> Probs wrong
const fromAddr = process.env.OWL_EML_FROM; const fromAddr = process.env.OWL_EML_FROM;
const smtpUser = process.env.OWL_EML_USER; const smtpUser = process.env.OWL_EML_USER;
@ -13,19 +13,19 @@ let transporter = mail.createTransport({
secure: false, // Must be false for STARTTLS on port 587 secure: false, // Must be false for STARTTLS on port 587
auth: { auth: {
user: smtpUser, user: smtpUser,
pass: smtpPass pass: smtpPass,
} },
}); });
/* 'message' is an object containing string values for: /* 'message' is an object containing string values for:
*to, cc, bcc, *subject, *txt, html. * denotes required */ *to, cc, bcc, *subject, *txt, html. * denotes required */
async function send(message) { async function send(message) {
log.out('mailServices.send: Message send request received', 'info'); log.out("mailServices.send: Message send request received", "info");
message.from = fromAddr; message.from = fromAddr;
try { try {
var res = await transporter.sendMail(message); var res = await transporter.sendMail(message);
} catch (err) { } catch (err) {
log.out(`mailServices.send: Message send failed: ${err}`, 'err'); log.out(`mailServices.send: Message send failed: ${err}`, "err");
return false; return false;
} }
log.out(`mailServices.send: SMTP Response: ${res.response}`); log.out(`mailServices.send: SMTP Response: ${res.response}`);
@ -33,5 +33,5 @@ async function send(message) {
} }
module.exports = { module.exports = {
send send,
}; };

View File

@ -1,52 +1,52 @@
// Finds PIS Codes using DB Lookups // Finds PIS Codes using DB Lookups
const db = require('../services/dbAccess.services'); const db = require("../services/dbAccess.services");
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
const clean = require('../utils/sanitizer.utils'); const clean = require("../utils/sanitizer.utils");
const supported = [ const supported = ["GW", "UK"];
'GW',
'UK'
];
async function findPisByOrigDest(start, end) { async function findPisByOrigDest(start, end) {
log.out('pisServices.findPisByOrigDest: Searching for PIS for Orig: ' + log.out(
`${start}, Dest: ${end}`, 'dbug'); "pisServices.findPisByOrigDest: Searching for PIS for Orig: " +
`${start}, Dest: ${end}`,
"dbug"
);
const firstCrs = clean.cleanApiEndpointTxt(start.toLowerCase()); const firstCrs = clean.cleanApiEndpointTxt(start.toLowerCase());
const lastCrs = clean.cleanApiEndpointTxt(end.toLowerCase()); const lastCrs = clean.cleanApiEndpointTxt(end.toLowerCase());
const query = { const query = {
stops: { stops: {
$all: [ $all: [
{ $elemMatch: { $eq: firstCrs } }, { $elemMatch: { $eq: firstCrs } },
{ $elemMatch: { $eq: lastCrs } } { $elemMatch: { $eq: lastCrs } },
] ],
}, },
$expr: { $expr: {
$and: [ $and: [
{ $eq: [{ $arrayElemAt: [ '$stops', -1 ] }, lastCrs] }, { $eq: [{ $arrayElemAt: ["$stops", -1] }, lastCrs] },
{ $eq: [{ $arrayElemAt: [ '$stops', 0 ] }, firstCrs] } { $eq: [{ $arrayElemAt: ["$stops", 0] }, firstCrs] },
] ],
} },
}; };
const search = db.query('pis', query); const search = db.query("pis", query);
return await search; return await search;
} }
async function findPisByCode(code) { async function findPisByCode(code) {
log.out(`pisServices.findPisByCode: Searching for PIS code: ${code}`, 'dbug'); log.out(`pisServices.findPisByCode: Searching for PIS code: ${code}`, "dbug");
const cleanCode = clean.removeNonNumeric(code); const cleanCode = clean.removeNonNumeric(code);
const query = { const query = {
'code': parseInt(cleanCode) code: parseInt(cleanCode),
}; };
const search = db.query('pis', query); const search = db.query("pis", query);
return await search; return await search;
} }
async function findByTiplocArray(tiplocArray) { async function findByTiplocArray(tiplocArray) {
const query = { const query = {
'tiplocs': tiplocArray tiplocs: tiplocArray,
}; };
const res = db.query('pis', query); const res = db.query("pis", query);
return await res; return await res;
} }
@ -54,5 +54,5 @@ module.exports = {
supported, supported,
findPisByOrigDest, findPisByOrigDest,
findPisByCode, findPisByCode,
findByTiplocArray findByTiplocArray,
}; };

View File

@ -1,19 +1,19 @@
import { createClient } from "redis"; import { createClient } from "redis";
import zlib from 'zlib'; import zlib from "zlib";
const client = createClient({ const client = createClient({
url: "redis:PORT" url: "redis:PORT",
}); });
client.on('error', err => console.log('Redis Client Error', err)); client.on("error", (err) => console.log("Redis Client Error", err));
async function addToCache(key: string, value: Object): Promise<boolean> { async function addToCache(key: string, value: Object): Promise<boolean> {
throw new Error("Unable to post to cache") throw new Error("Unable to post to cache");
} }
async function getFromCache(key: string): Promise<Object> { async function getFromCache(key: string): Promise<Object> {
throw new Error("Unable to retreive") throw new Error("Unable to retreive");
} }
/* /*
await client.connect(); await client.connect();

View File

@ -1,29 +1,32 @@
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
const auth = require('../utils/auth.utils'); const auth = require("../utils/auth.utils");
const db = require('./dbAccess.services'); const db = require("./dbAccess.services");
const mail = require('./mail.services'); const mail = require("./mail.services");
const clean = require('../utils/sanitizer.utils'); const clean = require("../utils/sanitizer.utils");
const domains = require('../configs/domains.configs'); const domains = require("../configs/domains.configs");
const errors = require('../configs/errorCodes.configs'); const errors = require("../configs/errorCodes.configs");
async function createRegKey(body) { async function createRegKey(body) {
log.out('registerServices.createRegKey: Incoming request', 'INFO'); log.out("registerServices.createRegKey: Incoming request", "INFO");
if (body.email) { if (body.email) {
const domain = await clean.getDomainFromEmail(body.email); const domain = await clean.getDomainFromEmail(body.email);
log.out(`registerServices: Registration request from: ${domain}`, 'info'); log.out(`registerServices: Registration request from: ${domain}`, "info");
if (domains.includes(domain)) { if (domains.includes(domain)) {
log.out(`registerServices.createRegKey: Key from valid: ${domain}`,'info'); log.out(
`registerServices.createRegKey: Key from valid: ${domain}`,
"info"
);
const uuid = await auth.generateKey(); const uuid = await auth.generateKey();
db.addRegReq(uuid, domain); db.addRegReq(uuid, domain);
const message = await auth.generateConfirmationEmail(body.email, uuid); const message = await auth.generateConfirmationEmail(body.email, uuid);
if (!message) { if (!message) {
const err = new Error('Message generation error'); const err = new Error("Message generation error");
log.out('registerServices.createRegKey: Error generating email', 'err'); log.out("registerServices.createRegKey: Error generating email", "err");
log.out(err, 'err'); log.out(err, "err");
return 500; return 500;
} }
if (await mail.send(message) == true) { if ((await mail.send(message)) == true) {
return {status: 201, message: 'email sent'}; return { status: 201, message: "email sent" };
} }
return { status: 500, errorCode: 950, errorMsg: errors[950] }; return { status: 500, errorCode: 950, errorMsg: errors[950] };
} }
@ -33,29 +36,30 @@ async function createRegKey(body) {
} }
} }
async function regUser(req) { // Add input validation async function regUser(req) {
log.out(`Read UUID: ${req.uuid}`, 'dbug'); // Add input validation
log.out(`registrationServices.regUser: Request from: ${req.uuid}`, 'info'); log.out(`Read UUID: ${req.uuid}`, "dbug");
log.out(`registrationServices.regUser: Request from: ${req.uuid}`, "info");
const res = await auth.checkRequest(req.uuid); const res = await auth.checkRequest(req.uuid);
log.out(`registrationServices.regUser: ${JSON.stringify(res)}`, 'info'); log.out(`registrationServices.regUser: ${JSON.stringify(res)}`, "info");
if (res.result) { if (res.result) {
const uuid = await auth.generateKey(); const uuid = await auth.generateKey();
const apiKey = await db.addUser(uuid, res.domain); const apiKey = await db.addUser(uuid, res.domain);
if (apiKey) { if (apiKey) {
db.delRegReq(req.uuid); db.delRegReq(req.uuid);
return {status: 201, message: 'User added', api_key: uuid}; return { status: 201, message: "User added", api_key: uuid };
} }
} }
return { status: 401, errorCode: 703, errorMsg: errors[703] }; return { status: 401, errorCode: 703, errorMsg: errors[703] };
} }
async function getUser(uuid) { async function getUser(uuid) {
log.out('registrationServices: Finding user for given UUID', 'dbug'); log.out("registrationServices: Finding user for given UUID", "dbug");
try { try {
const filter = { const filter = {
uuid: uuid uuid: uuid,
}; };
const res = await db.query('users', filter, false); const res = await db.query("users", filter, false);
if (res.length) { if (res.length) {
return res; return res;
} else { } else {
@ -70,5 +74,5 @@ async function getUser(uuid) {
module.exports = { module.exports = {
regUser, regUser,
createRegKey, createRegKey,
getUser getUser,
}; };

View File

@ -1,20 +1,20 @@
const log = require('../utils/logs.utils'); // Log Helper const log = require("../utils/logs.utils"); // Log Helper
const db = require('../services/dbAccess.services'); const db = require("../services/dbAccess.services");
const os = require('os'); const os = require("os");
const vers = require('../configs/version.configs'); const vers = require("../configs/version.configs");
async function buildJson() { async function buildJson() {
let json = {}; let json = {};
json.count = {}; json.count = {};
// Async call all db queries // Async call all db queries
const counters = db.query('meta', {target: 'counters'}); const counters = db.query("meta", { target: "counters" });
const versions = db.query('meta', {target: 'versions'}); const versions = db.query("meta", { target: "versions" });
const userCount = db.colCount('users'); const userCount = db.colCount("users");
const regCount = db.colCount('registrations'); const regCount = db.colCount("registrations");
const pisCount = db.colCount('pis'); const pisCount = db.colCount("pis");
const corpusCount = db.colCount('corpus'); const corpusCount = db.colCount("corpus");
const stationsCount = db.colCount('stations'); const stationsCount = db.colCount("stations");
const timetableCount = db.colCount('timetable'); const timetableCount = db.colCount("timetable");
// Insert data // Insert data
json.mode = process.env.NODE_ENV; json.mode = process.env.NODE_ENV;
@ -34,40 +34,40 @@ async function buildJson() {
} }
async function hits() { async function hits() {
log.out('statsServices.hits: Statistics Requested', 'info'); log.out("statsServices.hits: Statistics Requested", "info");
const out = await buildJson(); const out = await buildJson();
log.out(`statsServices.hits: Sending Data: ${JSON.stringify(out)}`, 'info'); log.out(`statsServices.hits: Sending Data: ${JSON.stringify(out)}`, "info");
return out; return out;
} }
async function getVersions() { async function getVersions() {
log.out('statsServices.getVersions: Fetching versions', 'info'); log.out("statsServices.getVersions: Fetching versions", "info");
const dbMan = await db.query('versions', {target: 'dbmanager'}); const dbMan = await db.query("versions", { target: "dbmanager" });
const mqClt = await db.query('versions', {target: 'mq-client'}); const mqClt = await db.query("versions", { target: "mq-client" });
const data = { const data = {
'backend': vers.app, backend: vers.app,
'db-manager': dbMan[0]?.['version'] || '', "db-manager": dbMan[0]?.["version"] || "",
'mq-client': mqClt[0]?.['version'] || '', "mq-client": mqClt[0]?.["version"] || "",
} };
return data return data;
} }
async function statistics() { async function statistics() {
log.out('statsServices.statistics: Fetching statistics', 'info'); log.out("statsServices.statistics: Fetching statistics", "info");
const countersPromise = db.query('meta', { target: 'counters' }); const countersPromise = db.query("meta", { target: "counters" });
const timetablePromise = db.query('meta', { target: 'timetable' }); const timetablePromise = db.query("meta", { target: "timetable" });
const pisPromise = db.query('meta', { target: 'pis' }); const pisPromise = db.query("meta", { target: "pis" });
const corpusPromise = db.query('meta', { target: 'corpus' }); const corpusPromise = db.query("meta", { target: "corpus" });
const reasonCodesPromise = db.query('meta', { target: 'reasonCodes' }); const reasonCodesPromise = db.query("meta", { target: "reasonCodes" });
const lengthUsersPromise = db.colCount('users'); const lengthUsersPromise = db.colCount("users");
const lengthRegistrationsPromise = db.colCount('registrations'); const lengthRegistrationsPromise = db.colCount("registrations");
const lengthCorpusPromise = db.colCount('corpus'); const lengthCorpusPromise = db.colCount("corpus");
const lengthStationsPromise = db.colCount('stations'); const lengthStationsPromise = db.colCount("stations");
const lengthPisPromise = db.colCount('pis'); const lengthPisPromise = db.colCount("pis");
const lengthTimetablePromise = db.colCount('timetable'); const lengthTimetablePromise = db.colCount("timetable");
const lengthReasonCodesPromise = db.colCount('reasonCodes'); const lengthReasonCodesPromise = db.colCount("reasonCodes");
const [ const [
counters, counters,
@ -81,7 +81,7 @@ async function statistics() {
lengthStations, lengthStations,
lengthPis, lengthPis,
lengthTimetable, lengthTimetable,
lengthReasonCodes lengthReasonCodes,
] = await Promise.all([ ] = await Promise.all([
countersPromise, countersPromise,
timetablePromise, timetablePromise,
@ -94,27 +94,27 @@ async function statistics() {
lengthStationsPromise, lengthStationsPromise,
lengthPisPromise, lengthPisPromise,
lengthTimetablePromise, lengthTimetablePromise,
lengthReasonCodesPromise lengthReasonCodesPromise,
]); ]);
return { return {
hostname: os.hostname() || 'Unknown', hostname: os.hostname() || "Unknown",
runtimeMode: process.env.NODE_ENV || 'Unknown', runtimeMode: process.env.NODE_ENV || "Unknown",
reset: counters[0]['since'], reset: counters[0]["since"],
updateTimes: { updateTimes: {
timetable: timetable[0]['updated'], timetable: timetable[0]["updated"],
pis: pis[0]['updated'], pis: pis[0]["updated"],
corpus: corpus[0]['updated'], corpus: corpus[0]["updated"],
reasonCodes: reasonCodes[0]['updated'] reasonCodes: reasonCodes[0]["updated"],
}, },
requestCounts: { requestCounts: {
ldbws_api: counters[0]['ldbws'] || 0, ldbws_api: counters[0]["ldbws"] || 0,
lsbsvws_api: counters[0]['ldbsvws'] || 0, lsbsvws_api: counters[0]["ldbsvws"] || 0,
corpus_api: counters[0]['corpus_api'] || 0, corpus_api: counters[0]["corpus_api"] || 0,
timetable_db: counters[0]['timetable'] || 0, timetable_db: counters[0]["timetable"] || 0,
pis_db: counters[0]['pis'] || 0, pis_db: counters[0]["pis"] || 0,
corpus_db: counters[0]['corpus'] || 0, corpus_db: counters[0]["corpus"] || 0,
stations_db: counters[0]['stations'] || 0 stations_db: counters[0]["stations"] || 0,
}, },
dbLengths: { dbLengths: {
users: lengthUsers, users: lengthUsers,
@ -123,14 +123,13 @@ async function statistics() {
stations: lengthStations, stations: lengthStations,
pis: lengthPis, pis: lengthPis,
timetable: lengthTimetable, timetable: lengthTimetable,
reasonCodes: lengthReasonCodes reasonCodes: lengthReasonCodes,
}, },
}; };
} }
module.exports = { module.exports = {
hits, hits,
statistics, statistics,
getVersions getVersions,
}; };

View File

@ -1,23 +1,26 @@
const log = require('../utils/logs.utils'); const log = require("../utils/logs.utils");
const db = require('./dbAccess.services'); const db = require("./dbAccess.services");
const clean = require('../utils/sanitizer.utils'); const clean = require("../utils/sanitizer.utils");
const pis = require('../services/pis.services'); const pis = require("../services/pis.services");
const { filter } = require('compression'); const { filter } = require("compression");
async function findByHeadcodeToday(headcode) { async function findByHeadcodeToday(headcode) {
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase();
log.out('trainServiceServices.findByHeadcode: Searching for headcode ' + log.out(
sanitizedHeadcode, 'dbug'); "trainServiceServices.findByHeadcode: Searching for headcode " +
sanitizedHeadcode,
"dbug"
);
const now = new Date(); const now = new Date();
const dayMap = ['su', 'm', 't', 'w', 'th', 'f', 's']; const dayMap = ["su", "m", "t", "w", "th", "f", "s"];
const shortDay = dayMap[now.getDay()]; // Fetch short day from map const shortDay = dayMap[now.getDay()]; // Fetch short day from map
const query = { const query = {
headcode: sanitizedHeadcode, headcode: sanitizedHeadcode,
scheduleStartDate: { $lte: now }, scheduleStartDate: { $lte: now },
scheduleEndDate: { $gte: now }, scheduleEndDate: { $gte: now },
daysRun: {$in: [shortDay]} daysRun: { $in: [shortDay] },
}; };
const queryData = await db.query('timetable', query); const queryData = await db.query("timetable", query);
let trainData = await parseTrains(queryData); let trainData = await parseTrains(queryData);
let preparedData = []; let preparedData = [];
for (let trainService in trainData) { for (let trainService in trainData) {
@ -27,9 +30,9 @@ async function findByHeadcodeToday(headcode) {
//console.log(tiplocList.length); console.log(tiplocList); //console.log(tiplocList.length); console.log(tiplocList);
if (tiplocList.length) { if (tiplocList.length) {
const pisDetail = await pis.findByTiplocArray(tiplocList); const pisDetail = await pis.findByTiplocArray(tiplocList);
trainService['pis'] = pisDetail?.[0]?.['code'] ?? 'None'; trainService["pis"] = pisDetail?.[0]?.["code"] ?? "None";
} else if (trainService?.operator === 'GW') { } else if (trainService?.operator === "GW") {
trainService['pis'] = '0015'; // Not in Service code trainService["pis"] = "0015"; // Not in Service code
// '0015' is a string becuase 0015 is not a valid number.. // '0015' is a string becuase 0015 is not a valid number..
} }
} }
@ -40,42 +43,45 @@ async function findByHeadcodeToday(headcode) {
async function findByHeadcode(date, headcode) { async function findByHeadcode(date, headcode) {
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase();
log.out('trainServiceServices.findByHeadcode: Searching for headcode ' + log.out(
sanitizedHeadcode, 'dbug'); "trainServiceServices.findByHeadcode: Searching for headcode " +
sanitizedHeadcode,
"dbug"
);
let searchDate; let searchDate;
if (date === 'now') { if (date === "now") {
searchDate = new Date(); searchDate = new Date();
} else { } else {
searchDate = new Date(date); searchDate = new Date(date);
} }
searchDate.setHours(12, 0, 0); // Set to midday to avoid any timezone issues searchDate.setHours(12, 0, 0); // Set to midday to avoid any timezone issues
const dayMap = ['su', 'm', 't', 'w', 'th', 'f', 's']; const dayMap = ["su", "m", "t", "w", "th", "f", "s"];
const shortDay = dayMap[searchDate.getDay()]; // Fetch short day from map const shortDay = dayMap[searchDate.getDay()]; // Fetch short day from map
const query = { const query = {
headcode: sanitizedHeadcode, headcode: sanitizedHeadcode,
scheduleStartDate: { $lte: searchDate }, scheduleStartDate: { $lte: searchDate },
scheduleEndDate: { $gte: searchDate }, scheduleEndDate: { $gte: searchDate },
daysRun: {$in: [shortDay]} daysRun: { $in: [shortDay] },
}; };
const pipeline = [ const pipeline = [
{ {
$match: query $match: query,
}, },
{ {
$project: { $project: {
operator: 1, operator: 1,
stops: { stops: {
$concatArrays: [ $concatArrays: [
[{ $first: '$stops'}], [{ $first: "$stops" }],
[{ $arrayElemAt: ['$stops', -1]}] [{ $arrayElemAt: ["$stops", -1] }],
] ],
}, },
trainUid: 1, trainUid: 1,
stpIndicator: 1 stpIndicator: 1,
} },
} },
]; ];
const queryData = await db.queryAggregate('timetable', pipeline); const queryData = await db.queryAggregate("timetable", pipeline);
console.log(JSON.stringify(queryData)); console.log(JSON.stringify(queryData));
let filteredData = filterServices(queryData); let filteredData = filterServices(queryData);
return await filteredData; return await filteredData;
@ -83,7 +89,7 @@ async function findByHeadcode(date, headcode) {
async function findByTrainUid(uid, date = new Date()) { async function findByTrainUid(uid, date = new Date()) {
let queryDate; let queryDate;
if (date === 'now') { if (date === "now") {
queryDate = new Date(); queryDate = new Date();
} else { } else {
queryDate = date; queryDate = date;
@ -91,9 +97,9 @@ async function findByTrainUid(uid, date = new Date()) {
const query = { const query = {
trainUid: uid, trainUid: uid,
scheduleStartDate: { $lte: queryDate }, scheduleStartDate: { $lte: queryDate },
scheduleEndDate: {$gte: queryDate} scheduleEndDate: { $gte: queryDate },
}; };
const queryData = await db.query('timetable', query); const queryData = await db.query("timetable", query);
if (queryData.length === 0) { if (queryData.length === 0) {
return []; return [];
} }
@ -106,8 +112,8 @@ async function findByTrainUid(uid, date = new Date()) {
if (publicStops.length) { if (publicStops.length) {
const pisCode = await pis.findByTiplocArray(publicStops); const pisCode = await pis.findByTiplocArray(publicStops);
services[0].pis = pisCode[0]?.code; services[0].pis = pisCode[0]?.code;
} else if ( services[0]?.operator === 'GW' && !publicStops.length) { } else if (services[0]?.operator === "GW" && !publicStops.length) {
services[0].pis = '0015'; services[0].pis = "0015";
} }
} }
return services[0]; return services[0];
@ -116,7 +122,7 @@ async function findByTrainUid(uid, date = new Date()) {
module.exports = { module.exports = {
findByHeadcodeToday, findByHeadcodeToday,
findByHeadcode, findByHeadcode,
findByTrainUid findByTrainUid,
}; };
/* Internal Functions, not to be exported */ /* Internal Functions, not to be exported */
@ -126,8 +132,8 @@ module.exports = {
async function getPublicStops(data) { async function getPublicStops(data) {
let tiplocList = []; let tiplocList = [];
for (const publicStop in data) { for (const publicStop in data) {
if (data[publicStop]['isPublic']) { if (data[publicStop]["isPublic"]) {
tiplocList.push(data[publicStop]['tiploc']); tiplocList.push(data[publicStop]["tiploc"]);
} }
} }
return tiplocList; return tiplocList;
@ -139,7 +145,7 @@ async function getPublicStops(data) {
async function parseTrains(data) { async function parseTrains(data) {
let trainUids = []; let trainUids = [];
for (const i of data) { for (const i of data) {
const trainUid = i['trainUid']; const trainUid = i["trainUid"];
if (!trainUids.includes(trainUid)) { if (!trainUids.includes(trainUid)) {
trainUids.push(trainUid); trainUids.push(trainUid);
} }
@ -153,53 +159,55 @@ async function parseTrains(data) {
} }
async function filterServices(data) { async function filterServices(data) {
let stpIndicators = {}, filteredServices = []; let stpIndicators = {},
filteredServices = [];
for (const serviceDetail of data) { for (const serviceDetail of data) {
const trainUid = serviceDetail['trainUid']; const trainUid = serviceDetail["trainUid"];
const stpIndicator = serviceDetail['stpIndicator']; const stpIndicator = serviceDetail["stpIndicator"];
if (!stpIndicators[trainUid]) { if (!stpIndicators[trainUid]) {
stpIndicators[trainUid] = { stpIndicators[trainUid] = {
hasC: false, hasC: false,
hasN: false, hasN: false,
hasO: false, hasO: false,
hasP: false hasP: false,
}; };
} }
if (stpIndicator === 'C') { if (stpIndicator === "C") {
stpIndicators[trainUid].hasC = true; stpIndicators[trainUid].hasC = true;
} }
if (stpIndicator === 'N') { if (stpIndicator === "N") {
stpIndicators[trainUid].hasN = true; stpIndicators[trainUid].hasN = true;
} }
if (stpIndicator === 'O') { if (stpIndicator === "O") {
stpIndicators[trainUid].hasO = true; stpIndicators[trainUid].hasO = true;
} }
if (stpIndicator === 'P') { if (stpIndicator === "P") {
stpIndicators[trainUid].hasP = true; stpIndicators[trainUid].hasP = true;
} }
} }
let preparedData; let preparedData;
for (const serviceDetail of data) { for (const serviceDetail of data) {
const trainUid = serviceDetail['trainUid']; const trainUid = serviceDetail["trainUid"];
const thisStpIndicators = stpIndicators[trainUid]; const thisStpIndicators = stpIndicators[trainUid];
const stpIndicator = serviceDetail['stpIndicator']; const stpIndicator = serviceDetail["stpIndicator"];
if (stpIndicator === 'C') { if (stpIndicator === "C") {
break; break;
} }
if (stpIndicator === 'N' && if (stpIndicator === "N" && !thisStpIndicators.hasC) {
!thisStpIndicators.hasC) {
filteredServices.push(serviceDetail); filteredServices.push(serviceDetail);
} else if (
} else if (stpIndicator === 'O' && stpIndicator === "O" &&
!thisStpIndicators.hasC && !thisStpIndicators.hasC &&
!thisStpIndicators.hasN) { !thisStpIndicators.hasN
) {
filteredServices.push(serviceDetail); filteredServices.push(serviceDetail);
} else if (
} else if (stpIndicator === 'P' && stpIndicator === "P" &&
!thisStpIndicators.hasC && !thisStpIndicators.hasC &&
!thisStpIndicators.hasN && !thisStpIndicators.hasN &&
!thisStpIndicators.hasO) { !thisStpIndicators.hasO
) {
filteredServices.push(serviceDetail); filteredServices.push(serviceDetail);
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,9 +1,9 @@
import { removeNewlineAndPTag } from "../../src/utils/newSanitizer"; import { removeNewlineAndPTag } from "../../src/utils/newSanitizer";
describe('newSanitizer', () => { describe("newSanitizer", () => {
test('Should remove /\n and <p>/</p> elements', () => { test("Should remove /\n and <p>/</p> elements", () => {
const input = "\n<p>This is a string</p>"; const input = "\n<p>This is a string</p>";
const expectedOutput = "This is a string" const expectedOutput = "This is a string";
expect(removeNewlineAndPTag(input)).toEqual(expectedOutput); expect(removeNewlineAndPTag(input)).toEqual(expectedOutput);
}); });
}); });

View File

@ -1,31 +1,31 @@
import { getDomainFromEmail } from "../../src/utils/sanitizer.utils"; import { getDomainFromEmail } from "../../src/utils/sanitizer.utils";
import { removeNonNumeric } from "../../src/utils/sanitizer.utils"; import { removeNonNumeric } from "../../src/utils/sanitizer.utils";
describe('Sanitize Email', () => { describe("Sanitize Email", () => {
const inputs = [ const inputs = [
"this+is+an-_email@example.com", "this+is+an-_email@example.com",
'"unusual email"@example.com', '"unusual email"@example.com',
"(brackets)addr@example.com", "(brackets)addr@example.com",
"I%Have{Special}%Characters@example.com", "I%Have{Special}%Characters@example.com",
"Basic.address@example.com", "Basic.address@example.com",
`"very.(),:;<>[]\".VERY.\"very\ \"very\".unusual"@example.com` `"very.(),:;<>[]\".VERY.\"very\ \"very\".unusual"@example.com`,
] ];
const expectedOutput = "example.com" const expectedOutput = "example.com";
for (const addr of inputs) { for (const addr of inputs) {
test(`Should return only domain: ${addr}`, () => { test(`Should return only domain: ${addr}`, () => {
expect(getDomainFromEmail(addr)).toEqual(expectedOutput); expect(getDomainFromEmail(addr)).toEqual(expectedOutput);
}) });
} }
}); });
describe('Remove non-numeric', () => { describe("Remove non-numeric", () => {
const inputs = ['abc123','<%43)($£@:}jfkd4'] const inputs = ["abc123", "<%43)($£@:}jfkd4"];
const expectedOutputs = ['123','434'] const expectedOutputs = ["123", "434"];
for (const key in inputs) { for (const key in inputs) {
const input = inputs[key]; const input = inputs[key];
const desired = expectedOutputs[key]; const desired = expectedOutputs[key];
test(`Should return only numbers: ${key}`, () => { test(`Should return only numbers: ${key}`, () => {
expect(removeNonNumeric(input)).toEqual(desired); expect(removeNonNumeric(input)).toEqual(desired);
}) });
} }
}) });

View File

@ -1,6 +1,6 @@
import { import {
transform, transform,
calculateLength calculateLength,
} from "../../../../src/utils/translators/ldb/staffStation"; } from "../../../../src/utils/translators/ldb/staffStation";
import { inputs } from "./stationInputs"; import { inputs } from "./stationInputs";
@ -8,38 +8,36 @@ import { outputs } from "./stationOutputs";
import { noLength as serviceNoLength } from "./trainServiceInputs"; import { noLength as serviceNoLength } from "./trainServiceInputs";
import { trainServices } from "./trainServiceInputs"; import { trainServices } from "./trainServiceInputs";
describe('transform', () => { describe("transform", () => {
test('Should return null for empty input', () => { test("Should return null for empty input", () => {
const input = {}; const input = {};
expect(transform(input)).toBeNull(); expect(transform(input)).toBeNull();
}); });
for (const testNo in inputs) { for (const testNo in inputs) {
test(`Should correctly transform data ${testNo}`, () => { test(`Should correctly transform data ${testNo}`, () => {
const input = inputs[testNo] const input = inputs[testNo];
const expectedOutput = outputs[testNo] const expectedOutput = outputs[testNo];
expect(transform(input)).toEqual(expectedOutput); expect(transform(input)).toEqual(expectedOutput);
}); });
} }
}); });
// Write test for calculateLength(input: TrainServices): number | undefined // Write test for calculateLength(input: TrainServices): number | undefined
describe('calculateLength', () => { describe("calculateLength", () => {
test('Should return ubdefined for no length', () => { test("Should return ubdefined for no length", () => {
const input = serviceNoLength; const input = serviceNoLength;
expect(calculateLength(input)).toBeUndefined(); expect(calculateLength(input)).toBeUndefined();
}); });
for (const testNo in trainServices) { for (const testNo in trainServices) {
test(`Should correctly calculate ${testNo}`, () => { test(`Should correctly calculate ${testNo}`, () => {
const input = trainServices[testNo] const input = trainServices[testNo];
const expectedOutput = 4 const expectedOutput = 4;
expect(calculateLength(input)).toEqual(expectedOutput); expect(calculateLength(input)).toEqual(expectedOutput);
}); });
} }
}); });

View File

@ -1,19 +1,19 @@
export const inputs: any[] = [ export const inputs: any[] = [
{ {
GetBoardResult: { GetBoardResult: {
generatedAt: '2023-08-01T20:37:05.559123+01:00', generatedAt: "2023-08-01T20:37:05.559123+01:00",
locationName: 'Railway Station', locationName: "Railway Station",
crs: 'RLY', crs: "RLY",
stationManager: 'Network Rail', stationManager: "Network Rail",
stationManagerCode: 'RT', stationManagerCode: "RT",
nrccMessages: { nrccMessages: {
message: { message: {
severity: "minor", severity: "minor",
xhtmlMessage: '\n<p>Minor Alert</p>', xhtmlMessage: "\n<p>Minor Alert</p>",
type: "station" type: "station",
}
}, },
isTruncated: 'true', },
isTruncated: "true",
trainServices: { trainServices: {
service: [ service: [
{ {
@ -36,21 +36,21 @@ export const inputs: any[] = [
location: { location: {
locationName: "Plymouth", locationName: "Plymouth",
crs: "PLY", crs: "PLY",
tiploc: "PLYMTH" tiploc: "PLYMTH",
} },
}, },
destination: { destination: {
location: { location: {
locationName: "Birmingham New Street", locationName: "Birmingham New Street",
crs: "BHM", crs: "BHM",
tiploc: "BHAMNWS" tiploc: "BHAMNWS",
} },
}, },
category: "XX", category: "XX",
activities: "T", activities: "T",
}
]
}
}
}, },
] ],
},
},
},
];

View File

@ -1,5 +1,9 @@
import type { StaffLdb, NrccMessage, TrainServices, import type {
ServiceLocation } from '@owlboard/ts-types'; StaffLdb,
NrccMessage,
TrainServices,
ServiceLocation,
} from "@owlboard/ts-types";
export const outputs: StaffLdb[] = [ export const outputs: StaffLdb[] = [
{ {
@ -9,8 +13,8 @@ export const outputs: StaffLdb[] = [
nrccMessages: [ nrccMessages: [
{ {
severity: "minor", severity: "minor",
xhtmlMessage: "Minor Alert" xhtmlMessage: "Minor Alert",
} },
], ],
trainServices: [ trainServices: [
{ {
@ -28,17 +32,17 @@ export const outputs: StaffLdb[] = [
length: 10, length: 10,
origin: [ origin: [
{ {
tiploc: "PLYMTH" tiploc: "PLYMTH",
} },
], ],
destination: [ destination: [
{ {
tiploc: "BHAMNWS" tiploc: "BHAMNWS",
} },
] ],
} },
], ],
busServices: [], busServices: [],
ferryServices: [] ferryServices: [],
}, },
] ];

View File

@ -1,41 +1,149 @@
import type { TrainServices } from "@owlboard/ts-types"; import type { TrainServices } from "@owlboard/ts-types";
export const noLength: any = { export const noLength: any = {
"rid": "202308058004480", rid: "202308058004480",
"uid": "P04480", uid: "P04480",
"trainid": "1A39", trainid: "1A39",
"sdd": "2023-08-05", sdd: "2023-08-05",
"operator": "Great Western Railway", operator: "Great Western Railway",
"operatorCode": "GW", operatorCode: "GW",
"sta": "2023-08-05T21:51:00", sta: "2023-08-05T21:51:00",
"eta": "2023-08-05T23:04:18", eta: "2023-08-05T23:04:18",
"arrivalType": "Forecast", arrivalType: "Forecast",
"std": "2023-08-05T22:00:00", std: "2023-08-05T22:00:00",
"etd": "2023-08-05T23:05:18", etd: "2023-08-05T23:05:18",
"departureType": "Forecast", departureType: "Forecast",
"departureSource": "Darwin", departureSource: "Darwin",
"platform": "7", platform: "7",
"origin": { origin: {
"location": { location: {
"locationName": "Penzance", locationName: "Penzance",
"crs": "PNZ", crs: "PNZ",
"tiploc": "PENZNCE" tiploc: "PENZNCE",
}
}, },
"destination": {
"location": {
"locationName": "London Paddington",
"crs": "PAD",
"tiploc": "PADTON"
}
}, },
"delayReason": "887", destination: {
"category": "XX", location: {
"activities": "T" locationName: "London Paddington",
} crs: "PAD",
tiploc: "PADTON",
},
},
delayReason: "887",
category: "XX",
activities: "T",
};
export const trainServices: any[] = [ export const trainServices: any[] = [
{"rid":"202308058005927","uid":"P05927","trainid":"2T53","sdd":"2023-08-05","operator":"Great Western Railway","operatorCode":"GW","sta":"2023-08-05T19:52:00","eta":"2023-08-05T19:52:00","arrivalType":"Forecast","std":"2023-08-05T19:56:00","etd":"2023-08-05T19:56:00","departureType":"Forecast","departureSource":"Darwin","platform":"2","formation":{"coaches":{"coach":[{"coachClass":"Standard"},{"coachClass":"Standard"},{"coachClass":"Standard"},{"coachClass":"Standard"}]}},"origin":{"location":{"locationName":"Worcester Foregate Street","crs":"WOF","tiploc":"WORCSFS"}},"destination":{"location":{"locationName":"Bristol Temple Meads","crs":"BRI","tiploc":"BRSTLTM","via":"via Gloucester"}},"category":"OO","activities":"T"}, {
{"rid":"202308057126314","uid":"G26314","trainid":"2V88","sdd":"2023-08-05","operator":"West Midlands Trains","operatorCode":"LM","sta":"2023-08-05T18:28:00","eta":"2023-08-05T18:28:00","arrivalType":"Forecast","std":"2023-08-05T18:33:00","etd":"2023-08-05T18:33:00","departureType":"Forecast","departureSource":"Darwin","platform":"2","formation":{"coaches":{"coach":[{"coachClass":"Standard"},{"coachClass":"Standard","toilet":"Accessible"},{"coachClass":"Standard"},{"coachClass":"Standard","toilet":"Accessible"}]}},"origin":{"location":{"locationName":"Dorridge","crs":"DDG","tiploc":"DORIDGE"}},"destination":{"location":{"locationName":"Worcester Foregate Street","crs":"WOF","tiploc":"WORCSFS"}},"category":"OO","activities":"T RM","length":"4"}, rid: "202308058005927",
{"rid":"202308057126318","uid":"G26318","trainid":"2V96","sdd":"2023-08-05","operator":"West Midlands Trains","operatorCode":"LM","sta":"2023-08-05T19:28:00","eta":"2023-08-05T19:28:00","arrivalType":"Forecast","std":"2023-08-05T19:33:00","etd":"2023-08-05T19:33:00","departureType":"Forecast","departureSource":"Darwin","platform":"2","origin":{"location":{"locationName":"Dorridge","crs":"DDG","tiploc":"DORIDGE"}},"destination":{"location":{"locationName":"Worcester Foregate Street","crs":"WOF","tiploc":"WORCSFS"}},"category":"OO","activities":"T RM","length":"4"} uid: "P05927",
] trainid: "2T53",
sdd: "2023-08-05",
operator: "Great Western Railway",
operatorCode: "GW",
sta: "2023-08-05T19:52:00",
eta: "2023-08-05T19:52:00",
arrivalType: "Forecast",
std: "2023-08-05T19:56:00",
etd: "2023-08-05T19:56:00",
departureType: "Forecast",
departureSource: "Darwin",
platform: "2",
formation: {
coaches: {
coach: [
{ coachClass: "Standard" },
{ coachClass: "Standard" },
{ coachClass: "Standard" },
{ coachClass: "Standard" },
],
},
},
origin: {
location: {
locationName: "Worcester Foregate Street",
crs: "WOF",
tiploc: "WORCSFS",
},
},
destination: {
location: {
locationName: "Bristol Temple Meads",
crs: "BRI",
tiploc: "BRSTLTM",
via: "via Gloucester",
},
},
category: "OO",
activities: "T",
},
{
rid: "202308057126314",
uid: "G26314",
trainid: "2V88",
sdd: "2023-08-05",
operator: "West Midlands Trains",
operatorCode: "LM",
sta: "2023-08-05T18:28:00",
eta: "2023-08-05T18:28:00",
arrivalType: "Forecast",
std: "2023-08-05T18:33:00",
etd: "2023-08-05T18:33:00",
departureType: "Forecast",
departureSource: "Darwin",
platform: "2",
formation: {
coaches: {
coach: [
{ coachClass: "Standard" },
{ coachClass: "Standard", toilet: "Accessible" },
{ coachClass: "Standard" },
{ coachClass: "Standard", toilet: "Accessible" },
],
},
},
origin: {
location: { locationName: "Dorridge", crs: "DDG", tiploc: "DORIDGE" },
},
destination: {
location: {
locationName: "Worcester Foregate Street",
crs: "WOF",
tiploc: "WORCSFS",
},
},
category: "OO",
activities: "T RM",
length: "4",
},
{
rid: "202308057126318",
uid: "G26318",
trainid: "2V96",
sdd: "2023-08-05",
operator: "West Midlands Trains",
operatorCode: "LM",
sta: "2023-08-05T19:28:00",
eta: "2023-08-05T19:28:00",
arrivalType: "Forecast",
std: "2023-08-05T19:33:00",
etd: "2023-08-05T19:33:00",
departureType: "Forecast",
departureSource: "Darwin",
platform: "2",
origin: {
location: { locationName: "Dorridge", crs: "DDG", tiploc: "DORIDGE" },
},
destination: {
location: {
locationName: "Worcester Foregate Street",
crs: "WOF",
tiploc: "WORCSFS",
},
},
category: "OO",
activities: "T RM",
length: "4",
},
];

View File

@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */ /* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */ // "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
@ -25,9 +25,9 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */ /* Modules */
"module": "commonjs", /* Specify what module code is generated. */ "module": "commonjs" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */ // "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node16", /* Specify how TypeScript looks up a file from a given module specifier. */ "moduleResolution": "node16" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
@ -44,8 +44,8 @@
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */ // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */ /* JavaScript Support */
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ "allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */,
"checkJs": false, /* Enable error reporting in type-checked JavaScript files. */ "checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */ /* Emit */
@ -55,7 +55,7 @@
// "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. */ // "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. */ // "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. */ "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. */ // "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. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
@ -77,12 +77,12 @@
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
/* Type Checking */ /* Type Checking */
"strict": true, /* Enable all strict type-checking options. */ "strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
@ -106,10 +106,5 @@
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */
}, },
"include": [ "include": ["src", "test", "./*", "./config"]
"src",
"test",
"./*",
"./config"
]
} }