public SendGridMessage PrepareDiaconateReminderEmail(DeaconMeeting deacon) { BuildEmailFrom(deacon.FromEmail, deacon.FromName); BuildEmailTo(deacon.Email, deacon.Name); BuildEmailCopy(deacon.Copy); SetUpDeaconReminderTemplate(deacon); return(_message); }
public static async Task RunAsync([TimerTrigger("0 0 15 * * THU")] TimerInfo myTimer, [SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] IAsyncCollector <SendGridMessage> messageCollector, ILogger log) { log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}"); var connection = Environment.GetEnvironmentVariable("CosmosDBConnection"); try { var cosmosClient = new CosmosClient(connection); const string databaseId = "ministries"; var database = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId); var deacon = new DeaconDuty { Month = DateTime.Now.AddMonths(1).ToString("MMMM") }; var meeting = new DeaconMeeting(); await GetCurrentDeacon(database.Database, deacon, meeting); if (string.IsNullOrEmpty(deacon.AttendeeId)) { log.LogInformation("Deacon not found"); } await GetDeaconInformation(database.Database, deacon); if (string.IsNullOrEmpty(deacon.AttendeeId)) { log.LogInformation("Attendee not found"); } var message = new SendGridMessage(); var worker = new MessageWorker(message); deacon.FromEmail = Environment.GetEnvironmentVariable("DeaconDutyFromEmail"); deacon.FromName = Environment.GetEnvironmentVariable("DeaconDutyFromName"); deacon.Copy = Environment.GetEnvironmentVariable("DeaconDutyCopy"); await messageCollector.AddAsync(worker.PrepareDiaconateEmail(deacon)); if (!string.IsNullOrEmpty(meeting.DeaconDate)) { meeting.Email = Environment.GetEnvironmentVariable("DeaconMeetingEmail"); meeting.Name = Environment.GetEnvironmentVariable("DeaconMeetingName"); meeting.FromEmail = Environment.GetEnvironmentVariable("DeaconMeetingFromEmail"); meeting.FromName = Environment.GetEnvironmentVariable("DeaconMeetingFromName"); meeting.Copy = Environment.GetEnvironmentVariable("DeaconMeetingCopy"); await messageCollector.AddAsync(worker.PrepareDiaconateReminderEmail(meeting)); } } catch (Exception e) { log.LogInformation(e.ToString()); } }
private void SetUpDeaconMeetingTemplate(DeaconMeeting deacon) { if (deacon.DeaconDate == null) { _message.SetTemplateId(DeaconMeetingCanceledTemplate); } else { _message.SetTemplateId(deacon.OldMeetingDate == null ? DeaconMeetingScheduleTemplate : DeaconMeetingChangeTemplate); } _message.SetTemplateData(deacon); }
private void SetUpDeaconReminderTemplate(DeaconMeeting deacon) { _message.SetTemplateId(DeaconMeetingReminderTemplate); _message.SetTemplateData(deacon); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, [SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] IAsyncCollector <SendGridMessage> messageCollector, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); var connection = Environment.GetEnvironmentVariable("CosmosDBConnection"); try { var cosmosClient = new CosmosClient(connection); const string databaseId = "ministries"; var database = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId); var deacon = new DeaconDuty { Month = DateTime.Now.AddMonths(1).ToString("MMMM") }; var meeting = new DeaconMeeting(); await GetCurrentDeacon(database.Database, deacon, meeting); if (string.IsNullOrEmpty(deacon.AttendeeId)) { log.LogInformation("Deacon not found"); return(new BadRequestResult()); } await GetDeaconInformation(database.Database, deacon); if (string.IsNullOrEmpty(deacon.AttendeeId)) { log.LogInformation("Deacon not found"); return(new BadRequestResult()); } var message = new SendGridMessage(); var worker = new MessageWorker(message); deacon.FromEmail = Environment.GetEnvironmentVariable("DeaconDutyFromEmail"); deacon.FromName = Environment.GetEnvironmentVariable("DeaconDutyFromName"); deacon.Copy = Environment.GetEnvironmentVariable("DeaconDutyCopy"); await messageCollector.AddAsync(worker.PrepareDiaconateEmail(deacon)); if (!string.IsNullOrEmpty(meeting.DeaconDate)) { meeting.Email = Environment.GetEnvironmentVariable("DeaconMeetingEmail"); meeting.Name = Environment.GetEnvironmentVariable("DeaconMeetingName"); meeting.FromEmail = Environment.GetEnvironmentVariable("DeaconMeetingFromEmail"); meeting.FromName = Environment.GetEnvironmentVariable("DeaconMeetingFromName"); meeting.Copy = Environment.GetEnvironmentVariable("DeaconMeetingCopy"); await messageCollector.AddAsync(worker.PrepareDiaconateReminderEmail(meeting)); } } catch (Exception e) { log.LogInformation(e.ToString()); return(new BadRequestResult()); } return(new OkResult()); }
private static async Task GetCurrentDeacon(Database database, DeaconDuty deacon, DeaconMeeting meeting) { const string deaconContainerId = "diaconate"; var deaconContainer = await database.CreateContainerIfNotExistsAsync(deaconContainerId, "/year"); var deaconQuery = "SELECT * FROM c WHERE ( c.month = " + DateTime.Now.Month + " AND c.year = " + DateTime.Now.Year + " ) OR (c.month = " + DateTime.Now.AddMonths(1).Month + " AND c.year = " + DateTime.Now.AddMonths(1).Year + " )"; var deaconQueryDefinition = new QueryDefinition(deaconQuery); var deaconIterator = deaconContainer.Container.GetItemQueryIterator <DiaconateDB>(deaconQueryDefinition); var deacons = new List <DiaconateDB>(); while (deaconIterator.HasMoreResults) { var deaconResults = await deaconIterator.ReadNextAsync(); deacons.AddRange(deaconResults); } foreach (var diaconate in deacons) { if (diaconate.month == DateTime.Now.Month) { if (!diaconate.meetingDate.HasValue || !(diaconate.meetingDate > DateTime.Now)) { continue; } var meetingTime = diaconate.meetingDate.Value.AddHours(-5); meeting.ZoomLink = diaconate.meetingUrl; meeting.DiaconateId = diaconate.id; meeting.Year = diaconate.year; if (DateTimeFormatInfo.CurrentInfo != null) { meeting.Month = DateTimeFormatInfo.CurrentInfo.GetMonthName(diaconate.month); } meeting.DeaconDate = meetingTime.ToLongDateString() + " " + meetingTime.ToShortTimeString(); } else { deacon.Name = diaconate.name; deacon.AttendeeId = diaconate.attendeeId; } } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, [CosmosDB( databaseName: "ministries", collectionName: "diaconate", ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector <DiaconateDB> diaconateDocuments, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, }; var requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var diaconate = JsonSerializer.Deserialize <DiaconateDB>(requestBody, options); try { if (diaconate.id == null) { diaconate.id = Guid.NewGuid().ToString(); } else { if (diaconate.meetingDate != diaconate.newMeetingDate) { var meetingUrl = System.Environment.GetEnvironmentVariable("MeetingUrl"); DeaconMeeting meeting = new DeaconMeeting(); meeting.ZoomLink = diaconate.meetingUrl; meeting.DiaconateId = diaconate.id; meeting.Year = diaconate.year; if (DateTimeFormatInfo.CurrentInfo != null) { meeting.Month = DateTimeFormatInfo.CurrentInfo.GetMonthName(diaconate.month); } if (diaconate.newMeetingDate.HasValue) { var meetingTime = diaconate.newMeetingDate.Value.AddHours(-5); meeting.DeaconDate = meetingTime.ToLongDateString() + " " + meetingTime.ToShortTimeString(); } if (diaconate.meetingDate.HasValue) { meeting.OldMeetingDate = diaconate.meetingDate.Value.AddHours(-5).ToLongDateString(); } var registrantDb = JsonSerializer.Serialize <DeaconMeeting>(meeting); var client = new HttpClient(); _ = client.PostAsync(meetingUrl, new StringContent(registrantDb, Encoding.UTF8, "application/json")); diaconate.meetingDate = diaconate.newMeetingDate; } } diaconate.startDate = new DateTime(diaconate.year, diaconate.month, 1); diaconate.newMeetingDate = null; await diaconateDocuments.AddAsync(diaconate); } catch (Exception e) { log.LogInformation(e.ToString()); } return(new OkObjectResult(diaconate)); }