Add find by headcode for train services

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-05-31 20:04:00 +01:00
parent cb8d53ae36
commit f6190bb1c9
5 changed files with 62 additions and 2 deletions

View File

@ -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
}

4
app.js
View File

@ -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);

View File

@ -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
};

View File

@ -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;

View File

@ -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,
};