Ensure services is always an array

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-07-01 21:40:06 +01:00
parent cf87c1a2b6
commit ce3033c455
2 changed files with 25 additions and 18 deletions

View File

@ -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}`);

View File

@ -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 = {