Add find by headcode for train services
Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
parent
cb8d53ae36
commit
f6190bb1c9
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -10,5 +10,6 @@
|
|||||||
"editor.formatOnSave": true, // optional
|
"editor.formatOnSave": true, // optional
|
||||||
"editor.formatOnSaveMode": "file", // required to format on save
|
"editor.formatOnSaveMode": "file", // required to format on save
|
||||||
"files.autoSave": "onFocusChange", // optional but recommended
|
"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
|
||||||
}
|
}
|
||||||
|
2
app.js
2
app.js
@ -29,6 +29,7 @@ const issueRtr = require('./src/routes/issue.routes'); // /issue endpoin
|
|||||||
const statRtr = require('./src/routes/stats.routes'); // /stat endpoints
|
const statRtr = require('./src/routes/stats.routes'); // /stat endpoints
|
||||||
const regRtr = require('./src/routes/registration.routes'); // /registration 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
|
// Set Server Configurations
|
||||||
const srvListen = process.env.OWL_SRV_LISTEN || '0.0.0.0';
|
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/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/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/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:
|
// Number of proxies:
|
||||||
app.set('trust proxy', 4);
|
app.set('trust proxy', 4);
|
||||||
|
16
src/controllers/train.controllers.js
Normal file
16
src/controllers/train.controllers.js
Normal 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
|
||||||
|
};
|
19
src/routes/train.routes.js
Normal file
19
src/routes/train.routes.js
Normal 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;
|
22
src/services/trainService.services.js
Normal file
22
src/services/trainService.services.js
Normal 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,
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user