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 = {
api: ['/api/v1/',],
app: '2023.5.5'
app: '2023.6.1'
};
module.exports = version;

View File

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

View File

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

View File

@ -42,7 +42,7 @@ async function findByTiplocArray(tiplocArray) {
'tiplocs': tiplocArray
};
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.

View File

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