3
3
using Microsoft . Extensions . Options ;
4
4
using SocketLabs . EventWebhooks . Extensions . Configuration ;
5
5
using SocketLabs . EventWebhooks . Extensions . Models . Events ;
6
+ using SocketLabs . EventWebhooks . Extensions . Models . Inbound ;
6
7
7
8
namespace SocketLabs . EventWebhooks . Extensions . Controllers
8
9
{
9
- [ Route ( "api/v1/[controller]" ) ]
10
+ [ Route ( "api/v1/[controller]/{id} " ) ]
10
11
[ ApiController ]
11
12
public class WebhookEventsController : ControllerBase
12
13
{
@@ -26,7 +27,6 @@ IOptionsMonitor<WebhookOptions> options
26
27
}
27
28
28
29
[ HttpPost ]
29
- [ Route ( "{id}" ) ]
30
30
public async Task < IActionResult > Post ( WebhookEventBase webhookEvent , string id )
31
31
{
32
32
if ( ! _options . TryGetWebhook ( id , out var endpoint ) || endpoint ? . SecretKey != webhookEvent . SecretKey )
@@ -61,6 +61,44 @@ public async Task<IActionResult> Post(WebhookEventBase webhookEvent, string id)
61
61
62
62
return Ok ( ) ;
63
63
}
64
+ [ HttpPost ]
65
+ public async Task < IActionResult > Post ( WebhookEventBase [ ] webhookEvents , string id )
66
+ {
67
+ foreach ( var webhookEvent in webhookEvents )
68
+ {
69
+ if ( ! _options . TryGetWebhook ( id , out var endpoint ) || endpoint ? . SecretKey != webhookEvent . SecretKey )
70
+ {
71
+ return Unauthorized ( ) ;
72
+ }
73
+
74
+ try
75
+ {
76
+ webhookEvent . WebhookEndpointName = id ;
77
+
78
+ Task result = webhookEvent switch
79
+ {
80
+ ComplaintEvent eventItem => ProcessEvent ( eventItem ) ,
81
+ DeferredEvent eventItem => ProcessEvent ( eventItem ) ,
82
+ EngagementEvent eventItem => ProcessEvent ( eventItem ) ,
83
+ FailedEvent eventItem => ProcessEvent ( eventItem ) ,
84
+ QueuedEvent eventItem => ProcessEvent ( eventItem ) ,
85
+ SentEvent eventItem => ProcessEvent ( eventItem ) ,
86
+ ValidationEvent eventItem => ProcessEvent ( eventItem ) ,
87
+ _ => throw new InvalidOperationException ( "Unable to convert event type." )
88
+ } ;
89
+
90
+ await result ;
91
+ }
92
+ catch ( Exception ex )
93
+ {
94
+ _logger . LogError ( ex , "Unable to process webhook event." ) ;
95
+ return BadRequest ( ) ;
96
+ }
97
+
98
+ }
99
+
100
+ return Ok ( ) ;
101
+ }
64
102
65
103
private async Task ProcessEvent ( ComplaintEvent webhookEvent )
66
104
{
0 commit comments