Kenric Nugteren il y a 6 mois
Parent
commit
801ab9dc23

+ 0 - 6
prs.classes/Server/Properties/ServerEmailProperties.cs

@@ -28,9 +28,6 @@ namespace PRSServer
         [PasswordEditor]
         [EditorSequence(5)]
         public string Password { get; set; } = "";
-
-        [EditorSequence(6)]
-        public string EmailAddress { get; set; } = "";
     }
 
     public class ServerEmailIMAPProperties : BaseObject
@@ -50,8 +47,5 @@ namespace PRSServer
         [PasswordEditor]
         [EditorSequence(5)]
         public string Password { get; set; } = "";
-
-        [EditorSequence(6)]
-        public string EmailAddress { get; set; } = "";
     }
 }

+ 2 - 4
prs.server/Engines/Database/DatabaseEngine.cs

@@ -156,7 +156,6 @@ public class DatabaseEngine : Engine<DatabaseServerProperties>
                         SMTPPassword = imap.Password,
                         SMTPPort = imap.Port
                     };
-                    DbFactory.EmailAddress = imap.EmailAddress;
                     break;
                 case ServerEmailExchangeProperties exchange:
                     DbFactory.Mailer = new ExchangeMailer
@@ -167,7 +166,6 @@ public class DatabaseEngine : Engine<DatabaseServerProperties>
                         MailboxPassword = exchange.Password,
                         MailboxPort = exchange.Port
                     };
-                    DbFactory.EmailAddress = exchange.EmailAddress;
                     break;
             }
         }
@@ -219,8 +217,6 @@ public class DatabaseEngine : Engine<DatabaseServerProperties>
         RestService.CheckPasswordExpiration = Properties.PasswordExpiryTime > 0;
 
         if (DbFactory.IsReadOnly)
-
-            
         {
             Logger.Send(LogType.Error,"","Unable to create ADMIN user at this time.");
         }
@@ -244,6 +240,8 @@ public class DatabaseEngine : Engine<DatabaseServerProperties>
         PurchaseOrder.PONumberPrefix = Properties.PurchaseOrderPrefix;
         Job.JobNumberPrefix = Properties.JobPrefix;
 
+        ConfigureMailer();
+
         ConfigureSmsProviders();
         CredentialsCache.SetCacheFile(Path.Combine(AppDataFolder, "session_cache.json"));
         CredentialsCache.LoadSessionCache();

+ 38 - 31
prs.shared/Grids/EventGrid.cs

@@ -19,12 +19,9 @@ namespace PRS.Shared;
 
 public class EventGrid : DynamicDataGrid<Event>
 {
-    private readonly BitmapImage _tick = InABox.Wpf.Resources.tick.AsBitmapImage();
-    private readonly BitmapImage _disabled = InABox.Wpf.Resources.disabled.AsBitmapImage();
-
     private Button? EnableButton = null;
 
-    private HashSet<Guid> _subscribedSet = new();
+    private Dictionary<Guid, EventSubscriberType> _subscribedSet = new();
 
     public Guid EmployeeID { get; set; }
 
@@ -36,7 +33,7 @@ public class EventGrid : DynamicDataGrid<Event>
         HiddenColumns.Add(x => x.Enabled);
         HiddenColumns.Add(x => x.Visible);
 
-        ActionColumns.Add(new DynamicImageColumn(Subscribed_Image, Subscribed_Click) { ToolTip = Subscribed_ToolTip });
+        ActionColumns.Add(new DynamicMenuColumn(BuildMenu) { ToolTip = Subscribed_ToolTip });
 
         if (Security.IsAllowed<CanManageEvents>())
         {
@@ -138,38 +135,54 @@ public class EventGrid : DynamicDataGrid<Event>
         }
         else
         {
-            return _subscribedSet.Contains(row.Get<Event, Guid>(x => x.ID))
+            return _subscribedSet.ContainsKey(row.Get<Event, Guid>(x => x.ID))
                 ? column.TextToolTip("You are subscribed to this event.")
                 : null;
         }
     }
 
-    private BitmapImage? Subscribed_Image(CoreRow? row)
+    private void BuildMenu(DynamicMenuColumn column, CoreRow? row)
     {
-        if(row is null)
+        if (row is null) return;
+
+        var menu = column.GetMenu();
+
+        var eventID = row.Get<Event, Guid>(x => x.ID);
+
+        if (_subscribedSet.TryGetValue(eventID, out var type))
         {
-            return _tick;
+            menu.AddItem("Unsubscribe", null, row, Unsubscribe);
+            if(type == EventSubscriberType.Notification)
+            {
+                menu.AddItem("Subscribe by Email", null, (row, EventSubscriberType.Email), ChangeSubscription);
+            }
+            else
+            {
+                menu.AddItem("Subscribe by Notification", null, (row, EventSubscriberType.Notification), ChangeSubscription);
+            }
         }
         else
         {
-            return _subscribedSet.Contains(row.Get<Event, Guid>(x => x.ID)) ? _tick : _disabled;
+            menu.AddItem("Subscribe by Notification", null, (row, EventSubscriberType.Notification), Subscribe);
+            menu.AddItem("Subscribe by Email", null, (row, EventSubscriberType.Email), Subscribe);
         }
     }
 
-    private bool Subscribed_Click(CoreRow? row)
+    private void ChangeSubscription((CoreRow row, EventSubscriberType Notification) tuple)
     {
-        if (row is null) return false;
+        Unsubscribe(tuple.row);
+        Subscribe(tuple);
+    }
 
-        var eventID = row.Get<Event, Guid>(x => x.ID);
-        if(_subscribedSet.Contains(eventID))
-        {
-            Unsubscribe(row);
-        }
-        else
-        {
-            Subscribe(row);
-        }
-        return true;
+    private void Subscribe((CoreRow row, EventSubscriberType type) tuple)
+    {
+        var ev = tuple.row.ToObject<Event>();
+        var subscriber = new EventSubscriber();
+        subscriber.Event.CopyFrom(ev);
+        subscriber.Employee.ID = EmployeeID;
+        subscriber.SubscriberType = tuple.type;
+        Client.Save(subscriber, "");
+        _subscribedSet.Add(ev.ID, subscriber.SubscriberType);
     }
 
     private void Unsubscribe(CoreRow row)
@@ -189,14 +202,8 @@ public class EventGrid : DynamicDataGrid<Event>
         _subscribedSet.Remove(eventID);
     }
 
-    private void Subscribe(CoreRow row)
+    private void Subscribe_(CoreRow row)
     {
-        var ev = row.ToObject<Event>();
-        var subscriber = new EventSubscriber();
-        subscriber.Event.CopyFrom(ev);
-        subscriber.Employee.ID = EmployeeID;
-        Client.Save(subscriber, "");
-        _subscribedSet.Add(ev.ID);
     }
 
     #endregion
@@ -289,8 +296,8 @@ public class EventGrid : DynamicDataGrid<Event>
                 _subscribedSet = Client.Query(
                     new Filter<EventSubscriber>(x => x.Employee.ID).IsEqualTo(EmployeeID)
                         .And(x => x.Event.ID).InList(ids),
-                    Columns.None<EventSubscriber>().Add(x => x.Event.ID))
-                    .ToObjects<EventSubscriber>().Select(x => x.Event.ID).ToHashSet();
+                    Columns.None<EventSubscriber>().Add(x => x.Event.ID).Add(x => x.SubscriberType))
+                    .ToObjects<EventSubscriber>().ToDictionary(x => x.Event.ID, x => x.SubscriberType);
             }
             action(data, error);
         });

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

@@ -218,12 +218,12 @@ public static class EventUtils
             else
             {
                 var mailer = DbFactory.Mailer;
-                if(mailer is not null && !DbFactory.EmailAddress.IsNullOrWhiteSpace())
+                if(mailer is not null)
                 {
                     if (mailer.Connect())
                     {
                         var msg = mailer.CreateMessage();
-                        msg.From = DbFactory.EmailAddress;
+                        // msg.From = DbFactory.EmailAddress;
                         msg.To = [subscriber.Employee.Email];
                         msg.Subject = notification.Title;
                         msg.Body = notification.Description;

+ 3 - 3
prs.stores/Events/StandardActions.cs

@@ -111,12 +111,12 @@ public partial class SendEmailEventAction<TEvent> : IStandardEventAction<TEvent>
     public object? Execute(IEventDataModel dataModel)
     {
         var mailer = DbFactory.Mailer;
-        if(mailer is not null && !DbFactory.EmailAddress.IsNullOrWhiteSpace())
+        if(mailer is not null)
         {
             if (mailer.Connect())
             {
                 var msg = mailer.CreateMessage();
-                msg.From = DbFactory.EmailAddress;
+                // msg.From = DbFactory.EmailAddress;
 
                 msg.To = ConvertAddressList(ToExpression.Evaluate(dataModel));
                 msg.CC = ConvertAddressList(CCExpression.Evaluate(dataModel));
@@ -136,7 +136,7 @@ public partial class SendEmailEventAction<TEvent> : IStandardEventAction<TEvent>
         {
             return list;
         }
-        else if(value is IEnumerable enumerable)
+        else if(value is IEnumerable enumerable && value is not string)
         {
             var lst = new List<string>();
             foreach(var obj in enumerable)