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