Example #1
0
 public void NewMessage(string EmailAddressTo, CannedEmailMessage Message, Order Order)
 {
     this._mailQueue.Enqueue(new EmailNotification()
     {
         To      = EmailAddressTo,
         Order   = Order,
         Message = Message
     });
 }
        static void Main(string[] args)
        {
            IConfiguration configuration = new ConfigurationBuilder()
                                           .AddJsonFile($"appsettings.json", true, true)
                                           .AddEnvironmentVariables()
                                           .Build();

            string keyvault_endpoint = configuration["KEYVAULT_ENDPOINT"];

            if (!string.IsNullOrEmpty(keyvault_endpoint))
            {
                ConsoleWrite("Loading configuration from Azure Key Vault: " + keyvault_endpoint);
                var azureServiceTokenProvider = new AzureServiceTokenProvider();
                var keyVaultClient            = new KeyVaultClient(
                    new KeyVaultClient.AuthenticationCallback(
                        azureServiceTokenProvider.KeyVaultTokenCallback));

                configuration = new ConfigurationBuilder()
                                .AddConfiguration(configuration)
                                .AddAzureKeyVault(keyvault_endpoint, keyVaultClient, new DefaultKeyVaultSecretManager())
                                .Build();
            }

            IConfigurationSection smtpConfig = configuration.GetSection("SMTP");
            DatabaseContext       dbContext  = new DatabaseContext(configuration.GetConnectionString(RunnerSettings.ConnectionStringName));


            ConsoleWrite("EmailRunner starting main loop...");
            // Program loop start
            while (true)
            {
                OrderRepository orderRepo = new OrderRepository(dbContext);
                OrderNotificationLogRepository orderNotificationRepo = new OrderNotificationLogRepository(dbContext);

                ConsoleWrite(" Loading orders...");
                // Find order ids requiring customer emails
                List <string> OrdersNeedingCustomerNotifications = orderNotificationRepo.GetOrdersNeedingCustomerNotifications();

                // Find order ids requiring order desk emails
                List <string> OrdersNeedingOrderDeskNotifications = orderNotificationRepo.GetOrdersNeedingOrderDeskNotifications();

                // Load the order objects so we can use them later
                List <string> RequiredOrders = new List <string>();
                RequiredOrders.AddRangeUnique(OrdersNeedingCustomerNotifications);
                RequiredOrders.AddRangeUnique(OrdersNeedingOrderDeskNotifications);
                Dictionary <string, Order> _orderCache = orderRepo.Get(RequiredOrders).ToDictionary(x => x.OrderThumbprint);

                EmailHelper email = new EmailHelper(smtpConfig["hostname"], smtpConfig["port"].ToInt(), smtpConfig["username"], smtpConfig["password"], smtpConfig["fromaddress"], smtpConfig["replytoaddress"]);

                string helpDeskEmailAddress = smtpConfig["HelpDeskEmail"];

                ConsoleWrite("Found " + RequiredOrders.Count + " orders requiring email notifications");

                // Enqueue order desk emails
                foreach (string t in OrdersNeedingOrderDeskNotifications)
                {
                    if (_orderCache.ContainsKey(t))
                    {
                        Order thisOrder = _orderCache[t];
                        // Only send emails to orders with email addresses
                        if (helpDeskEmailAddress.Length > 0)
                        {
                            email.NewMessage(helpDeskEmailAddress, CannedEmailMessage.OrderDeskNotification(thisOrder), thisOrder);
                            ConsoleWrite("Enqueueing Order Desk email for order: " + thisOrder.OrderThumbprint);
                        }
                    }
                }

                // Enqueue customer emails
                foreach (string t in OrdersNeedingCustomerNotifications)
                {
                    if (_orderCache.ContainsKey(t))
                    {
                        Order thisOrder = _orderCache[t];
                        // Only send emails to orders with email addresses
                        if (thisOrder.CustomerEmailAddress.Length > 0)
                        {
                            email.NewMessage(thisOrder.CustomerEmailAddress, CannedEmailMessage.CustomerOrderThanks(thisOrder), thisOrder);
                            ConsoleWrite("Enqueueing Customer email for order: " + thisOrder.OrderThumbprint);
                        }
                    }
                }

                // Send all emails
                ConsoleWrite("Sending " + (OrdersNeedingCustomerNotifications.Count + OrdersNeedingOrderDeskNotifications.Count) + "  emails...");
                email.FlushQueue(dbContext);
                ConsoleWrite("Done!");

                ConsoleWrite("Sleeping for " + sleepTimeMinutes + " minutes...");

                // Sleep for 15 minutes
                Task.Delay(sleepTimeMinutes * 60 * 1000).Wait();
            }
        }