Skip to content

Commit 01dd4f2

Browse files
committed
Added controller for batched events.
1 parent 52ce4b8 commit 01dd4f2

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/SocketLabs.EventWebhooks.Extensions/Controllers/WebhookEventsController.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
using Microsoft.Extensions.Options;
44
using SocketLabs.EventWebhooks.Extensions.Configuration;
55
using SocketLabs.EventWebhooks.Extensions.Models.Events;
6+
using SocketLabs.EventWebhooks.Extensions.Models.Inbound;
67

78
namespace SocketLabs.EventWebhooks.Extensions.Controllers
89
{
9-
[Route("api/v1/[controller]")]
10+
[Route("api/v1/[controller]/{id}")]
1011
[ApiController]
1112
public class WebhookEventsController : ControllerBase
1213
{
@@ -26,7 +27,6 @@ IOptionsMonitor<WebhookOptions> options
2627
}
2728

2829
[HttpPost]
29-
[Route("{id}")]
3030
public async Task<IActionResult> Post(WebhookEventBase webhookEvent, string id)
3131
{
3232
if (!_options.TryGetWebhook(id, out var endpoint) || endpoint?.SecretKey != webhookEvent.SecretKey)
@@ -61,6 +61,44 @@ public async Task<IActionResult> Post(WebhookEventBase webhookEvent, string id)
6161

6262
return Ok();
6363
}
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+
}
64102

65103
private async Task ProcessEvent(ComplaintEvent webhookEvent)
66104
{

0 commit comments

Comments
 (0)