Преглед изворни кода

Using server email for event notifications

Kenric Nugteren пре 7 месеци
родитељ
комит
9e25859dbc

+ 2 - 0
prs.classes/Entities/Employee/EmployeeLink.cs

@@ -54,5 +54,7 @@ namespace Comal.Classes
         
         [NullEditor]
         public DateTime RosterStart { get; set; }
+
+        public string Email { get; set; }
     }
 }

+ 10 - 0
prs.classes/Entities/Events/EventSubscriber.cs

@@ -5,6 +5,12 @@ using System.Text;
 
 namespace Comal.Classes
 {
+    public enum EventSubscriberType
+    {
+        Notification,
+        Email
+    }
+
     public class EventSubscriber : Entity, IRemotable, IPersistent, IManyToMany<Event, Employee>, ILicense<CoreLicense>
     {
         public EventLink Event { get; set; }
@@ -12,11 +18,15 @@ namespace Comal.Classes
         [EditorSequence(1)]
         public EmployeeLink Employee { get; set; }
 
+        [EditorSequence(2)]
+        public EventSubscriberType SubscriberType { get; set; } = EventSubscriberType.Notification;
+
         static EventSubscriber()
         {
             DefaultColumns.Add<EventSubscriber>(x => x.Event.Code);
             DefaultColumns.Add<EventSubscriber>(x => x.Employee.Code);
             DefaultColumns.Add<EventSubscriber>(x => x.Employee.Name);
+            DefaultColumns.Add<EventSubscriber>(x => x.SubscriberType);
         }
     }
 }

+ 16 - 4
prs.classes/Server/Properties/ServerEmailProperties.cs

@@ -20,11 +20,17 @@ namespace PRSServer
         public int Port { get; set; }
 
         [EditorSequence(3)]
-        public string EmailAddress { get; set; } = "";
+        public string Domain { get; set; } = "";
 
-        [PasswordEditor]
         [EditorSequence(4)]
+        public string UserName { get; set; } = "";
+
+        [PasswordEditor]
+        [EditorSequence(5)]
         public string Password { get; set; } = "";
+
+        [EditorSequence(6)]
+        public string EmailAddress { get; set; } = "";
     }
 
     public class ServerEmailIMAPProperties : BaseObject
@@ -36,10 +42,16 @@ namespace PRSServer
         public int Port { get; set; }
 
         [EditorSequence(3)]
-        public string EmailAddress { get; set; } = "";
+        public string Domain { get; set; } = "";
 
-        [PasswordEditor]
         [EditorSequence(4)]
+        public string UserName { get; set; } = "";
+
+        [PasswordEditor]
+        [EditorSequence(5)]
         public string Password { get; set; } = "";
+
+        [EditorSequence(6)]
+        public string EmailAddress { get; set; } = "";
     }
 }

+ 33 - 0
prs.server/Engines/Database/DatabaseEngine.cs

@@ -12,6 +12,7 @@ using InABox.Core;
 using InABox.Database;
 using InABox.Database.SQLite;
 using InABox.IPC;
+using InABox.Mail;
 using InABox.Rpc;
 using InABox.Server;
 using InABox.Wpf.Reports;
@@ -139,6 +140,38 @@ public class DatabaseEngine : Engine<DatabaseServerProperties>
                 x => x.Closed
             )).Rows.Select(x => x.ToObject<Notification>());
     }
+
+    private void ConfigureMailer()
+    {
+        if (!Properties.EmailProperties.IsNullOrWhiteSpace())
+        {
+            switch (Properties.GetEmailProperties())
+            {
+                case ServerEmailIMAPProperties imap:
+                    DbFactory.Mailer = new IMAPMailer
+                    {
+                        SMTPHost = imap.Host,
+                        SMTPDomain = imap.Domain,
+                        SMTPUserName = imap.UserName,
+                        SMTPPassword = imap.Password,
+                        SMTPPort = imap.Port
+                    };
+                    DbFactory.EmailAddress = imap.EmailAddress;
+                    break;
+                case ServerEmailExchangeProperties exchange:
+                    DbFactory.Mailer = new ExchangeMailer
+                    {
+                        MailboxHost = exchange.Host,
+                        MailboxDomain = exchange.Domain,
+                        MailboxUserName = exchange.UserName,
+                        MailboxPassword = exchange.Password,
+                        MailboxPort = exchange.Port
+                    };
+                    DbFactory.EmailAddress = exchange.EmailAddress;
+                    break;
+            }
+        }
+    }
     
     #region Run/Stop Functionality
 

+ 21 - 2
prs.stores/Events/Event.cs

@@ -220,7 +220,7 @@ public static class EventUtils
 
         var subscribers = store.Provider.Query(
             new Filter<EventSubscriber>(x => x.Event.ID).IsEqualTo(ev.ID),
-            Columns.None<EventSubscriber>().Add(x => x.Employee.ID))
+            Columns.None<EventSubscriber>().Add(x => x.Employee.ID).Add(x => x.Employee.Email).Add(x => x.SubscriberType))
             .ToArray<EventSubscriber>();
 
         var notifications = new List<Notification>();
@@ -232,7 +232,26 @@ public static class EventUtils
             {
                 notification.Description = description;
             }
-            notifications.Add(notification);
+            if(subscriber.SubscriberType == EventSubscriberType.Notification)
+            {
+                notifications.Add(notification);
+            }
+            else
+            {
+                var mailer = DbFactory.Mailer;
+                if(mailer is not null && !DbFactory.EmailAddress.IsNullOrWhiteSpace())
+                {
+                    if (mailer.Connect())
+                    {
+                        var msg = mailer.CreateMessage();
+                        msg.From = DbFactory.EmailAddress;
+                        msg.To = [subscriber.Employee.Email];
+                        msg.Subject = notification.Title;
+                        msg.Body = notification.Description;
+                        var bOK = mailer.SendMessage(msg);
+                    }
+                }
+            }
         }
         store.Provider.Save(notifications);
     }