From f6190bb1c99bbf93663e25d3ac998cf2084b1393 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 31 May 2023 20:04:00 +0100 Subject: [PATCH] Add find by headcode for train services Signed-off-by: Fred Boniface --- .vscode/settings.json | 3 ++- app.js | 4 +++- src/controllers/train.controllers.js | 16 ++++++++++++++++ src/routes/train.routes.js | 19 +++++++++++++++++++ src/services/trainService.services.js | 22 ++++++++++++++++++++++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/controllers/train.controllers.js create mode 100644 src/routes/train.routes.js create mode 100644 src/services/trainService.services.js diff --git a/.vscode/settings.json b/.vscode/settings.json index dd4c482..d9252b0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,5 +10,6 @@ "editor.formatOnSave": true, // optional "editor.formatOnSaveMode": "file", // required to format on save "files.autoSave": "onFocusChange", // optional but recommended - "vs-code-prettier-eslint.prettierLast": "false" // set as "true" to run 'prettier' last not first + "vs-code-prettier-eslint.prettierLast": "false", + "editor.tabSize": 2 // set as "true" to run 'prettier' last not first } diff --git a/app.js b/app.js index 9c499e5..97e9366 100644 --- a/app.js +++ b/app.js @@ -28,7 +28,8 @@ const findRtr = require('./src/routes/find.routes'); // /find endpoint const issueRtr = require('./src/routes/issue.routes'); // /issue endpoints const statRtr = require('./src/routes/stats.routes'); // /stat endpoints const regRtr = require('./src/routes/registration.routes'); // /registration endpoints -const pisRtr = require('./src/routes/pis.routes'); // /pis endpoints +const pisRtr = require('./src/routes/pis.routes'); // /pis endpoints +const trainRtr = require('./src/routes/train.routes'); // /train routes // Set Server Configurations const srvListen = process.env.OWL_SRV_LISTEN || '0.0.0.0'; @@ -80,6 +81,7 @@ app.use('/api/v1/register', regRtr); app.use('/api/v1/ldbs', authenticate, (req, res) => res.status(501).json({status: 'Not Implemented', message: 'This feature is not yet implemented due to upstream issues'})); app.use('/api/v1/pis', authenticate, pisRtr); 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/controllers/train.controllers.js b/src/controllers/train.controllers.js new file mode 100644 index 0000000..a1d1a9e --- /dev/null +++ b/src/controllers/train.controllers.js @@ -0,0 +1,16 @@ +const train = require('../services/train.services'); + +async function getByHeadcode(req, res, next){ + try { + var searchHeadcode = req.params.id; + res.json(await train.findByHeadcode(searchHeadcode)); + } catch (err) { + console.error('Unknown Error', err.message); + err.status = 500; + next(err); + } +} + +module.exports = { + getByHeadcode +}; \ No newline at end of file diff --git a/src/routes/train.routes.js b/src/routes/train.routes.js new file mode 100644 index 0000000..210d20b --- /dev/null +++ b/src/routes/train.routes.js @@ -0,0 +1,19 @@ +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/:id', trainController.getByHeadcode); + +module.exports = router; \ No newline at end of file diff --git a/src/services/trainService.services.js b/src/services/trainService.services.js new file mode 100644 index 0000000..0520809 --- /dev/null +++ b/src/services/trainService.services.js @@ -0,0 +1,22 @@ +const log = require('../utils/log.utils'); +const db = require('./dbAccess.services'); +const clean = require('../utils/sanitizer.utils'); + +async function findByHeadcode(headcode) { + const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode); + log.out(`trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}`, 'dbug'); + const now = new Date(); + const dayMap = ['su', 'm', 't', 'w', 'th', 'f', 's']; + const shortDay = dayMap[now.getDay()]; // Fetch short day from map + const query = { + headcode: sanitizedHeadcode, + scheduleStartDate: {$lte: now}, + scheduleEndDate: {$gte: now}, + daysRun: {$in: [shortDay]} + }; + return db.query('timetable', query); +} + +module.exports = { + findByHeadcode, +}; \ No newline at end of file