From a5a73812a9f8b733787537053ba61db585e5d2cb Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sun, 10 Mar 2024 21:16:21 +0000 Subject: [PATCH] Tidy 'pis' related code Signed-off-by: Fred Boniface --- src/controllers/pis.controllers.js | 54 ------------------------------ src/controllers/pis.controllers.ts | 38 +++++++++++++++++++++ src/services/pis.services.ts | 4 +-- src/utils/sanitizer.utils.ts | 2 -- 4 files changed, 40 insertions(+), 58 deletions(-) delete mode 100644 src/controllers/pis.controllers.js create mode 100644 src/controllers/pis.controllers.ts diff --git a/src/controllers/pis.controllers.js b/src/controllers/pis.controllers.js deleted file mode 100644 index 2b87874..0000000 --- a/src/controllers/pis.controllers.js +++ /dev/null @@ -1,54 +0,0 @@ -const pis = require("../services/pis.services"); - -/* Used in /api/v2 */ -async function byStartEndCRS(req, res, next) { -// if (!req.isAuthed) { -// const err = new Error("Unauthorized"); -// err.status = 401; -// return next(err); -// } - try { - let startCrs = req.params.startCrs; - let endCrs = req.params.endCrs; - res.json(await pis.findPisByOrigDest(startCrs, endCrs)); - } catch (err) { - console.error("Unknown Error", err.message); - return next(err); - } -} - -/* Used in /api/v2 */ -async function byCode(req, res, next) { -// if (!req.isAuthed) { -// const err = new Error("Unauthorized"); -// err.status = 401; -// return next(err); -// } - try { - let code = req.params.code; - res.json(await pis.findPisByCode(code)); - } catch (err) { - console.error("Unknown Error", err.message); - return next(err); - } -} - -async function random(req, res, next) { -// if (!req.isAuthed) { -// const err = new Error("Unauthorized"); -// err.status = 401; -// return next(err); -// } - try { - res.json(await pis.findRandom()); - } catch (err) { - console.error("Unknown Error", err.message); - return next(err); - } -} - -module.exports = { - byStartEndCRS, - byCode, - random, -}; diff --git a/src/controllers/pis.controllers.ts b/src/controllers/pis.controllers.ts new file mode 100644 index 0000000..09a73b0 --- /dev/null +++ b/src/controllers/pis.controllers.ts @@ -0,0 +1,38 @@ +import { Request, Response, NextFunction } from "express"; +import { findPisByOrigDest, findPisByCode } from "../services/pis.services"; + +async function byStartEndCRS(req: Request, res: Response, next: NextFunction) { +// if (!req.isAuthed) { +// const err = new Error("Unauthorized"); +// err.status = 401; +// return next(err); +// } + try { + let startCrs = req.params.startCrs; + let endCrs = req.params.endCrs; + res.json(await findPisByOrigDest(startCrs, endCrs)); + } catch (err: any) { + console.error("Unknown Error", err.message); + return next(err); + } +} + +async function byCode(req: Request, res: Response, next: NextFunction) { +// if (!req.isAuthed) { +// const err = new Error("Unauthorized"); +// err.status = 401; +// return next(err); +// } + try { + let code = req.params.code; + res.json(await findPisByCode(code)); + } catch (err: any) { + console.error("Unknown Error", err.message); + return next(err); + } +} + +module.exports = { + byStartEndCRS, + byCode, +}; diff --git a/src/services/pis.services.ts b/src/services/pis.services.ts index eb2b3a2..34af0b8 100644 --- a/src/services/pis.services.ts +++ b/src/services/pis.services.ts @@ -15,7 +15,7 @@ import { Document } from "mongodb"; export const supported = ["GW", "UK"]; -async function findPisByOrigDest(start: string, end: string) { +export async function findPisByOrigDest(start: string, end: string) { logger.debug( `pisServices.findPisByOrigDest: Searching for Orig: ${start}, Dest: ${end}` ); @@ -40,7 +40,7 @@ async function findPisByOrigDest(start: string, end: string) { return search; } -async function findPisByCode( +export async function findPisByCode( code: string ): Promise { logger.debug(`pisServices.findPisByCode: Searching for PIS code: ${code}`); diff --git a/src/utils/sanitizer.utils.ts b/src/utils/sanitizer.utils.ts index f7ebee8..da078b2 100644 --- a/src/utils/sanitizer.utils.ts +++ b/src/utils/sanitizer.utils.ts @@ -1,5 +1,3 @@ -//const log = require('../utils/log.utils'); - import { logger } from "./logger.utils"; function removeNonAlphanumeric(inputString: string) {