forked from orcasound/aifororcas-livesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SendModeratorEmail.cs
70 lines (64 loc) · 2.59 KB
/
SendModeratorEmail.cs
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
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using NotificationSystem.Template;
using NotificationSystem.Utilities;
using SendGrid.Helpers.Mail;
namespace NotificationSystem
{
[StorageAccount("OrcaNotificationStorageSetting")]
public static class SendModeratorEmail
{
[FunctionName("SendModeratorEmail")]
public static async Task Run(
[CosmosDBTrigger(
databaseName: "predictions",
collectionName: "metadata",
ConnectionStringSetting = "aifororcasmetadatastore_DOCUMENTDB",
LeaseCollectionName = "leases",
LeaseCollectionPrefix = "moderator",
CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> input,
[Table("EmailList")] CloudTable cloudTable,
[SendGrid(ApiKey = "SendGridKey")] IAsyncCollector<SendGridMessage> messageCollector,
ILogger log)
{
if (input == null || input.Count == 0)
{
log.LogInformation("No updated records");
return;
}
var newDocumentCreated = false;
DateTime? documentTimeStamp = null;
string location = null;
foreach (var document in input)
{
if (!document.GetPropertyValue<bool>("reviewed"))
{
newDocumentCreated = true;
documentTimeStamp = document.GetPropertyValue<DateTime>("timestamp");
location = document.GetPropertyValue<string>("location.name");
break;
}
}
if (!newDocumentCreated)
{
log.LogInformation("No unreviewed records");
return;
}
// TODO: make better email
string body = EmailTemplate.GetModeratorEmailBody(documentTimeStamp, location);
log.LogInformation("Retrieving email list and sending notifications");
foreach (var emailEntity in EmailHelpers.GetEmailEntities(cloudTable, "Moderator"))
{
string emailSubject = string.Format("OrcaHello Candidate at location {0}", location);
var email = EmailHelpers.CreateEmail(Environment.GetEnvironmentVariable("SenderEmail"),
emailEntity.Email, emailSubject, body);
await messageCollector.AddAsync(email);
}
}
}
}