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() crs: CRS.toUpperCase()
}; };
const api = new ldb(ldbKey,false); 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) { } catch (err) {
log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`, 'warn'); log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`, 'warn');
return { return {
@ -62,10 +63,7 @@ async function arrDepBoardStaff(CRS) {
services: 'PBS' services: 'PBS'
}; };
const api = new ldb(ldbsvKey,true); const api = new ldb(ldbsvKey,true);
let data = await api.call( return await api.call('GetArrivalDepartureBoardByCRS',options,false,false);
'GetArrivalDepartureBoardByCRS',
options, false, false);
return await util.cleanData(data);
} catch (err) { } catch (err) {
log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}`, 'warn'); log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}`, 'warn');
log.out(`ldbService.arrDepBoardStaff: ${err}`); log.out(`ldbService.arrDepBoardStaff: ${err}`);

View File

@ -46,21 +46,30 @@ async function cleanServices(input){
} }
async function cleanData(input) { async function cleanData(input) {
if (!Array.isArray(input?.GetStationBoardResult?.trainServices.service)) { try {
log.out('ldbUtils.cleanData: Changing train service data to array', 'warn'); if (input?.GetStationBoardResult?.trainServices) {
const trnServiceArray = [input.GetStationBoardResult.trainServices.service]; log.out('ldbUtils.cleanData: Changing train service data to array','dbug');
input.GetStationBoardResult.trainServices.service = trnServiceArray; input.GetStationBoardResult.trainServices.service = await ensureArray(input.GetStationBoardResult.trainServices.service);
} }
if (!Array.isArray(input?.GetStationBoardResult?.busServices.service)) { if (input?.GetStationBoardResult?.busServices) {
log.out('ldbUtils.cleanData: Changing bus service data to array', 'warn'); log.out('ldbUtils.cleanData: Changing bus service data to array','dbug');
const busServiceArray = [input.GetStationBoardResult.busServices.service]; input.GetStationBoardResult.busServices.service = await ensureArray(input.GetStationBoardResult.busServices.service);
input.GetStationBoardResult.busServices.service = busServiceArray;
} }
if (!Array.isArray(input?.GetStationBoardResult?.ferryServices.service)) { if (input?.GetStationBoardResult?.ferryServices) {
log.out('ldbUtils.cleanData: Changing ferry service data to array', 'warn'); log.out('ldbUtils.cleanData: Changing ferry service data to array','dbug');
const fryServiceArray = [input.GetStationBoardResult.ferryServices.service]; input.GetStationBoardResult.ferryServices.service = await ensureArray(input.GetStationBoardResult.ferryServices.service);
input.GetStationBoardResult.ferryServices.service = fryServiceArray;
} }
} catch (err) {
log.out(`ldbUtils.cleanData: Error: ${err}`, 'eror');
}
return input;
}
async function ensureArray(data) {
if (!Array.isArray(data)) {
return [data];
}
return data;
} }
module.exports = { module.exports = {