diff --git a/backend/logger/logger.controller.ts b/backend/logger/logger.controller.ts index d104e55..efce7b0 100644 --- a/backend/logger/logger.controller.ts +++ b/backend/logger/logger.controller.ts @@ -5,6 +5,7 @@ import { db } from "../db/db.controller.js"; import { logs } from "../db/schema/logs.schema.js"; import { emitToRoom } from "../socket.io/roomEmitter.socket.js"; import { tryCatch } from "../utils/trycatch.utils.js"; +import { notifySystemIssue } from "./logger.notify.js"; //import build from "pino-abstract-transport"; export const logLevel = process.env.LOG_LEVEL || "info"; @@ -45,6 +46,10 @@ const dbStream = new Writable({ console.error(res.error); } + if (obj.notify) { + notifySystemIssue(obj); + } + if (obj.room) { emitToRoom(obj.room, res.data ? res.data[0] : obj); } diff --git a/backend/logger/logger.notify.ts b/backend/logger/logger.notify.ts new file mode 100644 index 0000000..646b1ad --- /dev/null +++ b/backend/logger/logger.notify.ts @@ -0,0 +1,44 @@ +/** + * For all logging that has notify set to true well send an email to the system admins, if we have a discord webhook set well send it there as well + */ + +import { eq } from "drizzle-orm"; +import { db } from "../db/db.controller.js"; +import { user } from "../db/schema/auth.schema.js"; +import { sendEmail } from "../utils/sendEmail.utils.js"; + +type NotifyData = { + module: string; + submodule: string; + hostname: string; + msg: string; + stack: unknown[]; +}; + +export const notifySystemIssue = async (data: NotifyData) => { + // build the email out + + const formattedError = Array.isArray(data.stack) + ? data.stack.map((e: any) => e.error || e) + : data.stack; + + const sysAdmin = await db + .select() + .from(user) + .where(eq(user.role, "systemAdmin")); + + await sendEmail({ + email: sysAdmin.map((r) => r.email).join("; ") ?? "cowchmonkey@gmail.com", // change to pull in system admin emails + subject: `${data.hostname} has encountered a critical issue.`, + template: "serverCritialIssue", + context: { + plant: data.hostname, + module: data.module, + subModule: data.submodule, + message: data.msg, + error: JSON.stringify(formattedError, null, 2), + }, + }); + + // TODO: add discord +}; diff --git a/backend/notification/notification.reprintLabels.ts b/backend/notification/notification.reprintLabels.ts index 4e82dd7..2b5508b 100644 --- a/backend/notification/notification.reprintLabels.ts +++ b/backend/notification/notification.reprintLabels.ts @@ -23,7 +23,7 @@ const reprint = async (data: any, emails: string) => { module: "notification", subModule: "query", message: `${data.name} encountered an error while trying to get initial info`, - data: [le], + data: le as any, notify: true, }); } @@ -52,7 +52,7 @@ const reprint = async (data: any, emails: string) => { module: "notification", subModule: "query", message: `Data for: ${l[0].name} encountered an error while trying to get it`, - data: [error], + data: error as any, notify: true, }); } @@ -73,7 +73,7 @@ const reprint = async (data: any, emails: string) => { module: "notification", subModule: "query", message: `Data for: ${l[0].name} encountered an error while trying to get it`, - data: [dbe], + data: dbe as any, notify: true, }); } @@ -90,22 +90,13 @@ const reprint = async (data: any, emails: string) => { }); if (!sentEmail?.success) { - // sendEmail({ - // email: "Blake.matths@alpla.com", - // subject: `${os.hostname()} failed to run ${data[0]?.name}.`, - // template: "serverCrash", - // context: { - // error: sentEmail?.data, - // plant: `${os.hostname()}`, - // }, - // }); return returnFunc({ success: false, level: "error", module: "notification", subModule: "email", message: `${l[0].name} failed to send the email`, - data: [sentEmail?.data], + data: sentEmail?.data as any, notify: true, }); }