diff --git a/app/apps/onebox/src/modules/monitor/monitor.service.ts b/app/apps/onebox/src/modules/monitor/monitor.service.ts index f215910..c41c4b1 100644 --- a/app/apps/onebox/src/modules/monitor/monitor.service.ts +++ b/app/apps/onebox/src/modules/monitor/monitor.service.ts @@ -100,6 +100,17 @@ export class MonitorService { ]); }); + if (monitor.notification.method === MonitorNotificationMethod.Webhook) { + const method = monitor.notification as WebhookNotification; + await this.webhookService.updateWebhook(monitor.webhookId, { + name: monitor.monitorId, + webhookUrl: method.url, + secret_token: method.secret_token, + authorization: method.authorization, + active: false, + }); + } + return Builder().success(true).build(); } @@ -147,8 +158,26 @@ export class MonitorService { if (request.disabled != undefined) { updateMonitor['disabled'] = request.disabled; } - return this.monitorRepository + + return await this.monitorRepository .updateMonitor(monitor.monitorId, updateMonitor) - .then((monitor) => MonitorResponseDto.from(monitor)); + .then(async (monitor) => { + // @todo handle error on update webhook service + if (monitor.notification) { + if ( + request.notification.method === MonitorNotificationMethod.Webhook + ) { + const method = monitor.notification as WebhookNotification; + await this.webhookService.updateWebhook(monitor.webhookId, { + name: monitor.monitorId, + webhookUrl: method.url, + secret_token: method.secret_token, + authorization: method.authorization, + active: true, + }); + } + } + return MonitorResponseDto.from(monitor); + }); } } diff --git a/app/libs/shared_modules/src/webhook/webhook.service.ts b/app/libs/shared_modules/src/webhook/webhook.service.ts index 391ce44..a4abec0 100644 --- a/app/libs/shared_modules/src/webhook/webhook.service.ts +++ b/app/libs/shared_modules/src/webhook/webhook.service.ts @@ -119,17 +119,21 @@ export class WebhookService { async updateWebhook( webhookId: string, - webhookUrl?: string, - authorization?: string, - secret_token?: string, - active?: boolean, + options: { + name: string; + webhookUrl: string; + authorization: string; + secret_token: string; + active: boolean; + }, ) { try { const request = this.buildUpdateWebhookRequest( - webhookUrl, - authorization, - secret_token, - active, + options.name, + options.webhookUrl, + options.authorization, + options.secret_token, + options.active, ); const response = await sendPut( `${this.webhookUrl}/v1/webhooks/${webhookId}`, @@ -271,31 +275,27 @@ export class WebhookService { } private buildUpdateWebhookRequest( - webhookUrl?: string, - authorization?: string, - secret_token?: string, - active?: boolean, + name: string, + webhookUrl: string, + authorization: string, + secret_token: string, + active: boolean, ): UpdateWebhookRequestDto { - const options: UpdateWebhookRequestDto = {}; - - // Check each input and add it to the options object only if it's defined - if (webhookUrl !== undefined) { - options.url = webhookUrl; - } - - if (authorization !== undefined) { - options.authorization_token = authorization; - } - - if (secret_token !== undefined) { - options.secret_token = secret_token; - } - - if (active !== undefined) { - options.active = active; - } + const updateWebhookDto = { + name: name, + url: webhookUrl, + content_type: 'application/json', + valid_status_codes: [200, 201], + secret_token: secret_token, + authorization_token: authorization, + active: active, + max_delivery_attempts: 5, + delivery_attempt_timeout: 1, + retry_min_backoff: 10, + retry_max_backoff: 60, + }; - return options; + return updateWebhookDto; } }