diff --git a/src/services/ldb.services.js b/src/services/ldb.services.js index eca2df7..7b9a08d 100644 --- a/src/services/ldb.services.js +++ b/src/services/ldb.services.js @@ -40,7 +40,8 @@ async function arrDepBoard(CRS){ crs: CRS.toUpperCase() }; const api = new ldb(ldbKey,false); - return await api.call('GetArrDepBoardWithDetails', options, false, false); + let d = await api.call('GetArrDepBoardWithDetails', options, false, false); + return await util.cleanData(d); } catch (err) { log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`, 'warn'); return { @@ -62,10 +63,7 @@ async function arrDepBoardStaff(CRS) { services: 'PBS' }; const api = new ldb(ldbsvKey,true); - let data = await api.call( - 'GetArrivalDepartureBoardByCRS', - options, false, false); - return await util.cleanData(data); + return await api.call('GetArrivalDepartureBoardByCRS',options,false,false); } catch (err) { log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}`, 'warn'); log.out(`ldbService.arrDepBoardStaff: ${err}`); diff --git a/src/utils/ldb.utils.js b/src/utils/ldb.utils.js index 5646ad5..bccd4a8 100644 --- a/src/utils/ldb.utils.js +++ b/src/utils/ldb.utils.js @@ -46,21 +46,30 @@ async function cleanServices(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; + try { + if (input?.GetStationBoardResult?.trainServices) { + log.out('ldbUtils.cleanData: Changing train service data to array','dbug'); + input.GetStationBoardResult.trainServices.service = await ensureArray(input.GetStationBoardResult.trainServices.service); + } + if (input?.GetStationBoardResult?.busServices) { + log.out('ldbUtils.cleanData: Changing bus service data to array','dbug'); + input.GetStationBoardResult.busServices.service = await ensureArray(input.GetStationBoardResult.busServices.service); + } + if (input?.GetStationBoardResult?.ferryServices) { + log.out('ldbUtils.cleanData: Changing ferry service data to array','dbug'); + input.GetStationBoardResult.ferryServices.service = await ensureArray(input.GetStationBoardResult.ferryServices.service); + } + } catch (err) { + log.out(`ldbUtils.cleanData: Error: ${err}`, 'eror'); } - 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; + return input; +} + +async function ensureArray(data) { + if (!Array.isArray(data)) { + return [data]; } + return data; } module.exports = {