-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomationApi.cs
More file actions
59 lines (47 loc) · 2.82 KB
/
AutomationApi.cs
File metadata and controls
59 lines (47 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace NewsletterBuilder;
public static class AutomationApi
{
private static string _automationApiKey;
public static void Configure(string automationApiKey)
{
_automationApiKey = automationApiKey;
}
public static void MapAutomationApiPaths(this WebApplication app)
{
var group = app.MapGroup("/api/automate");
group.MapPut("/{domain}/recipients", [AllowAnonymous] async (string domain, HttpContext context, [FromHeader(Name = "X-Api-Key")] string auth) =>
{
if (string.IsNullOrEmpty(_automationApiKey)) return Results.Conflict("An automation API key is not configured.");
if (auth != _automationApiKey) return Results.Unauthorized();
if (string.IsNullOrEmpty(domain)) return Results.BadRequest("Domain required.");
if (!Organisation.ByDomain.ContainsKey(domain)) return Results.NotFound("Domain not recognised.");
if (!context.Request.ContentType.StartsWith("text/plain", StringComparison.OrdinalIgnoreCase)) return Results.BadRequest("Content type must be text/plain.");
using var reader = new StreamReader(context.Request.Body);
var data = await reader.ReadToEndAsync();
if (string.IsNullOrWhiteSpace(data)) return Results.BadRequest("Data cannot be empty.");
var suppressed = await Mailer.GetSuppressedRecipientsAsync();
var recipients = data.Trim().Split('\n').Select(o => o.Trim().ToLowerInvariant()).Distinct()
.Where(o => o.Contains('@', StringComparison.OrdinalIgnoreCase) && !suppressed.Contains(o, StringComparer.OrdinalIgnoreCase)).ToList();
var service = new TableService(domain);
await service.ReplaceRecipientsAsync(recipients);
return Results.Ok();
});
group.MapPut("/{domain}/users", [AllowAnonymous] async (string domain, HttpContext context, [FromHeader(Name = "X-Api-Key")] string auth) =>
{
if (string.IsNullOrEmpty(_automationApiKey)) return Results.Conflict("An automation API key is not configured.");
if (auth != _automationApiKey) return Results.Unauthorized();
if (string.IsNullOrEmpty(domain)) return Results.BadRequest("Domain required.");
if (!Organisation.ByDomain.ContainsKey(domain)) return Results.NotFound("Domain not recognised.");
if (!context.Request.ContentType.StartsWith("text/csv", StringComparison.OrdinalIgnoreCase)) return Results.BadRequest("Content type must be text/csv.");
using var reader = new StreamReader(context.Request.Body);
var data = await reader.ReadToEndAsync();
if (string.IsNullOrWhiteSpace(data)) return Results.BadRequest("Data cannot be empty.");
var csvUsers = data.Trim().Split('\n').Select(o => o.Trim()).ToList();
var service = new TableService(domain);
await service.ReplaceUsersAsync(csvUsers);
return Results.Ok();
});
}
}