Backend: Always return services as array, even if only one

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-01-20 20:34:48 +00:00
parent c03a2e2fb8
commit e4e81d0145
2 changed files with 38 additions and 5 deletions

View File

@ -22,13 +22,32 @@ async function get(body, id){
var crs = obj[0]['3ALPHA']; var crs = obj[0]['3ALPHA'];
log.out(`ldbService.get: Determined CRS for lookup to be: ${crs}`); log.out(`ldbService.get: Determined CRS for lookup to be: ${crs}`);
var data = await arrDepBoard(crs); var data = await arrDepBoard(crs);
} catch (err) {
log.out(`ldbService.get: Error, Unable to find CRS: ${err}`)
var data = {ERROR:'NOT_FOUND',description:'The entered station was not found. Please check and try again.'};
}
try {
if (data.GetStationBoardResult.nrccMessages) { if (data.GetStationBoardResult.nrccMessages) {
var msg = await util.cleanMessages(data.GetStationBoardResult.nrccMessages); var msg = await util.cleanMessages(data.GetStationBoardResult.nrccMessages);
data.GetStationBoardResult.nrccMessages.message = msg; data.GetStationBoardResult.nrccMessages.message = msg;
} }
if (data.GetStationBoardResult.trainServices && !Array.isArray(data.GetStationBoardResult.trainServices.service)) {
log.out(`ldbService.get: Transforming trainServices`)
var services = await util.cleanServices(data.GetStationBoardResult.trainServices.service)
data.GetStationBoardResult.trainServices.service = services;
}
if (data.GetStationBoardResult.ferryServices && !Array.isArray(data.GetStationBoardResult.ferryServices.service)) {
log.out(`ldbService.get: Transforming ferryServices`)
var services = await util.cleanServices(data.GetStationBoardResult.ferryServices.service)
data.GetStationBoardResult.ferryServices.service = services;
}
if (data.GetStationBoardResult.busServices && !Array.isArray(data.GetStationBoardResult.busServices.service)) {
log.out(`ldbService.get: Transforming busServices`)
var services = await util.cleanServices(data.GetStationBoardResult.busServices.service)
data.GetStationBoardResult.busServices.service = services;
}
} catch (err) { } catch (err) {
log.out(`ldbService.get: Error, Unable to find CRS: ${err}`) log.out(`ldbService.get: Error transforming upstream data: ${err}`)
var data = {ERROR:'NOT_FOUND',description:'The entered station was not found. Please check and try again.'};
} }
return data; return data;
} }

View File

@ -23,7 +23,21 @@ async function cleanMessages(input){
return out; return out;
} }
// Accepts an object but not an Array and returns it wrapped in an array.
async function cleanServices(input){
var out = []
if (!Array.isArray(input)) {
log.out(`ldbUtils.cleanServices: Transforming input: ${input}`)
out.push(input)
log.out(`ldbUtils.cleanServices: Returning output: ${out}`)
return out;
} else {
return input;
}
}
module.exports = { module.exports = {
checkCrs, checkCrs,
cleanMessages cleanMessages,
cleanServices
} }