<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Commencement.Core.Services;
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure;
using Dapper;

namespace Commencement.Jobs.NotificationsCommon
{
    public static class ProcessNotifications
    {
        static readonly string SparkPostApiKey = CloudConfigurationManager.GetSetting("SparkPostApiKey");

        public static void ProcessEmails(IDbService dbService, bool immediate)
        {
            var sendEmail = CloudConfigurationManager.GetSetting("send-email");
            var testEmail = CloudConfigurationManager.GetSetting("send-test-emails");

            //Don't execute unless email is turned on
            if (!string.Equals(sendEmail, "Yes", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.WriteLine("No emails sent because send-email is not set to 'Yes'");
                return;
            }

            using (var connection = dbService.GetConnection())
            {
                List<dynamic> pending = connection.Query(@"
                    select emailqueue.id, [subject], [body]
                        ,case 
				            when registrations.email is null then students.email
				            else students.email + ';' + registrations.email
				        end as emails
		            from emailqueue
			            inner join Students on students.Id = EmailQueue.Student_Id
			            LEFT OUTER JOIN Registrations on registrations.id = EmailQueue.RegistrationId
		            where Pending = 1 and [immediate] = @immediate").ToList();

                foreach (var email in pending)
                {
                    var subject = email.subject;

                }
            }
        }
    }
}<commit_msg>Notification Job. Still need to test<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Commencement.Core.Services;
using Microsoft.Azure.WebJobs;
using Microsoft.WindowsAzure;
using Dapper;
using SparkPost;

namespace Commencement.Jobs.NotificationsCommon
{
    public static class ProcessNotifications
    {
        static readonly string SparkPostApiKey = CloudConfigurationManager.GetSetting("SparkPostApiKey");

        public static void ProcessEmails(IDbService dbService, bool immediate)
        {
            var sendEmail = CloudConfigurationManager.GetSetting("send-email");
            var testEmail = CloudConfigurationManager.GetSetting("send-test-emails");
            var errorCount = 0;

            //Don't execute unless email is turned on
            if (!string.Equals(sendEmail, "Yes", StringComparison.InvariantCultureIgnoreCase))
            {
                Console.WriteLine("No emails sent because send-email is not set to 'Yes'");
                return;
            }

            using (var connection = dbService.GetConnection())
            {
                List<dynamic> pending = connection.Query(@"
                    select emailqueue.id as id, [subject], [body], students.email as sEmail, registrations.email as rEmail
		            from emailqueue
			            inner join Students on students.Id = EmailQueue.Student_Id
			            LEFT OUTER JOIN Registrations on registrations.id = EmailQueue.RegistrationId
		            where Pending = 1 and [immediate] = @immediate").ToList();


                foreach (var email in pending)
                {
                    var emailTransmission = new Transmission
                    {
                        Content = new Content
                        {
                            From =
                                new Address
                                {
                                    Email = "commencement-notify.ucdavis.edu",
                                    Name = "commencement-notify"
                                },
                            Subject = email.subject ,
                            Html = email.body
                        }
                    };

                    if (!string.IsNullOrWhiteSpace(testEmail))
                    {
                        emailTransmission.Recipients.Add(new Recipient { Address = new Address { Email = testEmail } });
                    }
                    else
                    {
                        emailTransmission.Recipients.Add(new Recipient { Address = new Address { Email = email.sEmail } });
                        if (!string.IsNullOrWhiteSpace(email.rEmail))
                        {
                            emailTransmission.Recipients.Add(new Recipient { Address = new Address { Email = email.rEmail } });
                        }
                    }
                    var client = new Client(SparkPostApiKey);
                    DateTime? sentDateTime = null;
                    try
                    {
                        client.Transmissions.Send(emailTransmission).Wait();
                        sentDateTime = DateTime.UtcNow; //TODO: Pacific time it?
                    }
                    catch (Exception ex)
                    {
                        //TODO: Logging.
                       // Log.Error(ex, "There was a problem emailing {email}", email.sEmail);
                       //I don't think we care if there are a few problems...
                        errorCount++;
                    }

                    if (string.IsNullOrWhiteSpace(testEmail))
                    {
                        //Update the db
                        connection.Execute(@"
                            UPDATE EmailQueue
                            SET 
                                [Pending] = 0
                                ,[SentDateTime] = @sentDateTime      
                            WHERE id = @email.id");
                    }



                }

                if (errorCount > 20)
                {
                    //Send us a notification?
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07362872673365178, 'fraction_numerical': 0.0009883050568275408, 'mean_word_length': 1.3219885277246655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2972542', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1293, 'n_words': 343}