From cf87c1a2b6468ab75c3d71e6ca8e0c04d2dadd4f Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 1 Jul 2023 21:07:18 +0100 Subject: [PATCH] Re-implement `ensureArray` Signed-off-by: Fred Boniface --- src/configs/version.configs.js | 2 +- src/services/ldb.services.js | 5 ++++- src/services/pis.services.js | 5 +++++ src/services/trainService.services.js | 18 +++++++++-------- src/utils/ldb.utils.js | 28 +++++++++++++++++++++++---- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/configs/version.configs.js b/src/configs/version.configs.js index 976a313..54dce41 100644 --- a/src/configs/version.configs.js +++ b/src/configs/version.configs.js @@ -1,6 +1,6 @@ const version = { api: ['/api/v1/','/api/v2'], - app: '2023.6.15' + app: '2023.7.1' }; module.exports = version; \ No newline at end of file diff --git a/src/services/ldb.services.js b/src/services/ldb.services.js index da0a32a..eca2df7 100644 --- a/src/services/ldb.services.js +++ b/src/services/ldb.services.js @@ -62,7 +62,10 @@ async function arrDepBoardStaff(CRS) { services: 'PBS' }; const api = new ldb(ldbsvKey,true); - return await api.call('GetArrivalDepartureBoardByCRS',options,false,false); + let data = await api.call( + 'GetArrivalDepartureBoardByCRS', + options, false, false); + return await util.cleanData(data); } catch (err) { log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}`, 'warn'); log.out(`ldbService.arrDepBoardStaff: ${err}`); diff --git a/src/services/pis.services.js b/src/services/pis.services.js index 642cdd9..77d11c8 100644 --- a/src/services/pis.services.js +++ b/src/services/pis.services.js @@ -4,6 +4,10 @@ const db = require('../services/dbAccess.services'); const log = require('../utils/log.utils'); const clean = require('../utils/sanitizer.utils'); +const supported = [ + 'GW' +]; + async function findPisByOrigDest(start,end) { log.out('pisServices.findPisByOrigDest: Searching for PIS for Orig: ' + `${start}, Dest: ${end}`, 'dbug'); @@ -46,6 +50,7 @@ async function findByTiplocArray(tiplocArray) { } module.exports = { + supported, findPisByOrigDest, findPisByCode, findByTiplocArray diff --git a/src/services/trainService.services.js b/src/services/trainService.services.js index 6b367c9..ac8b93d 100644 --- a/src/services/trainService.services.js +++ b/src/services/trainService.services.js @@ -20,15 +20,17 @@ async function findByHeadcodeToday(headcode) { let trainData = await parseTrains(queryData); let preparedData = []; for (let trainService in trainData) { + if (pis.supported.includes(trainData?.operator)) { // Search for PIS Code for each service - const tiplocList = await getPublicStops(trainData[trainService]['stops']); - //console.log(tiplocList.length); console.log(tiplocList); - if (tiplocList.length) { - const pisDetail = await pis.findByTiplocArray(tiplocList); - trainData[trainService]['pis'] = pisDetail?.[0]?.['code'] ?? 'None'; - } else { - trainData[trainService]['pis'] = '0015'; // Not in Service code - // '0015' is a string otherwise it is interpreted as octal 13. + const tiplocList = await getPublicStops(trainData[trainService]['stops']); + //console.log(tiplocList.length); console.log(tiplocList); + if (tiplocList.length) { + const pisDetail = await pis.findByTiplocArray(tiplocList); + trainData[trainService]['pis'] = pisDetail?.[0]?.['code'] ?? 'None'; + } else { + trainData[trainService]['pis'] = '0015'; // Not in Service code + // '0015' is a string becuase 0015 is not a valid number.. + } } preparedData.push(trainData[trainService]); } diff --git a/src/utils/ldb.utils.js b/src/utils/ldb.utils.js index 0395aa6..5646ad5 100644 --- a/src/utils/ldb.utils.js +++ b/src/utils/ldb.utils.js @@ -19,6 +19,7 @@ async function checkCrs(input){ // Needs to be moved to the frontend `ensureArray() func` async function cleanMessages(input){ + log.out('ldbUtils.cleanMessages: Deprecated function has been called', 'err'); var out = []; if (typeof input.message == 'string') { out.push(await san.cleanNrcc(input.message)); @@ -31,21 +32,40 @@ async function cleanMessages(input){ } // Accepts an object but not an Array and returns it wrapped in an array. -// Need to triple check but I don't think this is used anymore. async function cleanServices(input){ + log.out('ldbUtils.cleanServices: Deprecated function has been called', 'err'); var out = []; if (!Array.isArray(input)) { - log.out(`ldbUtils.cleanServices: Transforming input: ${input}`, 'depr'); + log.out(`ldbUtils.cleanServices: Transforming input: ${input}`, 'dbug'); out.push(input); - log.out(`ldbUtils.cleanServices: Returning output: ${out}`, 'depr'); + log.out(`ldbUtils.cleanServices: Returning output: ${out}`, 'dbug'); return out; } else { return input; } } +async function cleanData(input) { + if (!Array.isArray(input?.GetStationBoardResult?.trainServices.service)) { + log.out('ldbUtils.cleanData: Changing train service data to array', 'warn'); + const trnServiceArray = [input.GetStationBoardResult.trainServices.service]; + input.GetStationBoardResult.trainServices.service = trnServiceArray; + } + if (!Array.isArray(input?.GetStationBoardResult?.busServices.service)) { + log.out('ldbUtils.cleanData: Changing bus service data to array', 'warn'); + const busServiceArray = [input.GetStationBoardResult.busServices.service]; + input.GetStationBoardResult.busServices.service = busServiceArray; + } + if (!Array.isArray(input?.GetStationBoardResult?.ferryServices.service)) { + log.out('ldbUtils.cleanData: Changing ferry service data to array', 'warn'); + const fryServiceArray = [input.GetStationBoardResult.ferryServices.service]; + input.GetStationBoardResult.ferryServices.service = fryServiceArray; + } +} + module.exports = { checkCrs, cleanMessages, - cleanServices + cleanServices, + cleanData }; \ No newline at end of file