diff --git a/server/src/config/services.ts b/server/src/config/services.ts index 49e6f3d5e..92acd8868 100644 --- a/server/src/config/services.ts +++ b/server/src/config/services.ts @@ -8,6 +8,7 @@ import BufferService from "../service/infrastructure/bufferService.js"; import StatusService from "../service/infrastructure/statusService.js"; import NotificationUtils from "../service/infrastructure/notificationUtils.js"; import NotificationService from "../service/infrastructure/notificationService.js"; +import { NotificationsService } from "@/service/index.js"; import ErrorService from "../service/infrastructure/errorService.js"; import SuperSimpleQueueHelper from "../service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js"; import SuperSimpleQueue from "../service/infrastructure/SuperSimpleQueue/SuperSimpleQueue.js"; @@ -206,9 +207,9 @@ export const initializeServices = async ({ checksRepository, }); - const bufferService = new BufferService({ db, logger, envSettings, incidentService, checkService }); + const bufferService = new BufferService({ logger, incidentService, checkService }); - const statusService = new StatusService({ db, logger, buffer: bufferService, incidentService, monitorsRepository }); + const statusService = new StatusService({ db, logger, buffer: bufferService, monitorsRepository }); const notificationUtils = new NotificationUtils({ stringService, @@ -225,12 +226,15 @@ export const initializeServices = async ({ notificationUtils, }); + const notificationsService = new NotificationsService(); + const superSimpleQueueHelper = new SuperSimpleQueueHelper({ db, logger, networkService, statusService, notificationService, + notificationsService, checkService, buffer: bufferService, }); @@ -317,6 +321,7 @@ export const initializeServices = async ({ invitesRepository, recoveryTokensRepository, settingsRepository, + notificationsService, }; Object.values(services).forEach((service) => { diff --git a/server/src/service/index.ts b/server/src/service/index.ts index 4dba8f6f4..08c02f397 100644 --- a/server/src/service/index.ts +++ b/server/src/service/index.ts @@ -2,3 +2,4 @@ export * from "@/service/business/monitorService.js"; export * from "@/service/infrastructure/networkService.js"; export * from "@/service/infrastructure/notificationProviders/webhook.js"; export * from "@/service/infrastructure/notificationProviders/INotificationProvider.js"; +export * from "@/service/infrastructure/notificationsService.js"; diff --git a/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts b/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts index 891635d56..c2a9e1267 100644 --- a/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts +++ b/server/src/service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.ts @@ -1,7 +1,7 @@ const SERVICE_NAME = "JobQueueHelper"; import type { Monitor } from "@/types/monitor.js"; import { AppError } from "@/utils/AppError.js"; -import { INetworkService } from "@/service/index.js"; +import { INetworkService, INotificationsService } from "@/service/index.js"; class SuperSimpleQueueHelper { static SERVICE_NAME = SERVICE_NAME; @@ -10,6 +10,7 @@ class SuperSimpleQueueHelper { private networkService: INetworkService; private statusService: any; private notificationService: any; + private notificationsService: INotificationsService; private checkService: any; private buffer: any; @@ -19,6 +20,7 @@ class SuperSimpleQueueHelper { networkService, statusService, notificationService, + notificationsService, checkService, buffer, }: { @@ -27,6 +29,7 @@ class SuperSimpleQueueHelper { networkService: INetworkService; statusService: any; notificationService: any; + notificationsService: INotificationsService; checkService: any; buffer: any; }) { @@ -37,6 +40,7 @@ class SuperSimpleQueueHelper { this.notificationService = notificationService; this.checkService = checkService; this.buffer = buffer; + this.notificationsService = notificationsService; } get serviceName() { @@ -78,22 +82,27 @@ class SuperSimpleQueueHelper { // Step 4. Update monitor status const statusChangeResult = await this.statusService.updateMonitorStatus(status, check); - this.notificationService - .handleNotifications({ - ...status, - monitor: statusChangeResult.monitor, - prevStatus: statusChangeResult.prevStatus, - statusChanged: statusChangeResult.statusChanged, - }) - .catch((error: any) => { - this.logger.error({ - message: error.message, - service: SERVICE_NAME, - method: "getMonitorJob", - details: `Error sending notifications for job ${monitor.id}: ${error.message}`, - stack: error.stack, - }); - }); + // Step 5 handle incidents and notifications + if (statusChangeResult.statusChanged === true) { + this.notificationsService.handleNotifications(monitor, status); + } + + // this.notificationService + // .handleNotifications({ + // ...status, + // monitor: statusChangeResult.monitor, + // prevStatus: statusChangeResult.prevStatus, + // statusChanged: statusChangeResult.statusChanged, + // }) + // .catch((error: any) => { + // this.logger.error({ + // message: error.message, + // service: SERVICE_NAME, + // method: "getMonitorJob", + // details: `Error sending notifications for job ${monitor.id}: ${error.message}`, + // stack: error.stack, + // }); + // }); } catch (error: any) { this.logger.warn({ message: error.message, diff --git a/server/src/service/infrastructure/bufferService.ts b/server/src/service/infrastructure/bufferService.ts index 5a809eea5..0363aa362 100755 --- a/server/src/service/infrastructure/bufferService.ts +++ b/server/src/service/infrastructure/bufferService.ts @@ -6,7 +6,6 @@ const SERVICE_NAME = "BufferService"; class BufferService { static SERVICE_NAME = SERVICE_NAME; private BUFFER_TIMEOUT: number; - private db: any; private logger: any; private incidentService: any; private SERVICE_NAME: string; @@ -15,21 +14,8 @@ class BufferService { private bufferTimer: NodeJS.Timeout | null = null; private checksService: any; - constructor({ - db, - logger, - envSettings, - incidentService, - checkService, - }: { - db: any; - logger: any; - envSettings: any; - incidentService: any; - checkService: any; - }) { + constructor({ logger, incidentService, checkService }: { logger: any; incidentService: any; checkService: any }) { this.BUFFER_TIMEOUT = config.NODE_ENV === "development" ? 10 : 1000 * 60 * 1; // 1 minute - this.db = db; this.logger = logger; this.incidentService = incidentService; this.checksService = checkService; diff --git a/server/src/service/infrastructure/notificationsService.ts b/server/src/service/infrastructure/notificationsService.ts index e9a6e8e2d..f9fdbfea8 100644 --- a/server/src/service/infrastructure/notificationsService.ts +++ b/server/src/service/infrastructure/notificationsService.ts @@ -1,4 +1,11 @@ -import type { Monitor } from "@/types/index.js"; -interface INotificationSerivce { - handleNotifications: (moniotr: Monitor) => Promise; +import type { Monitor, MonitorStatusResponse } from "@/types/index.js"; +export interface INotificationsService { + handleNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => Promise; +} + +export class NotificationsService implements INotificationsService { + handleNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => { + console.log(JSON.stringify(monitor, null, 2)); + console.log(JSON.stringify(monitorStatusResponse, null, 2)); + }; } diff --git a/server/src/service/infrastructure/statusService.ts b/server/src/service/infrastructure/statusService.ts index 7d34017a5..c9aab706b 100755 --- a/server/src/service/infrastructure/statusService.ts +++ b/server/src/service/infrastructure/statusService.ts @@ -17,26 +17,12 @@ class StatusService { private db: any; private logger: any; private buffer: any; - private incidentService: any; private monitorsRepository: IMonitorsRepository; - constructor({ - db, - logger, - buffer, - incidentService, - monitorsRepository, - }: { - db: any; - logger: any; - buffer: any; - incidentService: any; - monitorsRepository: IMonitorsRepository; - }) { + constructor({ db, logger, buffer, monitorsRepository }: { db: any; logger: any; buffer: any; monitorsRepository: IMonitorsRepository }) { this.db = db; this.logger = logger; this.buffer = buffer; - this.incidentService = incidentService; this.monitorsRepository = monitorsRepository; }