2023-07-28 20:51:43 +01:00
|
|
|
const log = require('./logs.utils'); // Log Helper
|
2023-05-06 21:54:49 +01:00
|
|
|
const db = require('../services/dbAccess.services'); // DB Access
|
2023-07-25 01:00:36 +01:00
|
|
|
//const san = require('../utils/sanitizer.utils'); // Sanitiser
|
2023-02-09 20:34:53 +00:00
|
|
|
|
2023-07-25 01:00:36 +01:00
|
|
|
import * as san from '../utils/sanitizer.utils'
|
|
|
|
|
|
|
|
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') {
|
2023-07-25 01:00:36 +01:00
|
|
|
out.push(san.cleanNrcc(input.message));
|
2023-05-06 21:54:49 +01:00
|
|
|
} else if (typeof input.message == 'object') {
|
|
|
|
for(var i = 0; i < input.message.length; i++) {
|
2023-07-25 01:00:36 +01:00
|
|
|
out.push(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) {
|
2023-07-01 21:40:06 +01:00
|
|
|
try {
|
|
|
|
if (input?.GetStationBoardResult?.trainServices) {
|
|
|
|
log.out('ldbUtils.cleanData: Changing train service data to array','dbug');
|
|
|
|
input.GetStationBoardResult.trainServices.service = await ensureArray(input.GetStationBoardResult.trainServices.service);
|
|
|
|
}
|
|
|
|
if (input?.GetStationBoardResult?.busServices) {
|
|
|
|
log.out('ldbUtils.cleanData: Changing bus service data to array','dbug');
|
|
|
|
input.GetStationBoardResult.busServices.service = await ensureArray(input.GetStationBoardResult.busServices.service);
|
|
|
|
}
|
|
|
|
if (input?.GetStationBoardResult?.ferryServices) {
|
|
|
|
log.out('ldbUtils.cleanData: Changing ferry service data to array','dbug');
|
|
|
|
input.GetStationBoardResult.ferryServices.service = await ensureArray(input.GetStationBoardResult.ferryServices.service);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
log.out(`ldbUtils.cleanData: Error: ${err}`, 'eror');
|
2023-07-01 21:07:18 +01:00
|
|
|
}
|
2023-07-01 21:40:06 +01:00
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function ensureArray(data) {
|
|
|
|
if (!Array.isArray(data)) {
|
|
|
|
return [data];
|
2023-07-01 21:07:18 +01:00
|
|
|
}
|
2023-07-01 21:40:06 +01:00
|
|
|
return data;
|
2023-07-01 21:07:18 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|