inject logger

This commit is contained in:
Alex Holliday committed 2026-01-19 21:58:48 +00:00
1 parent 7c9410f9d7
commit bebb5d6942
9 files changed
+125 -18

No files matched your search

@@ -1,16 +1,22 @@
import type { Monitor, Alert, Notification, MonitorStatusResponse } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js";
import { buildHardwareAlerts, buildHardwareWebhookBody, buildWebhookBody } from "@/service/infrastructure/notificationProviders/utils.js";
import {
buildHardwareAlerts,
buildHardwareWebhookBody,
buildWebhookBody,
getTestMessage,
} from "@/service/infrastructure/notificationProviders/utils.js";
import got from "got";
export class WebhookProvider implements INotificationProvider {
constructor(private logger: any) {}
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
const body = buildHardwareWebhookBody(alertsToSend, monitor);
return body;
};
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): string => {
const body = buildWebhookBody(monitor, monitorStatusResponse);
return body;
};
@@ -28,8 +34,9 @@ export class WebhookProvider implements INotificationProvider {
}
try {
this.logger?.debug?.("Sending webhook alert", { address: notification.address });
await got.post(notification.address, {
json: body,
json: { text: body },
headers: {
"Content-Type": "application/json",
},
@@ -37,11 +44,28 @@ export class WebhookProvider implements INotificationProvider {
});
return true;
} catch (error) {
this.logger?.error?.("Webhook alert failed", { error });
return false;
}
};
sendTestAlert = async (notification: Notification) => {
return false;
if (!notification.address) {
return false;
}
try {
this.logger?.debug?.("Sending webhook test alert", { address: notification.address });
await got.post(notification.address, {
json: { text: getTestMessage() },
headers: {
"Content-Type": "application/json",
},
responseType: "json",
});
return true;
} catch (error) {
this.logger?.error?.("Webhook test alert failed", { error });
return false;
}
};
}