backend/src/services/ldb.services.js
Fred Boniface 849c31af36 Add eslint rule: max line length (80)
Signed-off-by: Fred Boniface <fred@fjla.uk>
2023-06-07 21:53:56 +01:00

74 lines
2.1 KiB
JavaScript

// Parse and return an LDB Request
const log = require('../utils/log.utils'); // Log Helper
const ldb = require('ldbs-json');
const util = require('../utils/ldb.utils');
const san = require('../utils/sanitizer.utils');
const db = require('../services/dbAccess.services');
const ldbKey = process.env.OWL_LDB_KEY;
const ldbsvKey = process.env.OWL_LDB_SVKEY;
async function get(id, staff=false){
const cleanId = san.cleanApiEndpointTxt(id);
const obj = await util.checkCrs(cleanId);
try {
const crs = obj[0]['3ALPHA'];
log.out(`ldbService.get: Determined CRS for lookup to be: ${crs}`, 'info');
if (staff) {
const data = arrDepBoardStaff(crs);
db.increment('ldbsvws');
return await data;
} else {
const data = arrDepBoard(crs);
db.increment('ldbws');
return await data;
}
} catch (err) {
log.out(`ldbService.get: Error, Unable to find CRS: ${err}`, 'info');
return {
ERROR:'NOT_FOUND',
description:'The entered station was not found.'};
}
}
async function arrDepBoard(CRS){
log.out(`ldbService.arrDepBoard: Trying to fetch board for ${CRS}`, 'info');
try {
const options = {
numRows: 10,
crs: CRS.toUpperCase()
};
const api = new ldb(ldbKey,false);
return await api.call('GetArrDepBoardWithDetails', options, false, false);
} catch (err) {
log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`, 'warn');
return {
GetStationBoardResult: 'not available',
Reason: `The CRS code ${CRS} is not valid`
};
}
}
async function arrDepBoardStaff(CRS) {
log.out(`ldbService.arrDepBoardStaff: Try to fetch board for ${CRS}`, 'dbug');
try {
const options = {
numRows: 25,
crs: CRS.toUpperCase(),
getNonPassengerServices: true
};
const api = new ldb(ldbsvKey,true);
return await api.call('GetArrDepBoardWithDetails', options, false, false);
} catch (err) {
log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}, "warn`);
return {
GetStationBoardResult: 'not available',
Reason: `The CRS code ${CRS} is not valid`
};
}
}
module.exports = {
get
};