backend/src/utils/ldb.utils.js

71 lines
2.5 KiB
JavaScript
Raw Normal View History

const log = require('../utils/log.utils'); // Log Helper
const db = require('../services/dbAccess.services'); // DB Access
const san = require('../utils/sanitizer.utils'); // Sanitiser
async function checkCrs(input){
var INPUT = input.toUpperCase();
var query = {
'$or':[
{'3ALPHA':INPUT},
{'TIPLOC':INPUT},
{'STANOX':INPUT}
]
};
var result = await db.query('stations', query);
log.out('ldbUtils.checkCrs: Query results: ' +
JSON.stringify(result), 'dbug');
return result;
}
// Needs to be moved to the frontend `ensureArray() func`
async function cleanMessages(input){
log.out('ldbUtils.cleanMessages: Deprecated function has been called', 'err');
var out = [];
if (typeof input.message == 'string') {
out.push(await san.cleanNrcc(input.message));
} else if (typeof input.message == 'object') {
for(var i = 0; i < input.message.length; i++) {
out.push(await san.cleanNrcc(input.message[i]));
}
}
return out;
}
// Accepts an object but not an Array and returns it wrapped in an array.
async function cleanServices(input){
log.out('ldbUtils.cleanServices: Deprecated function has been called', 'err');
var out = [];
if (!Array.isArray(input)) {
log.out(`ldbUtils.cleanServices: Transforming input: ${input}`, 'dbug');
out.push(input);
log.out(`ldbUtils.cleanServices: Returning output: ${out}`, 'dbug');
return out;
} else {
return 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;
}
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;
}
}
module.exports = {
checkCrs,
cleanMessages,
cleanServices,
cleanData
};