Port ldb module to new logger

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-09-26 21:06:18 +01:00
parent f29ff277ce
commit 9a6679f0d9

View File

@ -1,11 +1,12 @@
// Parse and return an LDB Request // Parse and return an LDB Request
const log = require("../utils/logs.utils"); // Log Helper
const ldb = require("ldbs-json"); const ldb = require("ldbs-json");
const util = require("../utils/ldb.utils"); const util = require("../utils/ldb.utils");
const san = require("../utils/sanitizer.utils"); const san = require("../utils/sanitizer.utils");
const db = require("../services/dbAccess.services"); const db = require("../services/dbAccess.services");
import { logger } from "../utils/logger.utils";
import { transform as staffStationTransform } from "../utils/translators/ldb/staffStation"; import { transform as staffStationTransform } from "../utils/translators/ldb/staffStation";
import { msgCodes } from "../configs/errorCodes.configs"; import { msgCodes } from "../configs/errorCodes.configs";
@ -17,7 +18,7 @@ async function get(id, staff = false) {
const obj = await util.checkCrs(cleanId); const obj = await util.checkCrs(cleanId);
try { try {
const crs = obj[0]["3ALPHA"]; const crs = obj[0]["3ALPHA"];
log.out(`ldbService.get: Determined CRS for lookup to be: ${crs}`, "info"); logger.debug(`ldbService.get: Determined CRS for lookup to be: ${crs}`)
if (staff) { if (staff) {
const data = arrDepBoardStaff(crs); const data = arrDepBoardStaff(crs);
db.increment("ldbsvws"); db.increment("ldbsvws");
@ -28,7 +29,7 @@ async function get(id, staff = false) {
return await data; return await data;
} }
} catch (err) { } catch (err) {
log.out(`ldbService.get: Error, Unable to find CRS: ${err}`, "info"); logger.error(err, "ldbService.get: Error, Unable to find CRS")
return { return {
obStatus: "LOC_NOT_FOUND", obStatus: "LOC_NOT_FOUND",
obMsg: "UNABLE TO FIND MESSAGE", obMsg: "UNABLE TO FIND MESSAGE",
@ -37,7 +38,7 @@ async function get(id, staff = false) {
} }
async function arrDepBoard(CRS) { async function arrDepBoard(CRS) {
log.out(`ldbService.arrDepBoard: Trying to fetch board for ${CRS}`, "info"); logger.trace(`ldbService.arrDepBoard: Trying to fetch board for ${CRS}`)
try { try {
const options = { const options = {
numRows: 10, numRows: 10,
@ -47,7 +48,7 @@ async function arrDepBoard(CRS) {
let d = await api.call("GetArrDepBoardWithDetails", options, false, false); let d = await api.call("GetArrDepBoardWithDetails", options, false, false);
return await util.cleanData(d); return await util.cleanData(d);
} catch (err) { } catch (err) {
log.out(`ldbService.arrDepBoard: Lookup Failed for: ${CRS}`, "warn"); logger.error(err, "ldbService.arrDepBoard: Lookup Failed")
return { return {
GetStationBoardResult: "not available", GetStationBoardResult: "not available",
Reason: `The CRS code ${CRS} is not valid`, Reason: `The CRS code ${CRS} is not valid`,
@ -56,7 +57,7 @@ async function arrDepBoard(CRS) {
} }
async function arrDepBoardStaff(CRS) { async function arrDepBoardStaff(CRS) {
log.out(`ldbService.arrDepBoardStaff: Try to fetch board for ${CRS}`, "dbug"); logger.debug(`ldbService.arrDepBoardStaff: Try to fetch board for ${CRS}`)
try { try {
const options = { const options = {
numRows: 40, numRows: 40,
@ -74,23 +75,24 @@ async function arrDepBoardStaff(CRS) {
false, false,
false false
); );
console.log("\n\n\nORIGINAL DATA")
console.log("\n" + result + "\n\n\n")
console.timeEnd(`Fetch Staff LDB for ${CRS.toUpperCase()}`); console.timeEnd(`Fetch Staff LDB for ${CRS.toUpperCase()}`);
try { try {
const _staffLdb = staffStationTransform(result); const _staffLdb = staffStationTransform(result);
console.log("Transformation Test Successful"); logger.debug("StaffLDB Transformed")
console.log(JSON.stringify(_staffLdb)); logger.trace(_staffLdb, "StaffLDB Transformed")
return { return {
obStatus: "OK", obStatus: "OK",
obMsg: "OK", obMsg: "OK",
data: _staffLdb, data: _staffLdb,
}; };
} catch (err) { } catch (err) {
console.log("Transformation Test Failed: " + err); logger.error(err, "Transformation Error")
} }
return result; return result;
} catch (err) { } catch (err) {
log.out(`ldbService.arrDepBoardStaff: Lookup Failed for: ${CRS}`, "warn"); logger.error(err, "ldbService.arrDepBoardStaff error")
log.out(`ldbService.arrDepBoardStaff: ${err}`);
return { return {
GetStationBoardResult: "not available", GetStationBoardResult: "not available",
Reason: `The CRS code ${CRS} is not valid`, Reason: `The CRS code ${CRS} is not valid`,
@ -99,7 +101,7 @@ async function arrDepBoardStaff(CRS) {
} }
async function getServiceByRID(rid) { async function getServiceByRID(rid) {
log.out(`ldbService.getServiceByRID: Finding RID: ${rid}`, "dbug"); logger.debug(`ldbService.getServiceByRID: Finding RID: ${rid}`);
try { try {
const options = { const options = {
rid: String(rid), rid: String(rid),
@ -107,12 +109,12 @@ async function getServiceByRID(rid) {
const api = new ldb(ldbsvKey, true); const api = new ldb(ldbsvKey, true);
return await api.call("GetServiceDetailsByRID", options, false, false); return await api.call("GetServiceDetailsByRID", options, false, false);
} catch (err) { } catch (err) {
log.out(`ldbService.queryService: ${err}`, "EROR"); logger.error(err, `ldbService.queryService`);
} }
} }
async function getServicesByOther(id) { async function getServicesByOther(id) {
log.out(`ldbService.getServiceByOther: Finding services: ${id}`, "dbug"); logger.debug(`ldbService.getServiceByOther: Finding services: ${id}`);
try { try {
const options = { const options = {
serviceID: id, serviceID: id,
@ -121,30 +123,30 @@ async function getServicesByOther(id) {
const api = new ldb(ldbsvKey, true); const api = new ldb(ldbsvKey, true);
return await api.call("QueryServices", options, false, false); return await api.call("QueryServices", options, false, false);
} catch (err) { } catch (err) {
log.out(`ldbService.getServiceByOther: Error: ${err}`, "EROR"); logger.error(err, "ldbService.getServiceByOther")
return false; return false;
} }
} }
async function getReasonCodeList() { async function getReasonCodeList() {
log.out("ldbService.getReasonCodeList: Fetching reason code list", "eror"); logger.debug("ldbService.getReasonCodeList: Fetching reason code list")
try { try {
const dbFilter = {}; const dbFilter = {};
return await db.query("reasonCodes", dbFilter, false); return await db.query("reasonCodes", dbFilter, false);
} catch (err) { } catch (err) {
log.out(`ldbService.getReasonCodeList: ${err}`, "eror"); logger.error(err, "ldbService.getReasonCodeList")
} }
} }
async function getReasonCode(code) { async function getReasonCode(code) {
log.out(`ldbService.getReasonCode: Fetching reason code ${code}`, "dbug"); logger.debug(`ldbService.getReasonCode: Fetching reason code ${code}`);
try { try {
const dbFilter = { const dbFilter = {
code: code, code: code,
}; };
return await db.query("reasonCodes", dbFilter, false); return await db.query("reasonCodes", dbFilter, false);
} catch (err) { } catch (err) {
log.out(`ldbService.getReasonCode: ${err}`, "eror"); logger.error(err, "ldbService.getReasonCode")
} }
} }