Bugfix: Cases where operator does not exist

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-07-11 22:43:34 +01:00
parent 24b882ce5f
commit 86e5c34e7f
2 changed files with 14 additions and 14 deletions

View File

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

View File

@ -20,19 +20,19 @@ async function findByHeadcodeToday(headcode) {
let trainData = await parseTrains(queryData); let trainData = await parseTrains(queryData);
let preparedData = []; let preparedData = [];
for (let trainService in trainData) { for (let trainService in trainData) {
if (pis.supported.includes(trainData[trainService]['operator'])) { if (pis.supported.includes(trainService?.operator)) {
// Search for PIS Code for each service // Search for PIS Code for each service
const tiplocList = await getPublicStops(trainData[trainService]['stops']); const tiplocList = await getPublicStops(trainService?.stops);
//console.log(tiplocList.length); console.log(tiplocList); //console.log(tiplocList.length); console.log(tiplocList);
if (tiplocList.length) { if (tiplocList.length) {
const pisDetail = await pis.findByTiplocArray(tiplocList); const pisDetail = await pis.findByTiplocArray(tiplocList);
trainData[trainService]['pis'] = pisDetail?.[0]?.['code'] ?? 'None'; trainService['pis'] = pisDetail?.[0]?.['code'] ?? 'None';
} else if (trainData[trainService]['operator'] === 'GW') { } else if (trainService?.operator === 'GW') {
trainData[trainService]['pis'] = '0015'; // Not in Service code trainService['pis'] = '0015'; // Not in Service code
// '0015' is a string becuase 0015 is not a valid number.. // '0015' is a string becuase 0015 is not a valid number..
} }
} }
preparedData.push(trainData[trainService]); preparedData.push(trainService);
} }
return preparedData; return preparedData;
} }
@ -59,20 +59,20 @@ async function findByHeadcode(date, headcode) {
const queryData = await db.query('timetable', query); const queryData = await db.query('timetable', query);
let trainData = await parseTrains(queryData); let trainData = await parseTrains(queryData);
let preparedData = []; let preparedData = [];
for (let trainService in trainData) { for (let trainService of trainData) {
// Search for PIS Code for each service if supported by PIS services // Search for PIS Code for each service if supported by PIS services
if (pis.supported.includes(trainData[trainService]['operator'])) { if (pis.supported.includes(trainService?.operator)) {
const tiplocList = await getPublicStops(trainData[trainService]['stops']); const tiplocList = await getPublicStops(trainService?.stops);
//console.log(tiplocList.length); console.log(tiplocList); //console.log(tiplocList.length); console.log(tiplocList);
if (tiplocList.length) { if (tiplocList.length) {
const pisDetail = await pis.findByTiplocArray(tiplocList); const pisDetail = await pis.findByTiplocArray(tiplocList);
trainData[trainService]['pis'] = pisDetail?.[0]?.['code'] ?? 'None'; trainService.pis = pisDetail?.[0]?.['code'] ?? 'None';
} else if (trainData[trainService]['operator'] === 'GW' && !tiplocList.length) { } else if (trainService.operator === 'GW' && !tiplocList.length) {
// Not in Service code - for GWR Only // Not in Service code - for GWR Only
trainData[trainService]['pis'] = '0015'; trainService.pis = '0015';
} }
} }
preparedData.push(trainData[trainService]); preparedData.push(trainService);
} }
return preparedData; return preparedData;
} }