Skip to content

Commit

Permalink
disable webhook on delete monitor
Browse files Browse the repository at this point in the history
update monitor method also update webhook
  • Loading branch information
cauta committed Apr 22, 2024
1 parent cbc8639 commit accc52a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
33 changes: 31 additions & 2 deletions app/apps/onebox/src/modules/monitor/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeleteMonitorResponseDto>().success(true).build();
}

Expand Down Expand Up @@ -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);
});
}
}
62 changes: 31 additions & 31 deletions app/libs/shared_modules/src/webhook/webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit accc52a

Please sign in to comment.