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());
            }
        }
        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());
        }