1
+ import { prisma } from '../../../server/db' ;
1
2
import { logger } from '../../../../src/server/logger' ;
2
3
import { type NotificationParameters } from '../../../Interfaces/NotificationParameters' ;
3
4
import type Notifier from '../Notifier' ;
@@ -8,6 +9,31 @@ class WebhookNotifier implements Notifier {
8
9
return [ NOTIFICATION_METHOD . WEBHOOK ] ;
9
10
}
10
11
12
+ async deleteNotificationDisableAndUnverifyWebhook ( destination : string , notificationId : string ) : Promise < void > {
13
+ try {
14
+ // Delete the notification
15
+ await prisma . notification . delete ( {
16
+ where : {
17
+ id : notificationId ,
18
+ } ,
19
+ } ) ;
20
+ // Unverify and disable the alertMethod
21
+ await prisma . alertMethod . updateMany ( {
22
+ where : {
23
+ destination : destination ,
24
+ method : NOTIFICATION_METHOD . WEBHOOK ,
25
+ } ,
26
+ data : {
27
+ isVerified : false ,
28
+ isEnabled : false ,
29
+ } ,
30
+ } ) ;
31
+ logger ( `Notification with ID: ${ notificationId } deleted and alertMethod for destination: ${ destination } has been unverified and disabled.` , "info" ) ;
32
+ } catch ( error ) {
33
+ logger ( `Database Error: Couldn't modify the alertMethod or delete the notification: ${ error } ` , "error" ) ;
34
+ }
35
+ }
36
+
11
37
async notify (
12
38
destination : string ,
13
39
parameters : NotificationParameters ,
@@ -34,12 +60,27 @@ class WebhookNotifier implements Notifier {
34
60
35
61
if ( ! response . ok ) {
36
62
logger (
37
- `Failed to send webhook notification. Error: ${ response . statusText } for ${ parameters . id } ` ,
63
+ `Failed to send webhook notification. Error: ${ response . statusText } for ${ parameters . id } . ` ,
38
64
'error' ,
39
65
) ;
66
+ // Specific status code handling
67
+ if ( response . status === 404 ) {
68
+ // Webhook URL Not Found - Token not found
69
+ await this . deleteNotificationDisableAndUnverifyWebhook ( destination , parameters . id ) ;
70
+ } else if ( response . status === 401 ) {
71
+ // Unauthorized
72
+ await this . deleteNotificationDisableAndUnverifyWebhook ( destination , parameters . id ) ;
73
+ } else if ( response . status === 403 ) {
74
+ // Forbidden
75
+ await this . deleteNotificationDisableAndUnverifyWebhook ( destination , parameters . id ) ;
76
+ } else {
77
+ logger (
78
+ `Failed to send webhook notification. Something went wrong. Try again in next run.` ,
79
+ 'error' ,
80
+ ) ;
81
+ }
40
82
return false ;
41
83
}
42
-
43
84
return true ;
44
85
}
45
86
}
0 commit comments