diff --git a/.eslintrc.js b/.eslintrc.js index e78c61e..eaa1a24 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,6 +1,7 @@ module.exports = { 'env': { - 'browser': true, + 'browser': false, + 'node': true, 'commonjs': true, 'es2021': true }, diff --git a/src/middlewares/auth.middlewares.js b/src/middlewares/auth.middlewares.js index 9cccc69..7960d23 100644 --- a/src/middlewares/auth.middlewares.js +++ b/src/middlewares/auth.middlewares.js @@ -15,7 +15,8 @@ module.exports = async function authCheck(req, res, next) { if (!result) { const err = new Error('Unauthorised'); err.status = 401; - log.out('authMiddlewares: Authentication attempted with incorrect key', 'warn'); + log.out('authMiddlewares: Authentication attempted with incorrect key', + 'warn'); return next(err); } else { log.out('authMiddlewares: User authenticated', 'dbug'); diff --git a/src/services/issue.services.js b/src/services/issue.services.js index 9af2ddb..7a50d4f 100644 --- a/src/services/issue.services.js +++ b/src/services/issue.services.js @@ -1,5 +1,4 @@ /* eslint-disable no-useless-escape */ -/* global process */ const axios = require('axios'); const log = require('../utils/log.utils'); @@ -20,12 +19,13 @@ async function sendToGitea(body) { } }; var res = await axios.post(url, body, opts); - // Need to read the output from the POST and pass the result upwards to the client. + /* Need to read the output from the POST and pass the result upwards to the + client.*/ if (res.status == 201) { log.out('issueService.sendToGitea: Issue sent to Gitea', 'info'); return {status: res.status,message:'issue created'}; } else { - log.out(`issueService.sendToGitea: Failed to send issue to Gitea: ${res.body}`, 'err'); + log.out(`issueService.sendToGitea: Fail to send issue: ${res.body}`, 'err'); return {status: res.status,message:'issue not created'}; } } diff --git a/src/services/ldb.services.js b/src/services/ldb.services.js index 7305d46..3e1b49e 100644 --- a/src/services/ldb.services.js +++ b/src/services/ldb.services.js @@ -1,4 +1,3 @@ -/* global process */ // Parse and return an LDB Request const log = require('../utils/log.utils'); // Log Helper @@ -27,12 +26,14 @@ async function get(id, staff=false){ } } catch (err) { log.out(`ldbService.get: Error, Unable to find CRS: ${err}`, 'info'); - return {ERROR:'NOT_FOUND',description:'The entered station was not found. Please check and try again.'}; + return { + ERROR:'NOT_FOUND', + description:'The entered station was not found.'}; } } async function arrDepBoard(CRS){ - log.out(`ldbService.arrDepBoard: Trying to fetch ArrDep Board for ${CRS}`, 'info'); + log.out(`ldbService.arrDepBoard: Trying to fetch board for ${CRS}`, 'info'); try { const options = { numRows: 10, @@ -42,12 +43,15 @@ async function arrDepBoard(CRS){ return await api.call('GetArrDepBoardWithDetails', options, false, false); } catch (err) { log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`, 'warn'); - return {GetStationBoardResult: 'not available', Reason: `The CRS code ${CRS} is not valid`, Why: 'Sometimes a station will have more than one CRS - for example Filton Abbey Wood has FIT and FAW however schedules are only available when looking up with FIT - this is how the National Rail Enquiries systems work.'}; + return { + GetStationBoardResult: 'not available', + Reason: `The CRS code ${CRS} is not valid` + }; } } async function arrDepBoardStaff(CRS) { - log.out(`ldbService.arrDepBoardStaff: Trying to fetch ArrDep Board for ${CRS}`, 'dbug'); + log.out(`ldbService.arrDepBoardStaff: Try to fetch board for ${CRS}`, 'dbug'); try { const options = { numRows: 25, @@ -58,7 +62,10 @@ async function arrDepBoardStaff(CRS) { return await api.call('GetArrDepBoardWithDetails', options, false, false); } catch (err) { log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}, "warn`); - return {GetStationBoardResult: 'not available', Reason: `The CRS code ${CRS} is not valid`, Why: 'Sometimes a station will have more than one CRS - for example Filton Abbey Wood has FIT and FAW however schedules are only available when looking up with FIT - this is how the National Rail Enquiries systems work.'}; + return { + GetStationBoardResult: 'not available', + Reason: `The CRS code ${CRS} is not valid` + }; } } diff --git a/src/services/mail.services.js b/src/services/mail.services.js index eb890ef..20d711f 100644 --- a/src/services/mail.services.js +++ b/src/services/mail.services.js @@ -1,4 +1,3 @@ -/* global process */ const log = require('../utils/log.utils'); const mail = require('nodemailer'); //>> Probs wrong @@ -18,7 +17,9 @@ let transporter = mail.createTransport({ } }); -async function send(message){ // message is an object containing strings for: *to, cc, bcc, *subject, *txt, html (* denotes required) +/* 'message' is an object containing string values for: + *to, cc, bcc, *subject, *txt, html. * denotes required */ +async function send(message) { log.out('mailServices.send: Message send request received', 'info'); message.from = fromAddr; try { diff --git a/src/services/registration.services.js b/src/services/registration.services.js index f640869..6754bb7 100644 --- a/src/services/registration.services.js +++ b/src/services/registration.services.js @@ -8,8 +8,8 @@ const errors = require('../configs/errorCodes.configs'); async function createRegKey(body) { log.out('registerServices.createRegKey: Incoming request', 'INFO'); - const domain = await clean.getDomainFromEmail(body.email); // The function should validate the email - log.out(`registerServices: New registration request from domain: ${domain}`, 'info'); + const domain = await clean.getDomainFromEmail(body.email); + log.out(`registerServices: Registration request from: ${domain}`, 'info'); if (domains.includes(domain)) { log.out(`registerServices.createRegKey: Key from valid domain: ${domain}`); const uuid = await auth.generateKey(); @@ -17,7 +17,7 @@ async function createRegKey(body) { const message = await auth.generateConfirmationEmail(body.email, uuid); if (!message) { const err = new Error('Message generation error'); - log.out('registerServices.createRegKey: Error generating registration email', 'err'); + log.out('registerServices.createRegKey: Error generating email', 'err'); log.out(err, 'err'); return 500; } @@ -31,9 +31,9 @@ async function createRegKey(body) { async function regUser(req) { // Add input validation log.out(`Read UUID: ${req.uuid}`, 'dbug'); - log.out(`registrationServices.regUser: Checking validity of : ${req.uuid}`, 'info'); + log.out(`registrationServices.regUser: Request from: ${req.uuid}`, 'info'); const res = await auth.checkRequest(req.uuid); - log.out(`registrationServices.regUser: checkRequest returned: ${JSON.stringify(res)}`, 'info'); + log.out(`registrationServices.regUser: ${JSON.stringify(res)}`, 'info'); if (res.result) { const uuid = await auth.generateKey(); const apiKey = await db.addUser(uuid, res.domain); diff --git a/src/services/trainService.services.js b/src/services/trainService.services.js index 23c304d..1ec4259 100644 --- a/src/services/trainService.services.js +++ b/src/services/trainService.services.js @@ -5,7 +5,8 @@ const pis = require('../services/pis.services'); async function findByHeadcodeToday(headcode) { const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); - log.out(`trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}`, 'dbug'); + log.out('trainServiceServices.findByHeadcode: Searching for headcode ' + + sanitizedHeadcode, 'dbug'); const now = new Date(); const dayMap = ['su', 'm', 't', 'w', 'th', 'f', 's']; const shortDay = dayMap[now.getDay()]; // Fetch short day from map @@ -40,6 +41,8 @@ module.exports = { /* Internal Functions, not to be exported */ +/* Accepts the 'stops' array from a db query and produces an + array of only public stops as TIPLOCs. */ async function getPublicStops(data) { let tiplocList = []; for (const publicStop in data) { @@ -50,7 +53,10 @@ async function getPublicStops(data) { return tiplocList; } -async function parseTrains(data) { // Takes a single days data from a headcode query and returns only relevant services +/* Takes a single days data from a headcode query and requeries + using the trainUid, required to ensure any cancellations are + accounted for */ +async function parseTrains(data) { let trainUids = []; for (const i of data) { const trainUid = i['trainUid']; @@ -66,7 +72,10 @@ async function parseTrains(data) { // Takes a single days data from a headcode q return parsedData; } -async function findByTrainUid(uid, date = new Date()) { // Date defaults to today +/* Queries using a trainUid and filters all schedules that + have been over-ridden by overlays, cancellations and new + schedules. */ +async function findByTrainUid(uid, date = new Date()) { const query = { trainUid: uid, scheduleStartDate: {$lte: date}, @@ -109,17 +118,21 @@ async function findByTrainUid(uid, date = new Date()) { // Date defaults to toda if (stpIndicator === 'C') { return; - //continue; } - if (stpIndicator === 'N' && !thisStpIndicators.hasC) { + if (stpIndicator === 'N' && + !thisStpIndicators.hasC) { return serviceDetail; - //preparedData.push(serviceDetail); - } else if (stpIndicator === 'O' && !thisStpIndicators.hasC && !thisStpIndicators.hasN) { + + } else if (stpIndicator === 'O' && + !thisStpIndicators.hasC && + !thisStpIndicators.hasN) { return serviceDetail; - //preparedData.push(serviceDetail); - } else if (stpIndicator === 'P' && !thisStpIndicators.hasC && !thisStpIndicators.hasN && !thisStpIndicators.hasO) { + + } else if (stpIndicator === 'P' && + !thisStpIndicators.hasC && + !thisStpIndicators.hasN && + !thisStpIndicators.hasO) { return serviceDetail; - //preparedData.push(serviceDetail); } } return preparedData; diff --git a/src/utils/auth.utils.js b/src/utils/auth.utils.js index 6b9e6ef..c32c417 100644 --- a/src/utils/auth.utils.js +++ b/src/utils/auth.utils.js @@ -8,7 +8,8 @@ const minify = require('../utils/minify.utils'); async function isAuthed(uuid) { // Needs testing const q = {uuid: uuid}; const res = await db.query('users', q); - log.out(`authUtils.checkUser: DB Query answer: ${JSON.stringify(res[0])}`, 'dbug'); + log.out('authUtils.checkUser: DB Query answer: ' + + JSON.stringify(res[0]), 'dbug'); const authorized = res && res[0] && res[0].domain; if (authorized) db.userAtime(uuid); return authorized; @@ -19,7 +20,8 @@ async function checkRequest(key) { const collection = 'registrations'; const query = {uuid: key}; const res = await db.query(collection, query); - log.out(`authUtils.checkRequest: DB Query result: ${JSON.stringify(res)}`, 'dbug'); + log.out('authUtils.checkRequest: DB Query result: ' + + JSON.stringify(res), 'dbug'); const result = res.length > 0 && res[0].time ? { result: true, domain: res[0].domain } : { result: false }; @@ -44,7 +46,8 @@ async function generateConfirmationEmail(eml, uuid) { html: htmlMin }; } catch(err) { - log.out('mailServices.generateConfirmationEmail: Error reading templates, $(err)', 'err'); + log.out('mailServices.generateConfirmationEmail: Error reading template, ' + + err, 'err'); return false; } } diff --git a/src/utils/ldb.utils.js b/src/utils/ldb.utils.js index 1f27d5b..0395aa6 100644 --- a/src/utils/ldb.utils.js +++ b/src/utils/ldb.utils.js @@ -4,7 +4,6 @@ const san = require('../utils/sanitizer.utils'); // Sanitiser async function checkCrs(input){ var INPUT = input.toUpperCase(); - log.out(`ldbUtils.checkCrs: Building database query to find: '${INPUT}'`, 'info'); var query = { '$or':[ {'3ALPHA':INPUT}, @@ -13,11 +12,13 @@ async function checkCrs(input){ ] }; var result = await db.query('stations', query); - log.out(`ldbUtils.checkCrs: Query results: ${JSON.stringify(result)}`, 'dbug'); + log.out('ldbUtils.checkCrs: Query results: ' + + JSON.stringify(result), 'dbug'); return result; } -async function cleanMessages(input){ // Needs to be moved to the frontend `ensureArray() func` +// Needs to be moved to the frontend `ensureArray() func` +async function cleanMessages(input){ var out = []; if (typeof input.message == 'string') { out.push(await san.cleanNrcc(input.message)); @@ -30,7 +31,8 @@ async function cleanMessages(input){ // Needs to be moved to the frontend `ensur } // Accepts an object but not an Array and returns it wrapped in an array. -async function cleanServices(input){ // Need to triple check but I don't think this is used anymore. +// Need to triple check but I don't think this is used anymore. +async function cleanServices(input){ var out = []; if (!Array.isArray(input)) { log.out(`ldbUtils.cleanServices: Transforming input: ${input}`, 'depr'); diff --git a/src/utils/log.utils.js b/src/utils/log.utils.js index 5f5068d..a741d0f 100644 --- a/src/utils/log.utils.js +++ b/src/utils/log.utils.js @@ -1,10 +1,10 @@ -/* global process */ const environment = process.env.NODE_ENV; const hideInProduction = ['info', 'dbug']; async function out(msg, level = 'othr') { - if (environment === 'production' && hideInProduction.includes(level.toLowerCase())) { + if (environment === 'production' && + hideInProduction.includes(level.toLowerCase())) { return; } else { const time = new Date().toISOString(); diff --git a/src/utils/sanitizer.utils.js b/src/utils/sanitizer.utils.js index a2fb3a5..9b7d99b 100644 --- a/src/utils/sanitizer.utils.js +++ b/src/utils/sanitizer.utils.js @@ -1,10 +1,10 @@ //const log = require('../utils/log.utils'); -function removeNonAlphanumeric(inputString) { // Should be able to replace sanitizer module +function removeNonAlphanumeric(inputString) { return inputString.replace(/[^a-zA-Z0-9]/g, ''); } -function removeNonAlpha(inputString) { // Should be able to replace sanitizer module +function removeNonAlpha(inputString) { return inputString.replace(/[^a-zA-Z]/g, ''); }