Add find by headcode including PIS

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-06-01 12:40:43 +01:00
parent 4ba93fdfa8
commit 524e9b271d
5 changed files with 30 additions and 21 deletions

View File

@ -1,6 +1,6 @@
const version = { const version = {
api: ['/api/v1/',], api: ['/api/v1/',],
app: '2023.5.5' app: '2023.6.1'
}; };
module.exports = version; module.exports = version;

View File

@ -1,9 +1,9 @@
const train = require('../services/trainService.services'); const train = require('../services/trainService.services');
async function getByHeadcode(req, res, next){ async function getByHeadcodeToday(req, res, next){
try { try {
var searchHeadcode = req.params.id; var searchHeadcode = req.params.id;
res.json(await train.findByHeadcode(searchHeadcode)); res.json(await train.findByHeadcodeToday(searchHeadcode));
} catch (err) { } catch (err) {
console.error('Unknown Error', err.message); console.error('Unknown Error', err.message);
err.status = 500; err.status = 500;
@ -12,5 +12,5 @@ async function getByHeadcode(req, res, next){
} }
module.exports = { module.exports = {
getByHeadcode getByHeadcodeToday
}; };

View File

@ -14,6 +14,6 @@ const trainController = require('../controllers/train.controllers');
/* DELETE programming language */ /* DELETE programming language */
//router.delete('/:id', programmingLanguagesController.remove); //router.delete('/:id', programmingLanguagesController.remove);
router.get('/headcode/:id', trainController.getByHeadcode); router.get('/headcode/today/:id', trainController.getByHeadcodeToday);
module.exports = router; module.exports = router;

View File

@ -42,7 +42,7 @@ async function findByTiplocArray(tiplocArray) {
'tiplocs': tiplocArray 'tiplocs': tiplocArray
}; };
const res = db.query('pis', query); const res = db.query('pis', query);
return res; return await res;
} }
// Hopefully at some point, I will also be able to implement a find PIS code by headcode option. // Hopefully at some point, I will also be able to implement a find PIS code by headcode option.

View File

@ -3,7 +3,7 @@ const db = require('./dbAccess.services');
const clean = require('../utils/sanitizer.utils'); const clean = require('../utils/sanitizer.utils');
const pis = require('../services/pis.services'); const pis = require('../services/pis.services');
async function findByHeadcode(headcode) { async function findByHeadcodeToday(headcode) {
const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode); const sanitizedHeadcode = clean.removeNonAlphanumeric(headcode);
log.out(`trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}`, 'dbug'); log.out(`trainServiceServices.findByHeadcode: Searching for headcode ${sanitizedHeadcode}`, 'dbug');
const now = new Date(); const now = new Date();
@ -15,23 +15,32 @@ async function findByHeadcode(headcode) {
scheduleEndDate: {$gte: now}, scheduleEndDate: {$gte: now},
daysRun: {$in: [shortDay]} daysRun: {$in: [shortDay]}
}; };
const queryData = await db.query('timetable', query);
let trainData = queryData;
// At this point, the returned objects need sorting to ensure the correct object is returned. // At this point, the returned objects need sorting to ensure the correct object is returned.
// C, N, O, P is the order - C being the preferred, then N, then O and finally P. // C, N, O, P is the order - C being the preferred, then N, then O and finally P.
const trainData = db.query('timetable', query); let preparedData = [];
return trainData; // Use pis.findByTiplocArray() to return a PIS code alongside the data for (const trainService in trainData) {
// eg. {trainData: {}, pis:5001} // Search for PIS Code for each service
// Will need to do something like: let tiplocList = await getPublicStops(trainData[trainService]['stops']);
/* for i in trainData { const pisDetail = await pis.findByTiplocArray(tiplocList);
let tiplocArray = [] trainData[trainService]['pis'] = pisDetail?.[0]?.['code'] ?? 'None';
for ii in i['stops'] { trainData[trainService]['publicStops'] = tiplocList;
get TIPLOC preparedData.push(trainData[trainService]);
push TIPLOC to tiplocArray }
} return preparedData;
const pis = pis.findByTiplocArray(tiplocArray) }
}
*/ async function getPublicStops(data) {
let tiplocList = [];
for (const publicStop in data) {
if (data[publicStop]['isPublic']) {
tiplocList.push(data[publicStop]['tiploc']);
}
}
return tiplocList;
} }
module.exports = { module.exports = {
findByHeadcode, findByHeadcodeToday,
}; };