2023-08-26 00:38:26 +01:00
|
|
|
const log = require("./logs.utils"); // Log Helper
|
|
|
|
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-08-26 00:38:26 +01:00
|
|
|
import * as san from "../utils/sanitizer.utils";
|
2023-07-25 01:00:36 +01:00
|
|
|
|
2023-08-26 00:38:26 +01:00
|
|
|
async function checkCrs(input = "") {
|
2023-05-06 21:54:49 +01:00
|
|
|
var INPUT = input.toUpperCase();
|
|
|
|
var query = {
|
2023-08-26 00:38:26 +01:00
|
|
|
$or: [{ "3ALPHA": INPUT }, { TIPLOC: INPUT }, { STANOX: INPUT }],
|
2023-05-06 21:54:49 +01:00
|
|
|
};
|
2023-08-26 00:38:26 +01:00
|
|
|
var result = await db.query("stations", query);
|
|
|
|
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`
|
2023-08-26 00:38:26 +01:00
|
|
|
async function cleanMessages(input) {
|
|
|
|
log.out("ldbUtils.cleanMessages: Deprecated function has been called", "err");
|
2023-05-06 21:54:49 +01:00
|
|
|
var out = [];
|
2023-08-26 00:38:26 +01:00
|
|
|
if (typeof input.message == "string") {
|
2023-07-25 01:00:36 +01:00
|
|
|
out.push(san.cleanNrcc(input.message));
|
2023-08-26 00:38:26 +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-08-26 00:38:26 +01:00
|
|
|
async function cleanServices(input) {
|
|
|
|
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-08-26 00:38:26 +01:00
|
|
|
log.out(`ldbUtils.cleanServices: Transforming input: ${input}`, "dbug");
|
2023-05-06 21:54:49 +01:00
|
|
|
out.push(input);
|
2023-08-26 00:38:26 +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) {
|
2023-08-26 00:38:26 +01:00
|
|
|
log.out(
|
|
|
|
"ldbUtils.cleanData: Changing train service data to array",
|
|
|
|
"dbug"
|
|
|
|
);
|
|
|
|
input.GetStationBoardResult.trainServices.service = await ensureArray(
|
|
|
|
input.GetStationBoardResult.trainServices.service
|
|
|
|
);
|
2023-07-01 21:40:06 +01:00
|
|
|
}
|
|
|
|
if (input?.GetStationBoardResult?.busServices) {
|
2023-08-26 00:38:26 +01:00
|
|
|
log.out("ldbUtils.cleanData: Changing bus service data to array", "dbug");
|
|
|
|
input.GetStationBoardResult.busServices.service = await ensureArray(
|
|
|
|
input.GetStationBoardResult.busServices.service
|
|
|
|
);
|
2023-07-01 21:40:06 +01:00
|
|
|
}
|
|
|
|
if (input?.GetStationBoardResult?.ferryServices) {
|
2023-08-26 00:38:26 +01:00
|
|
|
log.out(
|
|
|
|
"ldbUtils.cleanData: Changing ferry service data to array",
|
|
|
|
"dbug"
|
|
|
|
);
|
|
|
|
input.GetStationBoardResult.ferryServices.service = await ensureArray(
|
|
|
|
input.GetStationBoardResult.ferryServices.service
|
|
|
|
);
|
2023-07-01 21:40:06 +01:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2023-08-26 00:38:26 +01:00
|
|
|
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,
|
2023-08-26 00:38:26 +01:00
|
|
|
cleanData,
|
|
|
|
};
|