From c8eb5375733372d68fd4a3eeb524d705dd10980d Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 20:53:52 +0100 Subject: [PATCH 01/36] Rename 'transformers' folder to 'processors' Signed-off-by: Fred Boniface --- src/services/ldb.services.js | 2 +- src/utils/{translators => processors}/README.md | 0 src/utils/{translators => processors}/ldb/staffStation.ts | 0 test/utils/translators/ldb/staffStation.utils.test.ts | 2 +- 4 files changed, 2 insertions(+), 2 deletions(-) rename src/utils/{translators => processors}/README.md (100%) rename src/utils/{translators => processors}/ldb/staffStation.ts (100%) diff --git a/src/services/ldb.services.js b/src/services/ldb.services.js index 5b3e417..5b2762f 100644 --- a/src/services/ldb.services.js +++ b/src/services/ldb.services.js @@ -7,7 +7,7 @@ const db = require("../services/dbAccess.services"); import { logger } from "../utils/logger.utils"; -import { transform as staffStationTransform } from "../utils/translators/ldb/staffStation"; +import { transform as staffStationTransform } from "../utils/processors/ldb/staffStation"; import { msgCodes } from "../configs/errorCodes.configs"; const ldbKey = process.env.OWL_LDB_KEY; diff --git a/src/utils/translators/README.md b/src/utils/processors/README.md similarity index 100% rename from src/utils/translators/README.md rename to src/utils/processors/README.md diff --git a/src/utils/translators/ldb/staffStation.ts b/src/utils/processors/ldb/staffStation.ts similarity index 100% rename from src/utils/translators/ldb/staffStation.ts rename to src/utils/processors/ldb/staffStation.ts diff --git a/test/utils/translators/ldb/staffStation.utils.test.ts b/test/utils/translators/ldb/staffStation.utils.test.ts index b5dd0b5..997c64a 100644 --- a/test/utils/translators/ldb/staffStation.utils.test.ts +++ b/test/utils/translators/ldb/staffStation.utils.test.ts @@ -1,7 +1,7 @@ import { transform, calculateLength, -} from "../../../../src/utils/translators/ldb/staffStation"; +} from "../../../../src/utils/processors/ldb/staffStation"; import { inputs } from "./stationInputs"; import { outputs } from "./stationOutputs"; -- 2.34.1 From 34a1b5bf9a00cc5dc3b062cc513c4d96e60835b8 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 20:54:04 +0100 Subject: [PATCH 02/36] Add file for timetableProcessor Signed-off-by: Fred Boniface --- src/utils/processors/timetable/timetableProcessor.utils.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/utils/processors/timetable/timetableProcessor.utils.ts diff --git a/src/utils/processors/timetable/timetableProcessor.utils.ts b/src/utils/processors/timetable/timetableProcessor.utils.ts new file mode 100644 index 0000000..e69de29 -- 2.34.1 From 7d51bc002bd72b8c7d25e8e1ebcfc3ca0b41183e Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 20:58:11 +0100 Subject: [PATCH 03/36] Remove route declarations for old API version and update names of current route files Signed-off-by: Fred Boniface --- app.js | 22 +++++------------- src/routes/find.routes.ts | 23 ------------------- src/routes/issue.routes.ts | 7 ------ src/routes/kube.routes.ts | 9 -------- src/routes/ldb.routes.ts | 19 --------------- src/routes/ldbs.routes.ts | 22 ------------------ src/routes/list.routes.ts | 20 ---------------- .../{live2.routes.ts => live.routes.ts} | 0 src/routes/pis.routes.ts | 8 +++---- src/routes/pis2.routes.ts | 9 -------- src/routes/{ref2.routes.ts => ref.routes.ts} | 0 src/routes/registration.routes.ts | 8 ------- src/routes/stats.routes.ts | 7 ------ ...metable2.routes.ts => timetable.routes.ts} | 0 .../{user2.routes.ts => user.routes.ts} | 0 15 files changed, 10 insertions(+), 144 deletions(-) delete mode 100644 src/routes/find.routes.ts delete mode 100644 src/routes/issue.routes.ts delete mode 100644 src/routes/kube.routes.ts delete mode 100644 src/routes/ldb.routes.ts delete mode 100644 src/routes/ldbs.routes.ts delete mode 100644 src/routes/list.routes.ts rename src/routes/{live2.routes.ts => live.routes.ts} (100%) delete mode 100644 src/routes/pis2.routes.ts rename src/routes/{ref2.routes.ts => ref.routes.ts} (100%) delete mode 100644 src/routes/registration.routes.ts delete mode 100644 src/routes/stats.routes.ts rename src/routes/{timetable2.routes.ts => timetable.routes.ts} (100%) rename src/routes/{user2.routes.ts => user.routes.ts} (100%) diff --git a/app.js b/app.js index 4925e7a..fc95609 100644 --- a/app.js +++ b/app.js @@ -19,23 +19,13 @@ const cors = require("cors"); const authenticate = require("./src/middlewares/auth.middlewares"); // Internal Requires -const version = require("./src/configs/version.configs"); /* -{}{}{} REMOVE THESE FILES - THEY ARE NO LONGER REQUIRED - {}{}{} -const listRtr = require("./src/routes/list.routes"); -const ldbRtr = require("./src/routes/ldb.routes"); -const ldbsRtr = require("./src/routes/ldbs.routes"); -const kubeRtr = require("./src/routes/kube.routes"); -const findRtr = require("./src/routes/find.routes"); -const issueRtr = require("./src/routes/issue.routes"); -const statRtr = require("./src/routes/stats.routes"); -const regRtr = require("./src/routes/registration.routes"); -const pisRtr = require("./src/routes/pis.routes");*/ +const version = require("./src/configs/version.configs"); const trainRtr = require("./src/routes/train.routes"); -const pis2Rtr = require("./src/routes/pis2.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 tt2Rtr = require("./src/routes/timetable2.routes"); // API Version 2 -const user2Rtr = require("./src/routes/user2.routes"); // API Version 2 Routes +const pis2Rtr = require("./src/routes/pis.routes"); // API Version 2 Routes +const ref2Rtr = require("./src/routes/ref.routes"); // API Version 2 Routes +const live2Rtr = require("./src/routes/live.routes"); // API Version 2 Routes +const tt2Rtr = require("./src/routes/timetable.routes"); // API Version 2 +const user2Rtr = require("./src/routes/user.routes"); // API Version 2 Routes const miscRtr = require("./src/routes/misc.routes"); // Non-Public API Routes // Set Server Configurations diff --git a/src/routes/find.routes.ts b/src/routes/find.routes.ts deleted file mode 100644 index e8e572f..0000000 --- a/src/routes/find.routes.ts +++ /dev/null @@ -1,23 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const findController = require("../controllers/find.controllers"); - -/* GET programming languages. */ -//router.get('/', programmingLanguagesController.get); - -/* POST programming language */ -//router.post('/', programmingLanguagesController.create); - -/* PUT programming language */ -//router.put('/:id', programmingLanguagesController.update); - -/* DELETE programming language */ -//router.delete('/:id', programmingLanguagesController.remove); - -router.get("/name/:id", findController.findName); -router.get("/crs/:id", findController.findCrs); -router.get("/nlc/:id", findController.findNlc); -router.get("/tiploc/:id", findController.findTiploc); -router.get("/stanox/:id", findController.findStanox); - -module.exports = router; diff --git a/src/routes/issue.routes.ts b/src/routes/issue.routes.ts deleted file mode 100644 index cc5023e..0000000 --- a/src/routes/issue.routes.ts +++ /dev/null @@ -1,7 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const issueController = require("../controllers/issue.controllers"); - -router.post("/", issueController.post); - -module.exports = router; diff --git a/src/routes/kube.routes.ts b/src/routes/kube.routes.ts deleted file mode 100644 index 1fd2747..0000000 --- a/src/routes/kube.routes.ts +++ /dev/null @@ -1,9 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const kubeController = require("../controllers/kube.controllers"); - -router.get("/alive", kubeController.getAlive); -router.get("/ready", kubeController.getReady); -router.get("/time", kubeController.getTime); - -module.exports = router; diff --git a/src/routes/ldb.routes.ts b/src/routes/ldb.routes.ts deleted file mode 100644 index 8d4587e..0000000 --- a/src/routes/ldb.routes.ts +++ /dev/null @@ -1,19 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const ldbController = require("../controllers/ldb.controllers"); - -/* GET programming languages. */ -//router.get('/', programmingLanguagesController.get); - -/* POST programming language */ -//router.post('/', programmingLanguagesController.create); - -/* PUT programming language */ -//router.put('/:id', programmingLanguagesController.update); - -/* DELETE programming language */ -//router.delete('/:id', programmingLanguagesController.remove); - -router.get("/:id", ldbController.get); - -module.exports = router; diff --git a/src/routes/ldbs.routes.ts b/src/routes/ldbs.routes.ts deleted file mode 100644 index dd24ca3..0000000 --- a/src/routes/ldbs.routes.ts +++ /dev/null @@ -1,22 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const ldbsController = require("../controllers/ldbs.controllers"); - -/* GET programming languages. */ -//router.get('/', programmingLanguagesController.get); - -/* POST programming language */ -//router.post('/', programmingLanguagesController.create); - -/* PUT programming language */ -//router.put('/:id', programmingLanguagesController.update); - -/* DELETE programming language */ -//router.delete('/:id', programmingLanguagesController.remove); - -router.get("/arrdep/:id", ldbsController.get); -router.get("/reasonCode", ldbsController.getReasonCodeList); -router.get("/reasonCode/:code", ldbsController.getReasonCode); -router.get("/service/rid/:rid", ldbsController.getTrainByRID); - -module.exports = router; diff --git a/src/routes/list.routes.ts b/src/routes/list.routes.ts deleted file mode 100644 index ba48211..0000000 --- a/src/routes/list.routes.ts +++ /dev/null @@ -1,20 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const listController = require("../controllers/list.controllers"); - -/* GET programming languages. */ -//router.get('/', programmingLanguagesController.get); - -/* POST programming language */ -//router.post('/', programmingLanguagesController.create); - -/* PUT programming language */ -//router.put('/:id', programmingLanguagesController.update); - -/* DELETE programming language */ -//router.delete('/:id', programmingLanguagesController.remove); - -router.get("/stations", listController.getStations); -router.get("/corpus", listController.getCorpus); - -module.exports = router; diff --git a/src/routes/live2.routes.ts b/src/routes/live.routes.ts similarity index 100% rename from src/routes/live2.routes.ts rename to src/routes/live.routes.ts diff --git a/src/routes/pis.routes.ts b/src/routes/pis.routes.ts index d0f72c2..6a9aee8 100644 --- a/src/routes/pis.routes.ts +++ b/src/routes/pis.routes.ts @@ -1,9 +1,9 @@ const express = require("express"); const router = express.Router(); -const pisController = require("../controllers/pis.controllers"); +const pisCtr = require("../controllers/pis.controllers"); -router.get("/origdest/:start/:end", pisController.byOrigDest); -router.get("/code/:code", pisController.byCode); -router.get("/code/random", pisController.random); +// PIS +router.get("/byCode/:code", pisCtr.byCode); +router.get("/byStartEnd/:startCrs/:endCrs", pisCtr.byStartEndCRS); module.exports = router; diff --git a/src/routes/pis2.routes.ts b/src/routes/pis2.routes.ts deleted file mode 100644 index 6a9aee8..0000000 --- a/src/routes/pis2.routes.ts +++ /dev/null @@ -1,9 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const pisCtr = require("../controllers/pis.controllers"); - -// PIS -router.get("/byCode/:code", pisCtr.byCode); -router.get("/byStartEnd/:startCrs/:endCrs", pisCtr.byStartEndCRS); - -module.exports = router; diff --git a/src/routes/ref2.routes.ts b/src/routes/ref.routes.ts similarity index 100% rename from src/routes/ref2.routes.ts rename to src/routes/ref.routes.ts diff --git a/src/routes/registration.routes.ts b/src/routes/registration.routes.ts deleted file mode 100644 index ac0d4bc..0000000 --- a/src/routes/registration.routes.ts +++ /dev/null @@ -1,8 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const regController = require("../controllers/registration.controllers"); - -router.post("/request", regController.request); -router.post("/register", regController.register); - -module.exports = router; diff --git a/src/routes/stats.routes.ts b/src/routes/stats.routes.ts deleted file mode 100644 index 013e5f1..0000000 --- a/src/routes/stats.routes.ts +++ /dev/null @@ -1,7 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const statsController = require("../controllers/stats.controllers"); - -router.get("/", statsController.get); - -module.exports = router; diff --git a/src/routes/timetable2.routes.ts b/src/routes/timetable.routes.ts similarity index 100% rename from src/routes/timetable2.routes.ts rename to src/routes/timetable.routes.ts diff --git a/src/routes/user2.routes.ts b/src/routes/user.routes.ts similarity index 100% rename from src/routes/user2.routes.ts rename to src/routes/user.routes.ts -- 2.34.1 From 2c482aea6aca62a6288c802c69f48ea0f91a3950 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 21:00:48 +0100 Subject: [PATCH 04/36] Remove remaining unused routes Signed-off-by: Fred Boniface --- app.js | 12 ------------ src/routes/train.routes.ts | 19 ------------------- 2 files changed, 31 deletions(-) delete mode 100644 src/routes/train.routes.ts diff --git a/app.js b/app.js index fc95609..3e8e348 100644 --- a/app.js +++ b/app.js @@ -72,25 +72,13 @@ app.use("/api/v2/timetable", tt2Rtr); // API Version 2 app.use("/api/v2/user", user2Rtr); // API Version 2 app.use("/misc", miscRtr); // Non public-api endpoints (Stats, Issue, etc.) -// Unauthenticated Routes -//app.use("/api/v1/list", listRtr); - Remove v1 List API - list.routes -//app.use("/api/v1/ldb", ldbRtr); - Remove v1 LDB API -//app.use("/api/v1/kube", kubeRtr); - Remove v1 Kube API -//app.use("/api/v1/find", findRtr); - Remove v1 Find API -//app.use("/api/v1/issue", issueRtr); - Remove v1 Issue API -//app.use("/api/v1/stats", statRtr); - Remove v1 Stats API -//app.use("/api/v1/register", regRtr); - Remove v1 Register API -// Authented Routes -//app.use("/api/v1/ldbs", authenticate, ldbsRtr); - Remove v1 LDBS API -//app.use("/api/v1/pis", authenticate, pisRtr); - Remove v1 PIS API app.use("/api/v1/auth/test", authenticate, (req, res) => res.status(200).json({ status: "ok", message: "Authentication successful", }) ); // Returns 401 if auth failed, 200 if successful. -app.use("/api/v1/train", authenticate, trainRtr); // Number of proxies: app.set("trust proxy", 4); diff --git a/src/routes/train.routes.ts b/src/routes/train.routes.ts deleted file mode 100644 index 02969b7..0000000 --- a/src/routes/train.routes.ts +++ /dev/null @@ -1,19 +0,0 @@ -const express = require("express"); -const router = express.Router(); -const trainController = require("../controllers/train.controllers"); - -/* GET programming languages. */ -//router.get('/', programmingLanguagesController.get); - -/* POST programming language */ -//router.post('/', programmingLanguagesController.create); - -/* PUT programming language */ -//router.put('/:id', programmingLanguagesController.update); - -/* DELETE programming language */ -//router.delete('/:id', programmingLanguagesController.remove); - -router.get("/headcode/today/:id", trainController.getByHeadcodeToday); - -module.exports = router; -- 2.34.1 From 9e6706d109142e26fe2fb65bde9b608ef8937d47 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 21:02:21 +0100 Subject: [PATCH 05/36] Bump @owlboard/ts-types version Signed-off-by: Fred Boniface --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b467a7e..a8d3462 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.0.9", + "@owlboard/ts-types": "^0.1.0", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", -- 2.34.1 From 9674ad9392342eec8f268de8f795cdadfd0b2e7f Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 21:06:30 +0100 Subject: [PATCH 06/36] Remove unused code from controllers Signed-off-by: Fred Boniface --- src/controllers/find.controllers.js | 58 ---------------------------- src/controllers/kube.controllers.js | 35 ----------------- src/controllers/ldb.controllers.js | 13 ------- src/controllers/ldbs.controllers.js | 51 ------------------------ src/controllers/list.controllers.js | 37 ------------------ src/controllers/pis.controllers.js | 16 -------- src/controllers/stats.controllers.js | 11 ------ 7 files changed, 221 deletions(-) delete mode 100644 src/controllers/find.controllers.js delete mode 100644 src/controllers/kube.controllers.js delete mode 100644 src/controllers/ldbs.controllers.js delete mode 100644 src/controllers/list.controllers.js diff --git a/src/controllers/find.controllers.js b/src/controllers/find.controllers.js deleted file mode 100644 index 2c72e78..0000000 --- a/src/controllers/find.controllers.js +++ /dev/null @@ -1,58 +0,0 @@ -const find = require("../services/find.services"); - -async function findName(req, res, next) { - try { - var id = req.params.id; - res.json(await find.name(id)); - } catch (err) { - console.error("Unknown Error", err.message); - next(err); - } -} - -async function findCrs(req, res, next) { - try { - var id = req.params.id; - res.json(await find.crs(id)); - } catch (err) { - console.error("Unknown Error", err.message); - next(err); - } -} - -async function findNlc(req, res, next) { - try { - var id = req.params.id; - res.json(await find.nlc(id)); - } catch (err) { - console.error("Unknown Error", err.message); - next(err); - } -} - -async function findTiploc(req, res, next) { - try { - var id = req.params.id; - res.json(await find.tiploc(id)); - } catch (err) { - console.error("Unknown Error", err.message); - next(err); - } -} - -async function findStanox(req, res, next) { - try { - var id = req.params.id; - res.json(await find.stanox(id)); - } catch (err) { - console.error("Unknown Error", err.message); - next(err); - } -} -module.exports = { - findName, - findCrs, - findNlc, - findTiploc, - findStanox, -}; diff --git a/src/controllers/kube.controllers.js b/src/controllers/kube.controllers.js deleted file mode 100644 index f448bb6..0000000 --- a/src/controllers/kube.controllers.js +++ /dev/null @@ -1,35 +0,0 @@ -const kube = require("../services/kube.services"); - -async function getAlive(req, res, next) { - try { - var state = kube.getAlive(); - res.status((await state).code).send((await state).state); - } catch (err) { - res.status("503").send({ state: "error" }); - } -} - -async function getReady(req, res, next) { - try { - res.json(await kube.getReady(req.body)); - } catch (err) { - console.error("Unknown Error", err.message); - next(err); - } -} - -async function getTime(req, res, next) { - try { - res.json(await kube.getTime(req.body)); - } catch (err) { - console.error("Unknown Error", err.message); - err.status = 503; - next(err); - } -} - -module.exports = { - getAlive, - getReady, - getTime, -}; diff --git a/src/controllers/ldb.controllers.js b/src/controllers/ldb.controllers.js index bbafed3..9dc04b2 100644 --- a/src/controllers/ldb.controllers.js +++ b/src/controllers/ldb.controllers.js @@ -1,17 +1,5 @@ const ldb = require("../services/ldb.services"); -async function get(req, res, next) { - // API v1 only - try { - var id = req.params.id; - res.json(await ldb.get(id)); - } catch (err) { - console.error("Unknown Error", err.message); - err.status = 500; - next(err); - } -} - async function getTrain(req, res, next) { // API v2 Only if (!req.isAuthed) { @@ -66,7 +54,6 @@ async function getStation(req, res, next) { } module.exports = { - get, getTrain, getStation, }; diff --git a/src/controllers/ldbs.controllers.js b/src/controllers/ldbs.controllers.js deleted file mode 100644 index 47142c4..0000000 --- a/src/controllers/ldbs.controllers.js +++ /dev/null @@ -1,51 +0,0 @@ -const ldb = require("../services/ldb.services"); - -async function get(req, res, next) { - try { - var id = req.params.id; - res.json(await ldb.get(id, true)); - } catch (err) { - console.error("Unknown Error", err.message); - err.status = 500; - next(err); - } -} - -async function getReasonCodeList(req, res, next) { - try { - res.json(await ldb.getReasonCodeList()); - } catch (err) { - console.error("ERROR", err.message); - err.status = 500; - next(err); - } -} - -async function getReasonCode(req, res, next) { - try { - const code = req.params.code; - res.json(await ldb.getReasonCode(code)); - } catch (err) { - console.error("ERROR", err.message); - err.status = 500; - next(err); - } -} - -async function getTrainByRID(req, res, next) { - try { - const rid = req.params.rid; - res.json(await ldb.getServiceByRID(rid)); - } catch (err) { - console.error("ERROR", err); - err.status = 500; - next(err); - } -} - -module.exports = { - get, - getReasonCodeList, - getReasonCode, - getTrainByRID, -}; diff --git a/src/controllers/list.controllers.js b/src/controllers/list.controllers.js deleted file mode 100644 index 0119e48..0000000 --- a/src/controllers/list.controllers.js +++ /dev/null @@ -1,37 +0,0 @@ -const list = require("../services/list.services"); - -async function getStations(req, res, next) { - try { - res.json(await list.getStations(req.body)); - } catch (err) { - console.error("Controller Error", err.message); - err.status = 500; - next(err); - } -} - -async function getCorpus(req, res, next) { - try { - res.json(await list.getCorpus(req.body)); - } catch (err) { - console.error("Controller Error", err.message); - err.status = 500; - next(err); - } -} - -async function hits(req, res, next) { - try { - res.json(await list.hits()); - } catch (err) { - console.error("Controller Error", err); - err.status = 500; - next(err); - } -} - -module.exports = { - getStations, - getCorpus, - hits, -}; diff --git a/src/controllers/pis.controllers.js b/src/controllers/pis.controllers.js index 2e8c2d5..3e4743b 100644 --- a/src/controllers/pis.controllers.js +++ b/src/controllers/pis.controllers.js @@ -1,21 +1,5 @@ const pis = require("../services/pis.services"); -async function byOrigDest(req, res, next) { - if (!req.isAuthed) { - const err = new Error("Unauthorized"); - err.status = 401; - throw err; - } - try { - let start = req.params.start; - let end = req.params.end; - res.json(await pis.findPisByOrigDest(start, end)); - } catch (err) { - console.error("Unknown Error", err.message); - next(err); - } -} - /* Used in /api/v2 */ async function byStartEndCRS(req, res, next) { if (!req.isAuthed) { diff --git a/src/controllers/stats.controllers.js b/src/controllers/stats.controllers.js index add4894..17181f3 100644 --- a/src/controllers/stats.controllers.js +++ b/src/controllers/stats.controllers.js @@ -1,16 +1,5 @@ const stat = require("../services/stats.services"); -async function get(req, res, next) { - // API V1 - try { - res.json(await stat.hits()); - } catch (err) { - console.error("Controller Error", err); - err.status = 500; - next(err); - } -} - async function versions(req, res, next) { // API v2 try { -- 2.34.1 From 59a75f811d32c4e211ba7e6fcf1a15e5832ce298 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 21:56:11 +0100 Subject: [PATCH 07/36] Disable auth for development Signed-off-by: Fred Boniface --- package.json | 1 + src/middlewares/auth.middlewares.js | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/package.json b/package.json index a8d3462..d90fe5e 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "scripts": { "build": "tsc", "run": "tsc && node dist/app.js", + "dev": "NODE_ENV=development tsc && node dist/app.js", "start": "node app.js", "test": "jest", "format": "npx prettier -w ." diff --git a/src/middlewares/auth.middlewares.js b/src/middlewares/auth.middlewares.js index 9d021c8..2e524ac 100644 --- a/src/middlewares/auth.middlewares.js +++ b/src/middlewares/auth.middlewares.js @@ -4,6 +4,11 @@ const logger = require("../utils/logger.utils"); module.exports = async function authCheck(req, res, next) { //log.out("authMiddlewares: Checking authentication", "dbug"); logger.logger.debug("Auth check starting"); + if (process.env.NODE_ENV === "development") { + logger.logger.warn("DEVELOPMENT MODE - AUTHENTICATION DISABLED") + res.isAuthed = true; + return next() + } try { var uuid = req.headers.uuid; } catch (err) { -- 2.34.1 From 7503722d84cf379cada837c841cdafff692c2503 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 21:56:46 +0100 Subject: [PATCH 08/36] Prettier Signed-off-by: Fred Boniface --- package-lock.json | 8 ++++---- src/controllers/pis.controllers.js | 1 - src/controllers/stats.controllers.js | 1 - src/utils/processors/ldb/staffStation.ts | 5 ++++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index fb06bf7..c94f6c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.0.9", + "@owlboard/ts-types": "^0.1.0", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", @@ -1983,9 +1983,9 @@ } }, "node_modules/@owlboard/ts-types": { - "version": "0.0.9", - "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.0.9/ts-types-0.0.9.tgz", - "integrity": "sha512-kdystFqrfj6SF8jchv4OoaBIFfJF4YoM7m09POZfuEWrdj69IXZG41k0cfYadft1wZs8L6mcZIunu8t/S5Q82Q==", + "version": "0.1.0", + "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.0/ts-types-0.1.0.tgz", + "integrity": "sha512-5dLkm1wEimiv+pr5wuJbJvPfSek9WuApl/4SBwoFXVO2bmAIktFIHi+bznXKnhW9a7nJwX/w+bVwwJpBOI3I1g==", "dev": true, "license": "GPL-3.0-or-later" }, diff --git a/src/controllers/pis.controllers.js b/src/controllers/pis.controllers.js index 3e4743b..bdc1e65 100644 --- a/src/controllers/pis.controllers.js +++ b/src/controllers/pis.controllers.js @@ -48,7 +48,6 @@ async function random(req, res, next) { } module.exports = { - byOrigDest, byStartEndCRS, byCode, random, diff --git a/src/controllers/stats.controllers.js b/src/controllers/stats.controllers.js index 17181f3..327b080 100644 --- a/src/controllers/stats.controllers.js +++ b/src/controllers/stats.controllers.js @@ -23,7 +23,6 @@ async function statistics(req, res, next) { } module.exports = { - get, versions, statistics, }; diff --git a/src/utils/processors/ldb/staffStation.ts b/src/utils/processors/ldb/staffStation.ts index ba63e17..afa7a01 100644 --- a/src/utils/processors/ldb/staffStation.ts +++ b/src/utils/processors/ldb/staffStation.ts @@ -111,7 +111,10 @@ function transformTrainServices(input: any): TrainServices[] { function checkIsSupressed(service: TrainServices): string | undefined { logger.trace("utils/translators/ldb/staffStation.checkIsSupressed: Running"); - if (service.serviceIsSupressed === "true" || service.isPassengerService === "false") { + if ( + service.serviceIsSupressed === "true" || + service.isPassengerService === "false" + ) { return "true"; } else { return undefined; -- 2.34.1 From 45fdf1904d3ef259485c1c54f1a47206e4298024 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 21:56:58 +0100 Subject: [PATCH 09/36] Add pipeline for partial PIS match Signed-off-by: Fred Boniface --- app.js | 1 - src/services/pis.services.ts | 54 ++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index 3e8e348..2485165 100644 --- a/app.js +++ b/app.js @@ -72,7 +72,6 @@ app.use("/api/v2/timetable", tt2Rtr); // 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("/api/v1/auth/test", authenticate, (req, res) => res.status(200).json({ status: "ok", diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index b974cf3..e9b3449 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -4,6 +4,7 @@ const db = require("../services/dbAccess.services"); const clean = require("../utils/sanitizer.utils"); import { logger } from "../utils/logger.utils"; +import { queryAggregate } from "./dbAccess.services"; const supported = ["GW", "UK"]; @@ -47,19 +48,66 @@ async function findByTiplocArray(tiplocArray: string[]) { }; const res = await db.query("pis", query); // If res is empty then try to find partial match - /* if (res.length) { + if (res.length) { return res; } else { let partial = await findPartialMatchByTiploc(tiplocArray); return partial - } */ - return await res; + } } async function findPartialMatchByTiploc(tiplocArray: string[]) { // Do some magic here, similar to findPisByOrigDest but // ensuring that the stops in the array match the last // x number of items in the array. + + const pipeline = [ + { + $addFields: { + reversedStops: { + $reverseArray: "$stops", + }, + reversedQueryArray: { + $reverseArray: tiplocArray, + }, + }, + }, + { + $match: { + $expr: { + $gt: [ + { + $indexOfArray: [ + "$reversedStops", + { + $arrayElemAt: ["$reversedQueryArray", 0], + } + ], + }, + -1, + ], + }, + }, + }, + { + $project: { + code: 1, + skipStops: { + $subtract: [ + { + $size: "$stops", + }, + { + $size: tiplocArray, + }, + ], + }, + }, + }, + ]; + + const res = await queryAggregate('pis', pipeline) + console.log(JSON.stringify(res)) } module.exports = { -- 2.34.1 From e3097b1af9ecb9b6e3f30309636ee58051529793 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 22:00:13 +0100 Subject: [PATCH 10/36] Setup pipeline ready for testing Signed-off-by: Fred Boniface --- src/services/pis.services.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index e9b3449..d919a89 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -52,7 +52,7 @@ async function findByTiplocArray(tiplocArray: string[]) { return res; } else { let partial = await findPartialMatchByTiploc(tiplocArray); - return partial + return partial; } } @@ -108,6 +108,7 @@ async function findPartialMatchByTiploc(tiplocArray: string[]) { const res = await queryAggregate('pis', pipeline) console.log(JSON.stringify(res)) + return res } module.exports = { -- 2.34.1 From f37d4235971823af6da9504d0328bf23a68cb96c Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 22:04:03 +0100 Subject: [PATCH 11/36] Update pipeline for partial PIS match Signed-off-by: Fred Boniface --- src/services/pis.services.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index d919a89..433fc77 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -64,8 +64,8 @@ async function findPartialMatchByTiploc(tiplocArray: string[]) { const pipeline = [ { $addFields: { - reversedStops: { - $reverseArray: "$stops", + reversedTiplocs: { + $reverseArray: "$tiplocs", }, reversedQueryArray: { $reverseArray: tiplocArray, @@ -78,7 +78,7 @@ async function findPartialMatchByTiploc(tiplocArray: string[]) { $gt: [ { $indexOfArray: [ - "$reversedStops", + "$reversedTiplocs", { $arrayElemAt: ["$reversedQueryArray", 0], } @@ -95,7 +95,7 @@ async function findPartialMatchByTiploc(tiplocArray: string[]) { skipStops: { $subtract: [ { - $size: "$stops", + $size: "$tiplocs", }, { $size: tiplocArray, -- 2.34.1 From 398a62aadbd87deafd6d54bdf4ef46dde7a13505 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 22:04:54 +0100 Subject: [PATCH 12/36] Add partial match note to find by CRS Signed-off-by: Fred Boniface --- src/services/pis.services.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index 433fc77..1b7b478 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -28,8 +28,9 @@ async function findPisByOrigDest(start: string, end: string) { ], }, }; - const search = db.query("pis", query); - return await search; + const search = await db.query("pis", query); + // Check for results, if none then try partial match + return search; } async function findPisByCode(code: string) { -- 2.34.1 From a701102228fd9c7d243dc36bbb82f9136d83db96 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 12 Oct 2023 22:23:08 +0100 Subject: [PATCH 13/36] Adjust script for running in dev mode Signed-off-by: Fred Boniface --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d90fe5e..1174651 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "scripts": { "build": "tsc", "run": "tsc && node dist/app.js", - "dev": "NODE_ENV=development tsc && node dist/app.js", + "dev": "tsc && NODE_ENV=development node dist/app.js", "start": "node app.js", "test": "jest", "format": "npx prettier -w ." -- 2.34.1 From 22db9911f276d57e358a906f8f9536a055e0ba80 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 28 Oct 2023 22:40:03 +0100 Subject: [PATCH 14/36] Fix security bugs Signed-off-by: Fred Boniface --- package-lock.json | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index c94f6c0..135263b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -839,12 +839,12 @@ "dev": true }, "node_modules/@babel/generator": { - "version": "7.22.15", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.22.15.tgz", - "integrity": "sha512-Zu9oWARBqeVOW0dZOjXc3JObrzuqothQ3y/n1kUtrjCoCPLkXUwMvOo/F/TCfoHMbWIFlWwpZtkZVb9ga4U2pA==", + "version": "7.23.0", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.23.0.tgz", + "integrity": "sha512-lN85QRR+5IbYrMWM6Y4pE/noaQtg4pNiqeNGX60eqOfo6gtEj6uw/JagelB8vVztSd7R6M5n1+PQkDbHbBRU4g==", "dev": true, "dependencies": { - "@babel/types": "^7.22.15", + "@babel/types": "^7.23.0", "@jridgewell/gen-mapping": "^0.3.2", "@jridgewell/trace-mapping": "^0.3.17", "jsesc": "^2.5.1" @@ -879,13 +879,13 @@ } }, "node_modules/@babel/helper-function-name": { - "version": "7.22.5", - "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.22.5.tgz", - "integrity": "sha512-wtHSq6jMRE3uF2otvfuD3DIvVhOsSNshQl0Qrd7qC9oQJzHvOL4qQXlQn2916+CXGywIjpGuIkoyZRRxHPiNQQ==", + "version": "7.23.0", + "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.23.0.tgz", + "integrity": "sha512-OErEqsrxjZTJciZ4Oo+eoZqeW9UIiOcuYKRJA4ZAgV9myA+pOXhhmpfNCKjEH/auVfEYVFJ6y1Tc4r0eIApqiw==", "dev": true, "dependencies": { - "@babel/template": "^7.22.5", - "@babel/types": "^7.22.5" + "@babel/template": "^7.22.15", + "@babel/types": "^7.23.0" }, "engines": { "node": ">=6.9.0" @@ -1094,9 +1094,9 @@ } }, "node_modules/@babel/parser": { - "version": "7.22.16", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.16.tgz", - "integrity": "sha512-+gPfKv8UWeKKeJTUxe59+OobVcrYHETCsORl61EmSkmgymguYk/X5bp7GuUIXaFsc6y++v8ZxPsLSSuujqDphA==", + "version": "7.23.0", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.23.0.tgz", + "integrity": "sha512-vvPKKdMemU85V9WE/l5wZEmImpCtLqbnTvqDS2U1fJ96KrxoW7KrXhNsNCblQlg8Ck4b85yxdTyelsMUgFUXiw==", "dev": true, "bin": { "parser": "bin/babel-parser.js" @@ -1297,19 +1297,19 @@ } }, "node_modules/@babel/traverse": { - "version": "7.22.20", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.22.20.tgz", - "integrity": "sha512-eU260mPZbU7mZ0N+X10pxXhQFMGTeLb9eFS0mxehS8HZp9o1uSnFeWQuG1UPrlxgA7QoUzFhOnilHDp0AXCyHw==", + "version": "7.23.2", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.23.2.tgz", + "integrity": "sha512-azpe59SQ48qG6nu2CzcMLbxUudtN+dOM9kDbUqGq3HXUJRlo7i8fvPoxQUzYgLZ4cMVmuZgm8vvBpNeRhd6XSw==", "dev": true, "dependencies": { "@babel/code-frame": "^7.22.13", - "@babel/generator": "^7.22.15", + "@babel/generator": "^7.23.0", "@babel/helper-environment-visitor": "^7.22.20", - "@babel/helper-function-name": "^7.22.5", + "@babel/helper-function-name": "^7.23.0", "@babel/helper-hoist-variables": "^7.22.5", "@babel/helper-split-export-declaration": "^7.22.6", - "@babel/parser": "^7.22.16", - "@babel/types": "^7.22.19", + "@babel/parser": "^7.23.0", + "@babel/types": "^7.23.0", "debug": "^4.1.0", "globals": "^11.1.0" }, @@ -1350,13 +1350,13 @@ "dev": true }, "node_modules/@babel/types": { - "version": "7.22.19", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.22.19.tgz", - "integrity": "sha512-P7LAw/LbojPzkgp5oznjE6tQEIWbp4PkkfrZDINTro9zgBRtI324/EYsiSI7lhPbpIQ+DCeR2NNmMWANGGfZsg==", + "version": "7.23.0", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.23.0.tgz", + "integrity": "sha512-0oIyUfKoI3mSqMvsxBdclDwxXKXAUA8v/apZbc+iSyARYou1o8ZGDxbUYyLFoW2arqS2jDGqJuZvv1d/io1axg==", "dev": true, "dependencies": { "@babel/helper-string-parser": "^7.22.5", - "@babel/helper-validator-identifier": "^7.22.19", + "@babel/helper-validator-identifier": "^7.22.20", "to-fast-properties": "^2.0.0" }, "engines": { -- 2.34.1 From 725dfb86e239210923ce54f29f7b231282209bfa Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 9 Nov 2023 20:37:33 +0000 Subject: [PATCH 15/36] Add comments Signed-off-by: Fred Boniface --- src/services/pis.services.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index 1b7b478..4feffa3 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -44,6 +44,8 @@ async function findPisByCode(code: string) { } async function findByTiplocArray(tiplocArray: string[]) { + // If any of the TIPLOCS in an array is RDG4AB - Replace it with READING. + // ^^ Double check those though before doing it. const query = { tiplocs: tiplocArray, }; -- 2.34.1 From 84791ea93a6a229b0d75ed709d17a5a6366e67ae Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 9 Nov 2023 20:42:19 +0000 Subject: [PATCH 16/36] Add route for testing https://git.fjla.uk/OwlBoard/backend/issues/47 Signed-off-by: Fred Boniface --- src/controllers/pis.controllers.js | 6 ++++++ src/routes/pis.routes.ts | 1 + 2 files changed, 7 insertions(+) diff --git a/src/controllers/pis.controllers.js b/src/controllers/pis.controllers.js index bdc1e65..144e58c 100644 --- a/src/controllers/pis.controllers.js +++ b/src/controllers/pis.controllers.js @@ -47,8 +47,14 @@ async function random(req, res, next) { } } +async function testingTiplocArray(req, res, next) { + let array = JSON.parse(req.params.array) + return pis.testingTiplocArray(array) +} + module.exports = { byStartEndCRS, byCode, random, + testingTiplocArray }; diff --git a/src/routes/pis.routes.ts b/src/routes/pis.routes.ts index 6a9aee8..6e2fc63 100644 --- a/src/routes/pis.routes.ts +++ b/src/routes/pis.routes.ts @@ -5,5 +5,6 @@ const pisCtr = require("../controllers/pis.controllers"); // PIS router.get("/byCode/:code", pisCtr.byCode); router.get("/byStartEnd/:startCrs/:endCrs", pisCtr.byStartEndCRS); +router.get("/test/bytiplocarray/:array", pisCtr.testingTiplocArray); module.exports = router; -- 2.34.1 From 820c8b0dd46a8a9dd0ae717ef8ded40a5c45cc0c Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 28 Nov 2023 11:05:58 +0000 Subject: [PATCH 17/36] Prepare for testing partial tiploc matches Signed-off-by: Fred Boniface --- src/controllers/pis.controllers.js | 2 +- src/services/pis.services.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/pis.controllers.js b/src/controllers/pis.controllers.js index 144e58c..ad109a3 100644 --- a/src/controllers/pis.controllers.js +++ b/src/controllers/pis.controllers.js @@ -49,7 +49,7 @@ async function random(req, res, next) { async function testingTiplocArray(req, res, next) { let array = JSON.parse(req.params.array) - return pis.testingTiplocArray(array) + return pis.findByTiplocArray(array) } module.exports = { diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index 4feffa3..f5c4365 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -43,7 +43,7 @@ async function findPisByCode(code: string) { return await search; } -async function findByTiplocArray(tiplocArray: string[]) { +export async function findByTiplocArray(tiplocArray: string[]) { // If any of the TIPLOCS in an array is RDG4AB - Replace it with READING. // ^^ Double check those though before doing it. const query = { -- 2.34.1 From e01cdd2f7e8e7052c545d27b2b10a8498f090825 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 29 Nov 2023 22:58:08 +0000 Subject: [PATCH 18/36] Add support for partial PIS matches by skipping first n stops Signed-off-by: Fred Boniface --- package.json | 2 +- src/controllers/pis.controllers.js | 6 +- src/middlewares/auth.middlewares.js | 4 +- src/services/pis.services.ts | 137 +++++++++++++++------------- src/utils/pis.utils.ts | 102 +++++++++++++++++++++ 5 files changed, 180 insertions(+), 71 deletions(-) create mode 100644 src/utils/pis.utils.ts diff --git a/package.json b/package.json index 1174651..dec7acd 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.0", + "@owlboard/ts-types": "^0.1.2", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", diff --git a/src/controllers/pis.controllers.js b/src/controllers/pis.controllers.js index ad109a3..f83970b 100644 --- a/src/controllers/pis.controllers.js +++ b/src/controllers/pis.controllers.js @@ -48,13 +48,13 @@ async function random(req, res, next) { } async function testingTiplocArray(req, res, next) { - let array = JSON.parse(req.params.array) - return pis.findByTiplocArray(array) + let array = JSON.parse(req.params.array); + res.json(await pis.findByTiplocArray(array)); } module.exports = { byStartEndCRS, byCode, random, - testingTiplocArray + testingTiplocArray, }; diff --git a/src/middlewares/auth.middlewares.js b/src/middlewares/auth.middlewares.js index 2e524ac..77fdf3c 100644 --- a/src/middlewares/auth.middlewares.js +++ b/src/middlewares/auth.middlewares.js @@ -5,9 +5,9 @@ module.exports = async function authCheck(req, res, next) { //log.out("authMiddlewares: Checking authentication", "dbug"); logger.logger.debug("Auth check starting"); if (process.env.NODE_ENV === "development") { - logger.logger.warn("DEVELOPMENT MODE - AUTHENTICATION DISABLED") + logger.logger.warn("DEVELOPMENT MODE - AUTHENTICATION DISABLED"); res.isAuthed = true; - return next() + return next(); } try { var uuid = req.headers.uuid; diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index f5c4365..f4bbf9c 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -3,8 +3,15 @@ const db = require("../services/dbAccess.services"); const clean = require("../utils/sanitizer.utils"); +import type { OB_Pis_SimpleObject } from "@owlboard/ts-types"; + import { logger } from "../utils/logger.utils"; import { queryAggregate } from "./dbAccess.services"; +import { + getPartialEndTiplocMatchPipeline, + getFullTiplocMatchPipeline, +} from "../utils/pis.utils"; +import { Document } from "mongodb"; const supported = ["GW", "UK"]; @@ -33,7 +40,9 @@ async function findPisByOrigDest(start: string, end: string) { return search; } -async function findPisByCode(code: string) { +async function findPisByCode( + code: string +): Promise { logger.debug(`pisServices.findPisByCode: Searching for PIS code: ${code}`); const cleanCode = clean.removeNonNumeric(code); const query = { @@ -43,75 +52,73 @@ async function findPisByCode(code: string) { return await search; } -export async function findByTiplocArray(tiplocArray: string[]) { - // If any of the TIPLOCS in an array is RDG4AB - Replace it with READING. - // ^^ Double check those though before doing it. - const query = { - tiplocs: tiplocArray, - }; - const res = await db.query("pis", query); - // If res is empty then try to find partial match - if (res.length) { - return res; - } else { - let partial = await findPartialMatchByTiploc(tiplocArray); - return partial; +// Queries the database for PIS codes that match the given TIPLOC array +export async function findByTiplocArray( + tiplocArray: string[] +): Promise { + // Firstly fix errant TIPLOCS such as RDNG4AB which will never return a result + // currently only works with RDNG4AB - checks for presence of RDNG4AB first to + // avoid iterating over every array ever searched for. + if (tiplocArray.includes("RDNG4AB")) { + fixErrantTiplocs(tiplocArray); + } + + // PERFORMANCE NOTE: + // The majority of queries will return a full match, + // this means that a more performant pipeline is used + // to find a full match and only then are more + // complicated pipelines used to find partial matches + // if the first pipeline returns nothing. + try { + const exactMatch = await findExactMatchByTiploc(tiplocArray); + if (exactMatch) { + return convertDocument(exactMatch, "none"); + } else { + const partialEnd = await findPartialEndMatchByTiploc(tiplocArray); + if (partialEnd) { + return convertDocument(partialEnd, "first"); + } else { + // Here, I should search for a partialStart match. For now return null. + return null; + } + } + } catch (err) { + logger.error(err, "Error in findByTiplocArray"); + return null; } } -async function findPartialMatchByTiploc(tiplocArray: string[]) { - // Do some magic here, similar to findPisByOrigDest but - // ensuring that the stops in the array match the last - // x number of items in the array. +// Uses a pipeline to find an exact match +async function findExactMatchByTiploc(array: string[]): Promise { + const pipeline = getFullTiplocMatchPipeline(array); + const res = await queryAggregate("pis", pipeline); + return res[0]; +} - const pipeline = [ - { - $addFields: { - reversedTiplocs: { - $reverseArray: "$tiplocs", - }, - reversedQueryArray: { - $reverseArray: tiplocArray, - }, - }, - }, - { - $match: { - $expr: { - $gt: [ - { - $indexOfArray: [ - "$reversedTiplocs", - { - $arrayElemAt: ["$reversedQueryArray", 0], - } - ], - }, - -1, - ], - }, - }, - }, - { - $project: { - code: 1, - skipStops: { - $subtract: [ - { - $size: "$tiplocs", - }, - { - $size: tiplocArray, - }, - ], - }, - }, - }, - ]; +// Uses a pipeline to find a partial match - only supporting +// codes ending with the correct stops for now. +async function findPartialEndMatchByTiploc(array: string[]): Promise { + const pipeline = getPartialEndTiplocMatchPipeline(array); + const res = await queryAggregate("pis", pipeline); + return res[0]; +} - const res = await queryAggregate('pis', pipeline) - console.log(JSON.stringify(res)) - return res +function convertDocument(doc: Document, skipType: string): OB_Pis_SimpleObject { + return { + code: doc.code, + toc: doc.toc, + skipCount: doc.skipStops, + skipType: skipType, + }; +} + +// Changes any instance of 'RDNG4AB' to 'RDNGSTN' +function fixErrantTiplocs(input: string[]): void { + input.forEach((value, index, array) => { + if (value === "RDNG4AB") { + array[index] = "RDNGSTN"; + } + }); } module.exports = { diff --git a/src/utils/pis.utils.ts b/src/utils/pis.utils.ts new file mode 100644 index 0000000..5d46ba9 --- /dev/null +++ b/src/utils/pis.utils.ts @@ -0,0 +1,102 @@ +export function getPartialEndTiplocMatchPipeline(query: string[]) { + return [ + { + $match: { + tiplocs: { + $all: query, + }, + }, + }, + { + $addFields: { + reversedTiplocs: { + $reverseArray: "$tiplocs", + }, + query: { + $literal: query, + }, + }, + }, + { + $addFields: { + reversedQuery: { + $reverseArray: "$query", + }, + }, + }, + { + $match: { + $expr: { + $eq: [ + { + $slice: [ + "$reversedTiplocs", + 0, + { + $size: "$reversedQuery", + }, + ], + }, + "$reversedQuery", + ], + }, + }, + }, + { + $addFields: { + skipStops: { + $subtract: [ + { + $size: "$tiplocs", + }, + { + $size: "$reversedQuery", + }, + ], + }, + }, + }, + { + $sort: { + skipStops: 1, + }, + }, + { + $limit: 1, + }, + { + $project: { + code: 1, + skipStops: 1, + toc: 1, + _id: 0, + }, + }, + ]; +} + +export function getFullTiplocMatchPipeline(query: string[]) { + return [ + { + $match: { + tiplocs: query, + }, + }, + { + $limit: 1, + }, + { + $addFields: { + skipStops: 0, + }, + }, + { + $project: { + code: 1, + toc: 1, + skipStops: 1, + _id: 0, + }, + }, + ]; +} -- 2.34.1 From acf8c5760e1fdacf567d2714591580594fdfb1e0 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 30 Nov 2023 09:20:56 +0000 Subject: [PATCH 19/36] Bump @owlboard/ts-types Signed-off-by: Fred Boniface --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dec7acd..734e2cc 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.2", + "@owlboard/ts-types": "^0.1.3", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", -- 2.34.1 From ab09455c96eff04865505bb4a1f2c5f05c1ef450 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 30 Nov 2023 09:32:07 +0000 Subject: [PATCH 20/36] Prepare 'trainSerice.services' for TS migration Signed-off-by: Fred Boniface --- src/services/trainService.services.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/services/trainService.services.js b/src/services/trainService.services.js index 4eeb688..82a6014 100644 --- a/src/services/trainService.services.js +++ b/src/services/trainService.services.js @@ -4,6 +4,8 @@ const pis = require("../services/pis.services"); import { logger } from "../utils/logger.utils"; +// This function is deprecated and should no longer be used. +// It will be removed in a later version async function findByHeadcodeToday(headcode) { const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); logger.debug( @@ -39,6 +41,7 @@ async function findByHeadcodeToday(headcode) { return preparedData; } +// Finds a train by its headcode value async function findByHeadcode(date, headcode) { const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); logger.debug( @@ -82,6 +85,7 @@ async function findByHeadcode(date, headcode) { return await filteredData; } +// Finds a train by its trainUid value async function findByTrainUid(uid, date = new Date()) { let queryDate; if (date === "now") { @@ -153,6 +157,7 @@ async function parseTrains(data) { return parsedData; } +// Filters services based on their STP Indicator async function filterServices(data) { let stpIndicators = {}, filteredServices = []; @@ -180,7 +185,7 @@ async function filterServices(data) { stpIndicators[trainUid].hasP = true; } } - let preparedData; + for (const serviceDetail of data) { const trainUid = serviceDetail["trainUid"]; const thisStpIndicators = stpIndicators[trainUid]; -- 2.34.1 From b49b2221d7dd4abefaeb4a996fa8c5d37a32b4f3 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 30 Nov 2023 14:33:57 +0000 Subject: [PATCH 21/36] Update @owlboard/ts-types --- package-lock.json | 8 ++++---- ...{trainService.services.js => trainService.services.ts} | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) rename src/services/{trainService.services.js => trainService.services.ts} (96%) diff --git a/package-lock.json b/package-lock.json index 135263b..16ec17e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.0", + "@owlboard/ts-types": "^0.1.3", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", @@ -1983,9 +1983,9 @@ } }, "node_modules/@owlboard/ts-types": { - "version": "0.1.0", - "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.0/ts-types-0.1.0.tgz", - "integrity": "sha512-5dLkm1wEimiv+pr5wuJbJvPfSek9WuApl/4SBwoFXVO2bmAIktFIHi+bznXKnhW9a7nJwX/w+bVwwJpBOI3I1g==", + "version": "0.1.3", + "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.3/ts-types-0.1.3.tgz", + "integrity": "sha512-BKi/tWvA0SN2rgt2GnFnSXEXzG3DNjeTm0ojVuMElnok9MJ9IniTXRjdAdjm+fe5ShebH6EKacO5f1MDAD3qJw==", "dev": true, "license": "GPL-3.0-or-later" }, diff --git a/src/services/trainService.services.js b/src/services/trainService.services.ts similarity index 96% rename from src/services/trainService.services.js rename to src/services/trainService.services.ts index 82a6014..647b9b3 100644 --- a/src/services/trainService.services.js +++ b/src/services/trainService.services.ts @@ -6,7 +6,7 @@ import { logger } from "../utils/logger.utils"; // This function is deprecated and should no longer be used. // It will be removed in a later version -async function findByHeadcodeToday(headcode) { +async function findByHeadcodeToday(headcode: string) { const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); logger.debug( `trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}` @@ -42,7 +42,7 @@ async function findByHeadcodeToday(headcode) { } // Finds a train by its headcode value -async function findByHeadcode(date, headcode) { +async function findByHeadcode(date: string | Date, headcode: string) { const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); logger.debug( `trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}` @@ -86,7 +86,7 @@ async function findByHeadcode(date, headcode) { } // Finds a train by its trainUid value -async function findByTrainUid(uid, date = new Date()) { +async function findByTrainUid(uid: string, date: Date | string = new Date()) { let queryDate; if (date === "now") { queryDate = new Date(); -- 2.34.1 From 89cd3630d4d2c7e73366e8fe021566bf3ac3e9b1 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Thu, 30 Nov 2023 21:33:17 +0000 Subject: [PATCH 22/36] Begin re-write of TrainServices.Services Signed-off-by: Fred Boniface --- src/services/trainService.services.ts | 26 +++++++++-------- src/services/trainService.services2ts | 40 +++++++++++++++++++++++++++ src/utils/trainService.utils.ts | 20 ++++++++++++++ 3 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 src/services/trainService.services2ts create mode 100644 src/utils/trainService.utils.ts diff --git a/src/services/trainService.services.ts b/src/services/trainService.services.ts index 647b9b3..413416f 100644 --- a/src/services/trainService.services.ts +++ b/src/services/trainService.services.ts @@ -4,6 +4,8 @@ const pis = require("../services/pis.services"); import { logger } from "../utils/logger.utils"; +import type { TrainServices, Service, Stop } from "@owlboard/ts-types"; + // This function is deprecated and should no longer be used. // It will be removed in a later version async function findByHeadcodeToday(headcode: string) { @@ -42,7 +44,7 @@ async function findByHeadcodeToday(headcode: string) { } // Finds a train by its headcode value -async function findByHeadcode(date: string | Date, headcode: string) { +async function findByHeadcode(date: string | Date, headcode: string): Promise { const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); logger.debug( `trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}` @@ -80,13 +82,13 @@ async function findByHeadcode(date: string | Date, headcode: string) { }, }, ]; - const queryData = await db.queryAggregate("timetable", pipeline); + const queryData: Service[] = await db.queryAggregate("timetable", pipeline); let filteredData = filterServices(queryData); return await filteredData; } // Finds a train by its trainUid value -async function findByTrainUid(uid: string, date: Date | string = new Date()) { +async function findByTrainUid(uid: string, date: Date | string = new Date()): Promise { let queryDate; if (date === "now") { queryDate = new Date(); @@ -100,7 +102,7 @@ async function findByTrainUid(uid: string, date: Date | string = new Date()) { }; const queryData = await db.query("timetable", query); if (queryData.length === 0) { - return []; + return queryData; } let services; services = await filterServices(queryData); @@ -128,7 +130,7 @@ module.exports = { /* Accepts the 'stops' array from a db query and produces an array of only public stops as TIPLOCs. */ -async function getPublicStops(data) { +async function getPublicStops(data: Stop[]): Promise { let tiplocList = []; for (const publicStop in data) { if (data[publicStop]["isPublic"]) { @@ -141,24 +143,26 @@ async function getPublicStops(data) { /* 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 = []; +async function parseTrains(data: TrainServices[]): Promise { + let trainUids: string[] = []; for (const i of data) { const trainUid = i["trainUid"]; if (!trainUids.includes(trainUid)) { trainUids.push(trainUid); } } - let parsedData = []; + let parsedData: TrainServices[] = []; for (const i in trainUids) { - const result = await findByTrainUid(trainUids[i]); - parsedData.push(result); + const result: TrainServices | null = await findByTrainUid(trainUids[i]); + if (result) { + parsedData.push(result); + } } return parsedData; } // Filters services based on their STP Indicator -async function filterServices(data) { +async function filterServices(data: Service[]): Promise { let stpIndicators = {}, filteredServices = []; for (const serviceDetail of data) { diff --git a/src/services/trainService.services2ts b/src/services/trainService.services2ts new file mode 100644 index 0000000..6c66bb1 --- /dev/null +++ b/src/services/trainService.services2ts @@ -0,0 +1,40 @@ +import { logger } from "../utils/logger.utils"; +import { findByTiplocArray } from "../services/pis.services" +import { queryAggregate } from "./dbAccess.services"; +import { getFindByHeadcodePipeline } from "../utils/trainService.utils" +import { removeNonAlphanumeric } from "../utils/sanitizer.utils" + +import type { TrainServices, Service, Stop } from "@owlboard/ts-types"; +import type { Document } from "mongodb"; + +export async function findByHeadcode(headcode: string, date: Date | string): Promise { + const sanitizedHeadcode = removeNonAlphanumeric(headcode); + logger.debug(`trainServices.findByHeadcode: Searching for trains by headcode: ${headcode}`) + + // If 'now' then generate a new Date now, else use the provided date, then set time to 1200. + const searchDate = (date === "now") ? new Date() : new Date(date); + searchDate.setHours(12, 0, 0); + + // Get the 'shortDay' + const shortDay = getShortDay(searchDate) + + const query = { + headcode: sanitizedHeadcode, + scheduleStartDate: { $lte: searchDate }, + scheduleEndDate: { $gte: searchDate }, + daysRun: { $in: [shortDay] }, + }; + const pipeline = getFindByHeadcodePipeline(query) + // Before proceeding, I need to check the return from the database to ensure that it + // is of type Service[] - for this use the Aggregate function in Compass. + const result: Service[] = await queryAggregate("timetable", pipeline) as Service[] + + // The Filter Services function needs writing - possibly moving to utils as it is long. +} + +// Outputs the standard 'shortday' string from a Date. +function getShortDay(day: Date): string { + const dayMap = ["su", "m", "t", "w", "th", "f", "s"]; + const shortDay = dayMap[day.getDay()]; + return shortDay +} \ No newline at end of file diff --git a/src/utils/trainService.utils.ts b/src/utils/trainService.utils.ts new file mode 100644 index 0000000..7cef600 --- /dev/null +++ b/src/utils/trainService.utils.ts @@ -0,0 +1,20 @@ +export function getFindByHeadcodePipeline(query: any) { + return [ + { + $match: query, + }, + { + $project: { + operator: 1, + stops: { + $concatArrays: [ + [{ $first: "$stops" }], + [{ $arrayElemAt: ["$stops", -1] }], + ], + }, + trainUid: 1, + stpIndicator: 1, + }, + }, + ]; +} \ No newline at end of file -- 2.34.1 From f91609e0d2afc04fc4c71005dbc2306360925451 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 1 Dec 2023 21:40:25 +0000 Subject: [PATCH 23/36] Further rewrite of trainService.services Signed-off-by: Fred Boniface --- package-lock.json | 8 +- package.json | 2 +- src/services/trainService.services2.ts | 128 +++++++++++++++++++++++++ src/services/trainService.services2ts | 40 -------- src/utils/trainService.utils.ts | 6 ++ 5 files changed, 139 insertions(+), 45 deletions(-) create mode 100644 src/services/trainService.services2.ts delete mode 100644 src/services/trainService.services2ts diff --git a/package-lock.json b/package-lock.json index 16ec17e..bdf5461 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.3", + "@owlboard/ts-types": "^0.1.5", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", @@ -1983,9 +1983,9 @@ } }, "node_modules/@owlboard/ts-types": { - "version": "0.1.3", - "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.3/ts-types-0.1.3.tgz", - "integrity": "sha512-BKi/tWvA0SN2rgt2GnFnSXEXzG3DNjeTm0ojVuMElnok9MJ9IniTXRjdAdjm+fe5ShebH6EKacO5f1MDAD3qJw==", + "version": "0.1.5", + "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.5/ts-types-0.1.5.tgz", + "integrity": "sha512-bbCSDpwAJSUKFHcT3QwXAutlBp02QqAXvobg/wGm8zoaAeWNozWeJSdceMzgse6Ywc1aGamP4w20hlTREl7f8g==", "dev": true, "license": "GPL-3.0-or-later" }, diff --git a/package.json b/package.json index 734e2cc..03ed62c 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.3", + "@owlboard/ts-types": "^0.1.5", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", diff --git a/src/services/trainService.services2.ts b/src/services/trainService.services2.ts new file mode 100644 index 0000000..ffdafca --- /dev/null +++ b/src/services/trainService.services2.ts @@ -0,0 +1,128 @@ +import { logger } from "../utils/logger.utils"; +import { findByTiplocArray } from "../services/pis.services" +import { queryAggregate } from "./dbAccess.services"; +import { getFindByHeadcodePipeline, getFindByTrainUidPipeline } from "../utils/trainService.utils" +import { removeNonAlphanumeric } from "../utils/sanitizer.utils" + +import type { TrainServices, Service, Stop, SimpleService } from "@owlboard/ts-types"; + +export async function findByHeadcode(headcode: string, date: Date | string): Promise { + const sanitizedHeadcode = removeNonAlphanumeric(headcode); + logger.debug(`trainServices.findByHeadcode: Searching for trains by headcode: ${headcode}`) + + // If 'now' then generate a new Date now, else use the provided date, then set time to 1200. + const searchDate = (date === "now") ? new Date() : new Date(date); + searchDate.setHours(12, 0, 0); + + // Get the 'shortDay' + const shortDay = getShortDay(searchDate) + + const query = { + headcode: sanitizedHeadcode, + scheduleStartDate: { $lte: searchDate }, + scheduleEndDate: { $gte: searchDate }, + daysRun: { $in: [shortDay] }, + }; + const pipeline = getFindByHeadcodePipeline(query) + + const result: SimpleService[] = await queryAggregate("timetable", pipeline) as SimpleService[] + + const services = filterServices(result); + + return services; +} + +export async function findByTrainUid(uid: string, date: Date | string = new Date()) { + // Set the correct date - whether a date or "now" was passed to function + let queryDate: Date; + if (date instanceof Date) { + queryDate = date; + } else { + queryDate = new Date(); + } + + // Build query + const query = { + trainUid: uid, + scheduleStartDate: { $lte: queryDate }, + scheduleEndDate: { $gte: queryDate }, + daysRun: { $in: [getShortDay(queryDate)] } + } + const pipeline = getFindByTrainUidPipeline(query) + + const result = await queryAggregate("timetable", pipeline) as Service[] + + let services = filterServices(result) as Service[] + + // Next up is the public stops filter, followed by the PIS filter. +} + +// Internal Functions: + +// Outputs the standard 'shortday' string from a Date. +function getShortDay(day: Date): string { + const dayMap = ["su", "m", "t", "w", "th", "f", "s"]; + const shortDay = dayMap[day.getDay()]; + return shortDay +} + +// Filters services using their STP indicator so that over-riding entries are returned correctly +function filterServices(services: SimpleService[]): (SimpleService[]) { + let stpIndicators: Record = {}; + let filteredServices: SimpleService[] = [] + + for (const service of services) { + const trainUid = service["trainUid"], stpIndicator = service["stpIndicator"] + + // Creates the stpIndicators array: + if (!stpIndicators[trainUid]) { + stpIndicators[trainUid] = { + hasC: false, hasN: false, hasO: false, hasP: false, + } + } + + if (stpIndicator === "C") { + stpIndicators[trainUid].hasC = true; + } + if (stpIndicator === "N") { + stpIndicators[trainUid].hasN = true; + } + if (stpIndicator === "O") { + stpIndicators[trainUid].hasO = true; + } + if (stpIndicator === "P") { + stpIndicators[trainUid].hasP = true; + } + } + + // Iterate each service, and only output one service matching each trainUid, + // C > N > O > P is the order, with C being prioritised over other STP types. + for (const service of services) { + const trainUid = service["trainUid"] + const thisStpIndicators = stpIndicators[trainUid]; + const stpIndicator = service["stpIndicator"]; + + if (stpIndicator === "C") { + continue; + } + if (stpIndicator === "N" && !thisStpIndicators.hasC) { + filteredServices.push(service); + } else if ( + stpIndicator === "O" && + !thisStpIndicators.hasC && + !thisStpIndicators.hasN + ) { + filteredServices.push(service); + } else if ( + stpIndicator === "P" && + !thisStpIndicators.hasC && + !thisStpIndicators.hasN && + !thisStpIndicators.hasO + ) { + filteredServices.push(service); + } + } + return filteredServices; +} + +// Local Types: diff --git a/src/services/trainService.services2ts b/src/services/trainService.services2ts deleted file mode 100644 index 6c66bb1..0000000 --- a/src/services/trainService.services2ts +++ /dev/null @@ -1,40 +0,0 @@ -import { logger } from "../utils/logger.utils"; -import { findByTiplocArray } from "../services/pis.services" -import { queryAggregate } from "./dbAccess.services"; -import { getFindByHeadcodePipeline } from "../utils/trainService.utils" -import { removeNonAlphanumeric } from "../utils/sanitizer.utils" - -import type { TrainServices, Service, Stop } from "@owlboard/ts-types"; -import type { Document } from "mongodb"; - -export async function findByHeadcode(headcode: string, date: Date | string): Promise { - const sanitizedHeadcode = removeNonAlphanumeric(headcode); - logger.debug(`trainServices.findByHeadcode: Searching for trains by headcode: ${headcode}`) - - // If 'now' then generate a new Date now, else use the provided date, then set time to 1200. - const searchDate = (date === "now") ? new Date() : new Date(date); - searchDate.setHours(12, 0, 0); - - // Get the 'shortDay' - const shortDay = getShortDay(searchDate) - - const query = { - headcode: sanitizedHeadcode, - scheduleStartDate: { $lte: searchDate }, - scheduleEndDate: { $gte: searchDate }, - daysRun: { $in: [shortDay] }, - }; - const pipeline = getFindByHeadcodePipeline(query) - // Before proceeding, I need to check the return from the database to ensure that it - // is of type Service[] - for this use the Aggregate function in Compass. - const result: Service[] = await queryAggregate("timetable", pipeline) as Service[] - - // The Filter Services function needs writing - possibly moving to utils as it is long. -} - -// Outputs the standard 'shortday' string from a Date. -function getShortDay(day: Date): string { - const dayMap = ["su", "m", "t", "w", "th", "f", "s"]; - const shortDay = dayMap[day.getDay()]; - return shortDay -} \ No newline at end of file diff --git a/src/utils/trainService.utils.ts b/src/utils/trainService.utils.ts index 7cef600..1920c0c 100644 --- a/src/utils/trainService.utils.ts +++ b/src/utils/trainService.utils.ts @@ -17,4 +17,10 @@ export function getFindByHeadcodePipeline(query: any) { }, }, ]; +} + +export function getFindByTrainUidPipeline(query: any) { + return [{ + '$match': query, +},{'$project': {'_id': 0}}] } \ No newline at end of file -- 2.34.1 From 6166c83d5ac4f36312784f1142562272870916ca Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 1 Dec 2023 21:43:08 +0000 Subject: [PATCH 24/36] Add notes for continuation of work Signed-off-by: Fred Boniface --- src/services/trainService.services2.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/services/trainService.services2.ts b/src/services/trainService.services2.ts index ffdafca..1486cc4 100644 --- a/src/services/trainService.services2.ts +++ b/src/services/trainService.services2.ts @@ -54,7 +54,9 @@ export async function findByTrainUid(uid: string, date: Date | string = new Date let services = filterServices(result) as Service[] - // Next up is the public stops filter, followed by the PIS filter. + // Next up is the public stops filter, the resulting array is used for a PIS search + // I can probably write a function that both builds the public stop array and returns + // a PIS object - or null. } // Internal Functions: -- 2.34.1 From 04e8bdd7ae8caad32e9b431726695f9f036421e7 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 13 Jan 2024 21:11:06 +0000 Subject: [PATCH 25/36] Made runnable Signed-off-by: Fred Boniface --- app.js | 2 +- .../{trainService.services.ts => .trainService.services.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/services/{trainService.services.ts => .trainService.services.ts} (100%) diff --git a/app.js b/app.js index 2485165..9b6f6b8 100644 --- a/app.js +++ b/app.js @@ -20,7 +20,7 @@ const authenticate = require("./src/middlewares/auth.middlewares"); // Internal Requires const version = require("./src/configs/version.configs"); -const trainRtr = require("./src/routes/train.routes"); +//const trainRtr = require("./src/routes/train.routes"); const pis2Rtr = require("./src/routes/pis.routes"); // API Version 2 Routes const ref2Rtr = require("./src/routes/ref.routes"); // API Version 2 Routes const live2Rtr = require("./src/routes/live.routes"); // API Version 2 Routes diff --git a/src/services/trainService.services.ts b/src/services/.trainService.services.ts similarity index 100% rename from src/services/trainService.services.ts rename to src/services/.trainService.services.ts -- 2.34.1 From 8f26218ef294cb1cf394b01d5c1c0c472b079908 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 13 Jan 2024 22:22:47 +0000 Subject: [PATCH 26/36] Add Comments Signed-off-by: Fred Boniface --- src/services/pis.services.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index f4bbf9c..5d497a6 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -118,6 +118,7 @@ function fixErrantTiplocs(input: string[]): void { if (value === "RDNG4AB") { array[index] = "RDNGSTN"; } + // Additional substitutions can be applied here }); } -- 2.34.1 From 89043516ea32c48ed0382281f53d7f3906e92eb7 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 16 Jan 2024 21:31:39 +0000 Subject: [PATCH 27/36] Prepare for new PIS API response Signed-off-by: Fred Boniface --- src/services/.trainService.services.ts | 10 +- src/services/pis.services.ts | 2 +- src/services/trainService.services2.ts | 106 +++++++++++++----- .../timetable/timetableProcessor.utils.ts | 36 ++++++ src/utils/trainService.utils.ts | 11 +- 5 files changed, 127 insertions(+), 38 deletions(-) diff --git a/src/services/.trainService.services.ts b/src/services/.trainService.services.ts index 413416f..5cd0d45 100644 --- a/src/services/.trainService.services.ts +++ b/src/services/.trainService.services.ts @@ -44,7 +44,10 @@ async function findByHeadcodeToday(headcode: string) { } // Finds a train by its headcode value -async function findByHeadcode(date: string | Date, headcode: string): Promise { +async function findByHeadcode( + date: string | Date, + headcode: string +): Promise { const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode).toUpperCase(); logger.debug( `trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}` @@ -88,7 +91,10 @@ async function findByHeadcode(date: string | Date, headcode: string): Promise { +async function findByTrainUid( + uid: string, + date: Date | string = new Date() +): Promise { let queryDate; if (date === "now") { queryDate = new Date(); diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index 5d497a6..88b8d40 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -13,7 +13,7 @@ import { } from "../utils/pis.utils"; import { Document } from "mongodb"; -const supported = ["GW", "UK"]; +export const supported = ["GW", "UK"]; async function findPisByOrigDest(start: string, end: string) { logger.debug( diff --git a/src/services/trainService.services2.ts b/src/services/trainService.services2.ts index 1486cc4..6152a37 100644 --- a/src/services/trainService.services2.ts +++ b/src/services/trainService.services2.ts @@ -1,21 +1,36 @@ import { logger } from "../utils/logger.utils"; -import { findByTiplocArray } from "../services/pis.services" +import { findByTiplocArray, supported } from "../services/pis.services"; import { queryAggregate } from "./dbAccess.services"; -import { getFindByHeadcodePipeline, getFindByTrainUidPipeline } from "../utils/trainService.utils" -import { removeNonAlphanumeric } from "../utils/sanitizer.utils" +import { + getFindByHeadcodePipeline, + getFindByTrainUidPipeline, +} from "../utils/trainService.utils"; +import { removeNonAlphanumeric } from "../utils/sanitizer.utils"; +import { formatTimetableDetail } from "../utils/processors/timetable/timetableProcessor.utils"; -import type { TrainServices, Service, Stop, SimpleService } from "@owlboard/ts-types"; +import type { + TrainServices, + Service, + Stop, + SimpleService, + OB_Pis_SimpleObject, +} from "@owlboard/ts-types"; -export async function findByHeadcode(headcode: string, date: Date | string): Promise { +export async function findByHeadcode( + headcode: string, + date: Date | string +): Promise { const sanitizedHeadcode = removeNonAlphanumeric(headcode); - logger.debug(`trainServices.findByHeadcode: Searching for trains by headcode: ${headcode}`) - + logger.debug( + `trainServices.findByHeadcode: Searching for trains by headcode: ${headcode}` + ); + // If 'now' then generate a new Date now, else use the provided date, then set time to 1200. - const searchDate = (date === "now") ? new Date() : new Date(date); + const searchDate = date === "now" ? new Date() : new Date(date); searchDate.setHours(12, 0, 0); // Get the 'shortDay' - const shortDay = getShortDay(searchDate) + const shortDay = getShortDay(searchDate); const query = { headcode: sanitizedHeadcode, @@ -23,16 +38,22 @@ export async function findByHeadcode(headcode: string, date: Date | string): Pro scheduleEndDate: { $gte: searchDate }, daysRun: { $in: [shortDay] }, }; - const pipeline = getFindByHeadcodePipeline(query) - - const result: SimpleService[] = await queryAggregate("timetable", pipeline) as SimpleService[] + const pipeline = getFindByHeadcodePipeline(query); + + const result: SimpleService[] = (await queryAggregate( + "timetable", + pipeline + )) as SimpleService[]; const services = filterServices(result); return services; } -export async function findByTrainUid(uid: string, date: Date | string = new Date()) { +export async function findByTrainUid( + uid: string, + date: Date | string = new Date() +) { // Set the correct date - whether a date or "now" was passed to function let queryDate: Date; if (date instanceof Date) { @@ -46,41 +67,64 @@ export async function findByTrainUid(uid: string, date: Date | string = new Date trainUid: uid, scheduleStartDate: { $lte: queryDate }, scheduleEndDate: { $gte: queryDate }, - daysRun: { $in: [getShortDay(queryDate)] } + daysRun: { $in: [getShortDay(queryDate)] }, + }; + const pipeline = getFindByTrainUidPipeline(query); + + const result = (await queryAggregate("timetable", pipeline)) as Service[]; + + let services = filterServices(result) as Service[]; + + // Check if the operator is on the supported TOC list for PIS Codes - if so, call the fetchPisCode function. + let pis: OB_Pis_SimpleObject | null; + if (supported.includes(services[0]?.operator)) { + pis = await fetchPisCode(services[0]?.stops); } - const pipeline = getFindByTrainUidPipeline(query) - - const result = await queryAggregate("timetable", pipeline) as Service[] - - let services = filterServices(result) as Service[] - - // Next up is the public stops filter, the resulting array is used for a PIS search - // I can probably write a function that both builds the public stop array and returns - // a PIS object - or null. + // TODO: Format and return data, the function called is not yet complete + return formatTimetableDetail(services[0], pis) } // Internal Functions: +// Filters out non-passenger stops and then uses the stop array to request a PIS code for the service +async function fetchPisCode( + stops: Stop[] +): Promise { + let tiplocList: string[] = []; + for (const stop in stops) { + if (stops[stop]["isPublic"]) tiplocList.push(stops[stop]["tiploc"]); + } + const pisData = await findByTiplocArray(tiplocList); + return pisData; +} + // Outputs the standard 'shortday' string from a Date. function getShortDay(day: Date): string { const dayMap = ["su", "m", "t", "w", "th", "f", "s"]; const shortDay = dayMap[day.getDay()]; - return shortDay + return shortDay; } // Filters services using their STP indicator so that over-riding entries are returned correctly -function filterServices(services: SimpleService[]): (SimpleService[]) { - let stpIndicators: Record = {}; - let filteredServices: SimpleService[] = [] +function filterServices(services: SimpleService[]): SimpleService[] { + let stpIndicators: Record< + string, + { hasC: boolean; hasN: boolean; hasO: boolean; hasP: boolean } + > = {}; + let filteredServices: SimpleService[] = []; for (const service of services) { - const trainUid = service["trainUid"], stpIndicator = service["stpIndicator"] + const trainUid = service["trainUid"], + stpIndicator = service["stpIndicator"]; // Creates the stpIndicators array: if (!stpIndicators[trainUid]) { stpIndicators[trainUid] = { - hasC: false, hasN: false, hasO: false, hasP: false, - } + hasC: false, + hasN: false, + hasO: false, + hasP: false, + }; } if (stpIndicator === "C") { @@ -100,7 +144,7 @@ function filterServices(services: SimpleService[]): (SimpleService[]) { // Iterate each service, and only output one service matching each trainUid, // C > N > O > P is the order, with C being prioritised over other STP types. for (const service of services) { - const trainUid = service["trainUid"] + const trainUid = service["trainUid"]; const thisStpIndicators = stpIndicators[trainUid]; const stpIndicator = service["stpIndicator"]; diff --git a/src/utils/processors/timetable/timetableProcessor.utils.ts b/src/utils/processors/timetable/timetableProcessor.utils.ts index e69de29..4926334 100644 --- a/src/utils/processors/timetable/timetableProcessor.utils.ts +++ b/src/utils/processors/timetable/timetableProcessor.utils.ts @@ -0,0 +1,36 @@ +import type {Service, OB_TrainTT_service, OB_Pis_SimpleObject, OB_TrainTT_stopDetail, Stop} from "@owlboard/ts-types" + +export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject | null): OB_TrainTT_service { + const formattedService: OB_TrainTT_service = { + stpIndicator: service.stpIndicator, + operator: service.operator, + trainUid: service.trainUid, + headcode: service.headcode, + powerType: service.powerType, + planSpeed: convertStringToNumber(service.planSpeed), // Force convert from string to number. + scheduleStart: service.scheduleStartDate, + scheduleEnd: service.scheduleEndDate, + daysRun: service.daysRun, + stops: service.stops, // Need separate function to ensure type compatibility + vstp: service.vstp, // Need to ensure that ts-types is up to date. + } + + if (pis) { + formattedService.pis = pis; + } + + return formattedService +} + +function formatStops(stops: Stop[]): OB_TrainTT_stopDetail[] { + // Cleanly coerce Stop[] to OB_TrainTT_stopDetail[] +} + +function convertStringToNumber(str: string): number { + const number = parseFloat(str) + if (isNaN(number)) { + return 0; + } else { + return number + } +} \ No newline at end of file diff --git a/src/utils/trainService.utils.ts b/src/utils/trainService.utils.ts index 1920c0c..c34efb7 100644 --- a/src/utils/trainService.utils.ts +++ b/src/utils/trainService.utils.ts @@ -20,7 +20,10 @@ export function getFindByHeadcodePipeline(query: any) { } export function getFindByTrainUidPipeline(query: any) { - return [{ - '$match': query, -},{'$project': {'_id': 0}}] -} \ No newline at end of file + return [ + { + $match: query, + }, + { $project: { _id: 0 } }, + ]; +} -- 2.34.1 From 7eb1eaba502a1647b7b1e15db90c4d569ff7a94b Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 2 Feb 2024 12:42:12 +0000 Subject: [PATCH 28/36] Nothing important Signed-off-by: Fred Boniface --- src/utils/processors/timetable/timetableProcessor.utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/processors/timetable/timetableProcessor.utils.ts b/src/utils/processors/timetable/timetableProcessor.utils.ts index 4926334..921178b 100644 --- a/src/utils/processors/timetable/timetableProcessor.utils.ts +++ b/src/utils/processors/timetable/timetableProcessor.utils.ts @@ -11,7 +11,7 @@ export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject scheduleStart: service.scheduleStartDate, scheduleEnd: service.scheduleEndDate, daysRun: service.daysRun, - stops: service.stops, // Need separate function to ensure type compatibility + stops: formatStops(service.stops), // Need separate function to ensure type compatibility vstp: service.vstp, // Need to ensure that ts-types is up to date. } -- 2.34.1 From 3b03330ce11ec651d8f0a96c3307669def2a2e3b Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 3 Feb 2024 21:29:52 +0000 Subject: [PATCH 29/36] Complete server side PIS searching. Temporary disablement of authentication. Signed-off-by: Fred Boniface --- package-lock.json | 8 ++--- package.json | 2 +- src/controllers/train.controllers.js | 8 ++--- src/services/pis.services.ts | 2 +- ....services2.ts => trainService.services.ts} | 24 +++++++++++-- .../timetable/timetableProcessor.utils.ts | 36 +++++++++++++++++-- 6 files changed, 65 insertions(+), 15 deletions(-) rename src/services/{trainService.services2.ts => trainService.services.ts} (89%) diff --git a/package-lock.json b/package-lock.json index bdf5461..b1c016d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.5", + "@owlboard/ts-types": "^0.1.8", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", @@ -1983,9 +1983,9 @@ } }, "node_modules/@owlboard/ts-types": { - "version": "0.1.5", - "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.5/ts-types-0.1.5.tgz", - "integrity": "sha512-bbCSDpwAJSUKFHcT3QwXAutlBp02QqAXvobg/wGm8zoaAeWNozWeJSdceMzgse6Ywc1aGamP4w20hlTREl7f8g==", + "version": "0.1.8", + "resolved": "https://git.fjla.uk/api/packages/OwlBoard/npm/%40owlboard%2Fts-types/-/0.1.8/ts-types-0.1.8.tgz", + "integrity": "sha512-GNwaaAiP9MJKvDODg6C3U9GSwm1+BodkAndFRWoZXN+rU1KUFoxMEp2k1ThnCrrJHktVYDWk9dkyl1PlgSKtaw==", "dev": true, "license": "GPL-3.0-or-later" }, diff --git a/package.json b/package.json index 03ed62c..fe5b269 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "zlib": "^1.0.5" }, "devDependencies": { - "@owlboard/ts-types": "^0.1.5", + "@owlboard/ts-types": "^0.1.8", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", diff --git a/src/controllers/train.controllers.js b/src/controllers/train.controllers.js index cfd7a21..dc65e80 100644 --- a/src/controllers/train.controllers.js +++ b/src/controllers/train.controllers.js @@ -17,18 +17,18 @@ async function getByHeadcodeToday(req, res, next) { } async function get(req, res, next) { - if (!req.isAuthed) { +/* if (!req.isAuthed) { const err = new Error("Unauthorized"); err.status = 401; next(err); - } - let date = req.params.date; + } -_-_-_ TEMP DISABLE AUTH _-_-_- +*/ let date = req.params.date; let searchType = req.params.searchType; let id = req.params.id; try { switch (searchType) { case "headcode": - res.json(await train.findByHeadcode(date, id)); + res.json(await train.findByHeadcode(id, date)); break; case "byTrainUid": res.json(await train.findByTrainUid(id, date)); diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index 88b8d40..eb2b3a2 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -105,7 +105,7 @@ async function findPartialEndMatchByTiploc(array: string[]): Promise { function convertDocument(doc: Document, skipType: string): OB_Pis_SimpleObject { return { - code: doc.code, + code: doc.code.toString(), toc: doc.toc, skipCount: doc.skipStops, skipType: skipType, diff --git a/src/services/trainService.services2.ts b/src/services/trainService.services.ts similarity index 89% rename from src/services/trainService.services2.ts rename to src/services/trainService.services.ts index 6152a37..59c7598 100644 --- a/src/services/trainService.services2.ts +++ b/src/services/trainService.services.ts @@ -1,5 +1,5 @@ import { logger } from "../utils/logger.utils"; -import { findByTiplocArray, supported } from "../services/pis.services"; +import { findByTiplocArray, supported } from "./pis.services"; import { queryAggregate } from "./dbAccess.services"; import { getFindByHeadcodePipeline, @@ -79,6 +79,8 @@ export async function findByTrainUid( let pis: OB_Pis_SimpleObject | null; if (supported.includes(services[0]?.operator)) { pis = await fetchPisCode(services[0]?.stops); + } else { + pis = null } // TODO: Format and return data, the function called is not yet complete return formatTimetableDetail(services[0], pis) @@ -94,10 +96,28 @@ async function fetchPisCode( for (const stop in stops) { if (stops[stop]["isPublic"]) tiplocList.push(stops[stop]["tiploc"]); } - const pisData = await findByTiplocArray(tiplocList); + // Check if no public stops - then it should use an ECS headcode + let pisData: OB_Pis_SimpleObject | null + if (tiplocList) { + pisData = await findByTiplocArray(tiplocList); + } else { + pisData = { + toc: "GW", + skipCount: 0, + code: randomEcsPis() + } + } + return pisData; } +// Picks a random choice of the ECS PIS Codes +function randomEcsPis(): string { + const options = ["0015", "9997"] + const randomValue = Math.floor(Math.random() * 2 ) + return options[randomValue] +} + // Outputs the standard 'shortday' string from a Date. function getShortDay(day: Date): string { const dayMap = ["su", "m", "t", "w", "th", "f", "s"]; diff --git a/src/utils/processors/timetable/timetableProcessor.utils.ts b/src/utils/processors/timetable/timetableProcessor.utils.ts index 921178b..ecbabe2 100644 --- a/src/utils/processors/timetable/timetableProcessor.utils.ts +++ b/src/utils/processors/timetable/timetableProcessor.utils.ts @@ -7,12 +7,12 @@ export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject trainUid: service.trainUid, headcode: service.headcode, powerType: service.powerType, - planSpeed: convertStringToNumber(service.planSpeed), // Force convert from string to number. + planSpeed: convertStringToNumber(service.planSpeed), scheduleStart: service.scheduleStartDate, scheduleEnd: service.scheduleEndDate, daysRun: service.daysRun, - stops: formatStops(service.stops), // Need separate function to ensure type compatibility - vstp: service.vstp, // Need to ensure that ts-types is up to date. + stops: formatStops(service.stops), + vstp: service.vstp, } if (pis) { @@ -24,6 +24,36 @@ export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject function formatStops(stops: Stop[]): OB_TrainTT_stopDetail[] { // Cleanly coerce Stop[] to OB_TrainTT_stopDetail[] + const formattedStops: OB_TrainTT_stopDetail[] = [] + + for (const stop of stops) { + formattedStops.push(formatStopTimes(stop)) + } + + return formattedStops +} + +function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail { + // Cleanly converts a single stop to a stopdetail object + let formattedStop: OB_TrainTT_stopDetail = { + tiploc: stop.tiploc, + isPublic: false, + } + if (stop.publicArrival) { + formattedStop.publicArrival = stop.publicArrival; + formattedStop.isPublic = true; + } + if (stop.publicDeparture) { + formattedStop.publicDeparture = stop.publicDeparture; + formattedStop.isPublic = true; + } + if (stop.wttArrival) { + formattedStop.wttArrival = stop.wttArrival; + } + if (stop.wttDeparture) { + formattedStop.wttDeparture = stop.wttDeparture; + } + return formattedStop } function convertStringToNumber(str: string): number { -- 2.34.1 From e1ba881be49ba5cf60bf1be8f144f347ae163aef Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 3 Feb 2024 21:33:31 +0000 Subject: [PATCH 30/36] Fix for ECS Headcodes Signed-off-by: Fred Boniface --- src/services/trainService.services.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/trainService.services.ts b/src/services/trainService.services.ts index 59c7598..449758c 100644 --- a/src/services/trainService.services.ts +++ b/src/services/trainService.services.ts @@ -98,7 +98,7 @@ async function fetchPisCode( } // Check if no public stops - then it should use an ECS headcode let pisData: OB_Pis_SimpleObject | null - if (tiplocList) { + if (tiplocList.length) { pisData = await findByTiplocArray(tiplocList); } else { pisData = { -- 2.34.1 From 88cf7d3d159d9336af27391fc86c8d326f8d9b71 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 5 Feb 2024 19:55:10 +0000 Subject: [PATCH 31/36] Re-implement a working auth middleware Signed-off-by: Fred Boniface --- app.js | 1 + package-lock.json | 89 ++++++++++++++++++++++++++++ package.json | 1 + src/controllers/train.controllers.js | 6 +- src/middlewares/auth.middlewares.js | 40 ------------- src/middlewares/auth.middlewares.ts | 42 +++++++++++++ src/types/index.d.ts | 12 ++++ 7 files changed, 148 insertions(+), 43 deletions(-) delete mode 100644 src/middlewares/auth.middlewares.js create mode 100644 src/middlewares/auth.middlewares.ts create mode 100644 src/types/index.d.ts diff --git a/app.js b/app.js index 9b6f6b8..9798d8c 100644 --- a/app.js +++ b/app.js @@ -27,6 +27,7 @@ const live2Rtr = require("./src/routes/live.routes"); // API Version 2 Routes const tt2Rtr = require("./src/routes/timetable.routes"); // API Version 2 const user2Rtr = require("./src/routes/user.routes"); // API Version 2 Routes const miscRtr = require("./src/routes/misc.routes"); // Non-Public API Routes +const testRtr = require("./src/routes/test.routes"); // Set Server Configurations const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0"; diff --git a/package-lock.json b/package-lock.json index b1c016d..cd41d93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,6 +25,7 @@ }, "devDependencies": { "@owlboard/ts-types": "^0.1.8", + "@types/express": "^4.17.21", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", @@ -2636,6 +2637,49 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/body-parser": { + "version": "1.19.5", + "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.5.tgz", + "integrity": "sha512-fB3Zu92ucau0iQ0JMCFQE7b/dv8Ot07NI3KaZIkIUNXq82k4eBAqUaneXfleGY9JWskeS9y+u0nXMyspcuQrCg==", + "dev": true, + "dependencies": { + "@types/connect": "*", + "@types/node": "*" + } + }, + "node_modules/@types/connect": { + "version": "3.4.38", + "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz", + "integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/express": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.21.tgz", + "integrity": "sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==", + "dev": true, + "dependencies": { + "@types/body-parser": "*", + "@types/express-serve-static-core": "^4.17.33", + "@types/qs": "*", + "@types/serve-static": "*" + } + }, + "node_modules/@types/express-serve-static-core": { + "version": "4.17.43", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.43.tgz", + "integrity": "sha512-oaYtiBirUOPQGSWNGPWnzyAFJ0BP3cwvN4oWZQY+zUBwpVIGsKUkpBpSztp74drYcjavs7SKFZ4DX1V2QeN8rg==", + "dev": true, + "dependencies": { + "@types/node": "*", + "@types/qs": "*", + "@types/range-parser": "*", + "@types/send": "*" + } + }, "node_modules/@types/graceful-fs": { "version": "4.1.6", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.6.tgz", @@ -2645,6 +2689,12 @@ "@types/node": "*" } }, + "node_modules/@types/http-errors": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.4.tgz", + "integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==", + "dev": true + }, "node_modules/@types/istanbul-lib-coverage": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz", @@ -2679,11 +2729,50 @@ "pretty-format": "^29.0.0" } }, + "node_modules/@types/mime": { + "version": "1.3.5", + "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz", + "integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==", + "dev": true + }, "node_modules/@types/node": { "version": "20.6.2", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.2.tgz", "integrity": "sha512-Y+/1vGBHV/cYk6OI1Na/LHzwnlNCAfU3ZNGrc1LdRe/LAIbdDPTTv/HU3M7yXN448aTVDq3eKRm2cg7iKLb8gw==" }, + "node_modules/@types/qs": { + "version": "6.9.11", + "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.11.tgz", + "integrity": "sha512-oGk0gmhnEJK4Yyk+oI7EfXsLayXatCWPHary1MtcmbAifkobT9cM9yutG/hZKIseOU0MqbIwQ/u2nn/Gb+ltuQ==", + "dev": true + }, + "node_modules/@types/range-parser": { + "version": "1.2.7", + "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz", + "integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==", + "dev": true + }, + "node_modules/@types/send": { + "version": "0.17.4", + "resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.4.tgz", + "integrity": "sha512-x2EM6TJOybec7c52BX0ZspPodMsQUd5L6PRwOunVyVUhXiBSKf3AezDL8Dgvgt5o0UfKNfuA0eMLr2wLT4AiBA==", + "dev": true, + "dependencies": { + "@types/mime": "^1", + "@types/node": "*" + } + }, + "node_modules/@types/serve-static": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.5.tgz", + "integrity": "sha512-PDRk21MnK70hja/YF8AHfC7yIsiQHn1rcXx7ijCFBX/k+XQJhQT/gw3xekXKJvx+5SXaMMS8oqQy09Mzvz2TuQ==", + "dev": true, + "dependencies": { + "@types/http-errors": "*", + "@types/mime": "*", + "@types/node": "*" + } + }, "node_modules/@types/stack-utils": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.1.tgz", diff --git a/package.json b/package.json index fe5b269..3f592f3 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ }, "devDependencies": { "@owlboard/ts-types": "^0.1.8", + "@types/express": "^4.17.21", "@types/jest": "^29.5.3", "eslint": "^8.39.0", "jest": "^29.6.2", diff --git a/src/controllers/train.controllers.js b/src/controllers/train.controllers.js index dc65e80..dcf6b0e 100644 --- a/src/controllers/train.controllers.js +++ b/src/controllers/train.controllers.js @@ -17,12 +17,12 @@ async function getByHeadcodeToday(req, res, next) { } async function get(req, res, next) { -/* if (!req.isAuthed) { + if (!req.isAuthed) { const err = new Error("Unauthorized"); err.status = 401; next(err); - } -_-_-_ TEMP DISABLE AUTH _-_-_- -*/ let date = req.params.date; + } + let date = req.params.date; let searchType = req.params.searchType; let id = req.params.id; try { diff --git a/src/middlewares/auth.middlewares.js b/src/middlewares/auth.middlewares.js deleted file mode 100644 index 77fdf3c..0000000 --- a/src/middlewares/auth.middlewares.js +++ /dev/null @@ -1,40 +0,0 @@ -const utils = require("../utils/auth.utils"); -const logger = require("../utils/logger.utils"); - -module.exports = async function authCheck(req, res, next) { - //log.out("authMiddlewares: Checking authentication", "dbug"); - logger.logger.debug("Auth check starting"); - if (process.env.NODE_ENV === "development") { - logger.logger.warn("DEVELOPMENT MODE - AUTHENTICATION DISABLED"); - res.isAuthed = true; - return next(); - } - try { - var uuid = req.headers.uuid; - } catch (err) { - logger.logger.warn("Unable to read UUID header - Not authenticated"); - req.isAuthed = false; - return next(); - } - try { - var result = (await utils.isAuthed(uuid)) || false; - if (!result) { - req.isAuthed = false; - //log.out("authMiddlewares: User !isAuthed", "dbug"); - logger.logger.debug("Auth denied"); - } else { - req.isAuthed = true; - //log.out("authMiddlewares: User isAuthed", "dbug"); - logger.logger.debug("Auth successful"); - } - return next(); - } catch (err) { - /*log.out( - "authMiddlewares: Unable to check auth, default to !isAuthed", - "warn" - );*/ - logger.logger.error(err, `Auth check failed`); - req.isAuthed = false; - return next(err); - } -}; diff --git a/src/middlewares/auth.middlewares.ts b/src/middlewares/auth.middlewares.ts new file mode 100644 index 0000000..41b5468 --- /dev/null +++ b/src/middlewares/auth.middlewares.ts @@ -0,0 +1,42 @@ +import { NextFunction, Request, Response } from "express"; + +const utils = require("../utils/auth.utils"); +const logger = require("../utils/logger.utils"); + +module.exports = async function authCheck(req: Request, res: Response, next: NextFunction) { + logger.logger.debug("auth.middleware: Auth check begun"); + if (process.env.NODE_ENV === "development") { + req.isAuthed = true; + logger.logger.warn("auth.middleware: DEV MODE - Access Granted"); + next(); + } else { + const id: string | string[] | undefined = req.headers.uuid; + if (typeof id === 'undefined') { + req.isAuthed = false; + logger.logger.info("auth.middleware: Authentication failed"); + next(); + } else if (typeof id === 'string') { + const authCheck = await utils.isAuthed(id) || false; + if (authCheck) { + req.isAuthed = true; + logger.logger.info("auth.middleware: Authentication Successful"); + next(); + } else { + req.isAuthed = false; + logger.logger.info("auth.middleware: Authentication Failed"); + next(); + } + } else if (Array.isArray(id)) { + const authCheck = await utils.isAuthed(id[0]) || false; + if (authCheck) { + req.isAuthed = true; + logger.logger.warn("auth.middleware: UUID Passed as Array - Authentication Successful"); + next(); + } else { + req.isAuthed = false; + logger.logger.warn("auth.middleware: UUID Passed as Array - Authentication Failed"); + next(); + } + } + } +}; diff --git a/src/types/index.d.ts b/src/types/index.d.ts new file mode 100644 index 0000000..d6e7e36 --- /dev/null +++ b/src/types/index.d.ts @@ -0,0 +1,12 @@ +// src/types/express/index.d.ts + +// to make the file a module and avoid the TypeScript error +export {} + +declare global { + namespace Express { + export interface Request { + isAuthed: boolean; + } + } +} \ No newline at end of file -- 2.34.1 From 7657085bc3123d77b6fe159935c9b36b7e1e0b1f Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 5 Feb 2024 20:03:01 +0000 Subject: [PATCH 32/36] Fix state where cancelled service is not returned. Signed-off-by: Fred Boniface --- .../timetable/timetableProcessor.utils.ts | 64 +++++++++++-------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/utils/processors/timetable/timetableProcessor.utils.ts b/src/utils/processors/timetable/timetableProcessor.utils.ts index ecbabe2..789d0e7 100644 --- a/src/utils/processors/timetable/timetableProcessor.utils.ts +++ b/src/utils/processors/timetable/timetableProcessor.utils.ts @@ -1,36 +1,46 @@ -import type {Service, OB_TrainTT_service, OB_Pis_SimpleObject, OB_TrainTT_stopDetail, Stop} from "@owlboard/ts-types" +import type { + Service, + OB_TrainTT_service, + OB_Pis_SimpleObject, + OB_TrainTT_stopDetail, + Stop, +} from "@owlboard/ts-types"; +import { logger } from "../../logger.utils"; -export function formatTimetableDetail(service: Service, pis: OB_Pis_SimpleObject | null): OB_TrainTT_service { - const formattedService: OB_TrainTT_service = { - stpIndicator: service.stpIndicator, - operator: service.operator, - trainUid: service.trainUid, - headcode: service.headcode, - powerType: service.powerType, - planSpeed: convertStringToNumber(service.planSpeed), - scheduleStart: service.scheduleStartDate, - scheduleEnd: service.scheduleEndDate, - daysRun: service.daysRun, - stops: formatStops(service.stops), - vstp: service.vstp, - } +export function formatTimetableDetail( + service: Service, + pis: OB_Pis_SimpleObject | null +): OB_TrainTT_service { + const formattedService: OB_TrainTT_service = { + stpIndicator: service.stpIndicator, + operator: service.operator, + trainUid: service.trainUid, + headcode: service.headcode, + powerType: service.powerType, + planSpeed: convertStringToNumber(service.planSpeed), + scheduleStart: service.scheduleStartDate, + scheduleEnd: service.scheduleEndDate, + daysRun: service.daysRun, + stops: formatStops(service.stops), + vstp: service.vstp, + }; - if (pis) { - formattedService.pis = pis; - } + if (pis) { + formattedService.pis = pis; + } - return formattedService + return formattedService; } function formatStops(stops: Stop[]): OB_TrainTT_stopDetail[] { // Cleanly coerce Stop[] to OB_TrainTT_stopDetail[] - const formattedStops: OB_TrainTT_stopDetail[] = [] + const formattedStops: OB_TrainTT_stopDetail[] = []; for (const stop of stops) { - formattedStops.push(formatStopTimes(stop)) + formattedStops.push(formatStopTimes(stop)); } - return formattedStops + return formattedStops; } function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail { @@ -38,7 +48,7 @@ function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail { let formattedStop: OB_TrainTT_stopDetail = { tiploc: stop.tiploc, isPublic: false, - } + }; if (stop.publicArrival) { formattedStop.publicArrival = stop.publicArrival; formattedStop.isPublic = true; @@ -53,14 +63,14 @@ function formatStopTimes(stop: Stop): OB_TrainTT_stopDetail { if (stop.wttDeparture) { formattedStop.wttDeparture = stop.wttDeparture; } - return formattedStop + return formattedStop; } function convertStringToNumber(str: string): number { - const number = parseFloat(str) + const number = parseFloat(str); if (isNaN(number)) { return 0; } else { - return number + return number; } -} \ No newline at end of file +} -- 2.34.1 From a1fdf2280563b6975208f307ffcd5f8f91b346e2 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 5 Feb 2024 20:03:11 +0000 Subject: [PATCH 33/36] Run prettier Signed-off-by: Fred Boniface --- src/middlewares/auth.middlewares.ts | 22 +++++++++++++++------- src/services/trainService.services.ts | 23 +++++++++++------------ src/types/index.d.ts | 4 ++-- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/middlewares/auth.middlewares.ts b/src/middlewares/auth.middlewares.ts index 41b5468..ea6f105 100644 --- a/src/middlewares/auth.middlewares.ts +++ b/src/middlewares/auth.middlewares.ts @@ -3,7 +3,11 @@ import { NextFunction, Request, Response } from "express"; const utils = require("../utils/auth.utils"); const logger = require("../utils/logger.utils"); -module.exports = async function authCheck(req: Request, res: Response, next: NextFunction) { +module.exports = async function authCheck( + req: Request, + res: Response, + next: NextFunction +) { logger.logger.debug("auth.middleware: Auth check begun"); if (process.env.NODE_ENV === "development") { req.isAuthed = true; @@ -11,12 +15,12 @@ module.exports = async function authCheck(req: Request, res: Response, next: Nex next(); } else { const id: string | string[] | undefined = req.headers.uuid; - if (typeof id === 'undefined') { + if (typeof id === "undefined") { req.isAuthed = false; logger.logger.info("auth.middleware: Authentication failed"); next(); - } else if (typeof id === 'string') { - const authCheck = await utils.isAuthed(id) || false; + } else if (typeof id === "string") { + const authCheck = (await utils.isAuthed(id)) || false; if (authCheck) { req.isAuthed = true; logger.logger.info("auth.middleware: Authentication Successful"); @@ -27,14 +31,18 @@ module.exports = async function authCheck(req: Request, res: Response, next: Nex next(); } } else if (Array.isArray(id)) { - const authCheck = await utils.isAuthed(id[0]) || false; + const authCheck = (await utils.isAuthed(id[0])) || false; if (authCheck) { req.isAuthed = true; - logger.logger.warn("auth.middleware: UUID Passed as Array - Authentication Successful"); + logger.logger.warn( + "auth.middleware: UUID Passed as Array - Authentication Successful" + ); next(); } else { req.isAuthed = false; - logger.logger.warn("auth.middleware: UUID Passed as Array - Authentication Failed"); + logger.logger.warn( + "auth.middleware: UUID Passed as Array - Authentication Failed" + ); next(); } } diff --git a/src/services/trainService.services.ts b/src/services/trainService.services.ts index 449758c..995fe32 100644 --- a/src/services/trainService.services.ts +++ b/src/services/trainService.services.ts @@ -80,10 +80,10 @@ export async function findByTrainUid( if (supported.includes(services[0]?.operator)) { pis = await fetchPisCode(services[0]?.stops); } else { - pis = null + pis = null; } // TODO: Format and return data, the function called is not yet complete - return formatTimetableDetail(services[0], pis) + return formatTimetableDetail(services[0], pis); } // Internal Functions: @@ -97,25 +97,25 @@ async function fetchPisCode( if (stops[stop]["isPublic"]) tiplocList.push(stops[stop]["tiploc"]); } // Check if no public stops - then it should use an ECS headcode - let pisData: OB_Pis_SimpleObject | null + let pisData: OB_Pis_SimpleObject | null; if (tiplocList.length) { pisData = await findByTiplocArray(tiplocList); } else { pisData = { toc: "GW", skipCount: 0, - code: randomEcsPis() - } + code: randomEcsPis(), + }; } - + return pisData; } // Picks a random choice of the ECS PIS Codes function randomEcsPis(): string { - const options = ["0015", "9997"] - const randomValue = Math.floor(Math.random() * 2 ) - return options[randomValue] + const options = ["0015", "9997"]; + const randomValue = Math.floor(Math.random() * 2); + return options[randomValue]; } // Outputs the standard 'shortday' string from a Date. @@ -169,9 +169,8 @@ function filterServices(services: SimpleService[]): SimpleService[] { const stpIndicator = service["stpIndicator"]; if (stpIndicator === "C") { - continue; - } - if (stpIndicator === "N" && !thisStpIndicators.hasC) { + filteredServices.push(service); + } else if (stpIndicator === "N" && !thisStpIndicators.hasC) { filteredServices.push(service); } else if ( stpIndicator === "O" && diff --git a/src/types/index.d.ts b/src/types/index.d.ts index d6e7e36..48fb262 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -1,7 +1,7 @@ // src/types/express/index.d.ts // to make the file a module and avoid the TypeScript error -export {} +export {}; declare global { namespace Express { @@ -9,4 +9,4 @@ declare global { isAuthed: boolean; } } -} \ No newline at end of file +} -- 2.34.1 From 67a50b2f94af3b36b9faf566a7e4163c837640d2 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 5 Feb 2024 20:05:05 +0000 Subject: [PATCH 34/36] Update version strings Signed-off-by: Fred Boniface --- package.json | 2 +- src/configs/version.configs.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 3f592f3..c40d762 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "owlboard", - "version": "1.2.0-dev", + "version": "2024.2.1", "description": "OwlBoard is an API and PWA for live rail departure board in the UK.", "repository": { "type": "git", diff --git a/src/configs/version.configs.ts b/src/configs/version.configs.ts index e061e54..af1e50a 100644 --- a/src/configs/version.configs.ts +++ b/src/configs/version.configs.ts @@ -4,8 +4,8 @@ interface versions { } const version: versions = { - api: ["/api/v1/", "/api/v2"], - app: "2023.10.1", + api: ["/api/v2"], + app: "2024.2.1", }; module.exports = version; -- 2.34.1 From 47056d2daef240cce34b3d603eb449e006158f84 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 5 Feb 2024 20:32:57 +0000 Subject: [PATCH 35/36] Add debug log for missing PIS code Signed-off-by: Fred Boniface --- src/services/trainService.services.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/services/trainService.services.ts b/src/services/trainService.services.ts index 995fe32..8baefdc 100644 --- a/src/services/trainService.services.ts +++ b/src/services/trainService.services.ts @@ -107,7 +107,9 @@ async function fetchPisCode( code: randomEcsPis(), }; } - + if (!pisData) { + logger.debug(tiplocList, "No PIS found for service") + } return pisData; } -- 2.34.1 From 4b69dc3af59cb16b334b5765194ed633e890a848 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 5 Feb 2024 21:25:44 +0000 Subject: [PATCH 36/36] - Ensure headcodes and trainUIDs can be accepted in lower case. - Remove testing endpoints - Add deprecation notices to APIv1 functions Signed-off-by: Fred Boniface --- app.js | 2 -- src/controllers/pis.controllers.js | 6 ------ src/controllers/train.controllers.js | 4 ++++ src/routes/pis.routes.ts | 1 - src/services/trainService.services.ts | 4 ++-- 5 files changed, 6 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index 9798d8c..431483d 100644 --- a/app.js +++ b/app.js @@ -20,14 +20,12 @@ const authenticate = require("./src/middlewares/auth.middlewares"); // Internal Requires const version = require("./src/configs/version.configs"); -//const trainRtr = require("./src/routes/train.routes"); const pis2Rtr = require("./src/routes/pis.routes"); // API Version 2 Routes const ref2Rtr = require("./src/routes/ref.routes"); // API Version 2 Routes const live2Rtr = require("./src/routes/live.routes"); // API Version 2 Routes const tt2Rtr = require("./src/routes/timetable.routes"); // API Version 2 const user2Rtr = require("./src/routes/user.routes"); // API Version 2 Routes const miscRtr = require("./src/routes/misc.routes"); // Non-Public API Routes -const testRtr = require("./src/routes/test.routes"); // Set Server Configurations const srvListen = process.env.OWL_SRV_LISTEN || "0.0.0.0"; diff --git a/src/controllers/pis.controllers.js b/src/controllers/pis.controllers.js index f83970b..bdc1e65 100644 --- a/src/controllers/pis.controllers.js +++ b/src/controllers/pis.controllers.js @@ -47,14 +47,8 @@ async function random(req, res, next) { } } -async function testingTiplocArray(req, res, next) { - let array = JSON.parse(req.params.array); - res.json(await pis.findByTiplocArray(array)); -} - module.exports = { byStartEndCRS, byCode, random, - testingTiplocArray, }; diff --git a/src/controllers/train.controllers.js b/src/controllers/train.controllers.js index dcf6b0e..986b59f 100644 --- a/src/controllers/train.controllers.js +++ b/src/controllers/train.controllers.js @@ -1,6 +1,10 @@ +import { logger } from "../utils/logger.utils"; + const train = require("../services/trainService.services"); async function getByHeadcodeToday(req, res, next) { + // Deprecated - for future removal. + logger.warn("Deprecated Function Called - trainService.services-getByHeadcodeToday") if (!req.isAuthed) { const err = new Error("Unauthorized"); err.status = 401; diff --git a/src/routes/pis.routes.ts b/src/routes/pis.routes.ts index 6e2fc63..6a9aee8 100644 --- a/src/routes/pis.routes.ts +++ b/src/routes/pis.routes.ts @@ -5,6 +5,5 @@ const pisCtr = require("../controllers/pis.controllers"); // PIS router.get("/byCode/:code", pisCtr.byCode); router.get("/byStartEnd/:startCrs/:endCrs", pisCtr.byStartEndCRS); -router.get("/test/bytiplocarray/:array", pisCtr.testingTiplocArray); module.exports = router; diff --git a/src/services/trainService.services.ts b/src/services/trainService.services.ts index 8baefdc..f013651 100644 --- a/src/services/trainService.services.ts +++ b/src/services/trainService.services.ts @@ -33,7 +33,7 @@ export async function findByHeadcode( const shortDay = getShortDay(searchDate); const query = { - headcode: sanitizedHeadcode, + headcode: sanitizedHeadcode.toUpperCase(), scheduleStartDate: { $lte: searchDate }, scheduleEndDate: { $gte: searchDate }, daysRun: { $in: [shortDay] }, @@ -64,7 +64,7 @@ export async function findByTrainUid( // Build query const query = { - trainUid: uid, + trainUid: uid.toUpperCase(), scheduleStartDate: { $lte: queryDate }, scheduleEndDate: { $gte: queryDate }, daysRun: { $in: [getShortDay(queryDate)] }, -- 2.34.1