backend/src/utils/ldb.utils.js
Fred Boniface 911aa113ea Comment
Signed-off-by: Fred Boniface <fred@fjla.uk>
2023-09-20 14:27:19 +01:00

94 lines
2.7 KiB
JavaScript

const log = require("./logs.utils"); // Log Helper
const db = require("../services/dbAccess.services"); // DB Access
//const san = require('../utils/sanitizer.utils'); // Sanitiser
import * as san from "../utils/sanitizer.utils";
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`
// Usage of this function should be migrated to the `translator` utilities.
async function cleanMessages(input) {
log.out("ldbUtils.cleanMessages: Deprecated function has been called", "err");
var out = [];
if (typeof input.message == "string") {
out.push(san.cleanNrcc(input.message));
} else if (typeof input.message == "object") {
for (var i = 0; i < input.message.length; i++) {
out.push(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) {
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");
}
return input;
}
async function ensureArray(data) {
if (!Array.isArray(data)) {
return [data];
}
return data;
}
module.exports = {
checkCrs,
cleanMessages,
cleanServices,
cleanData,
};