Browse Source

prs: Update made to event system; also databaseupdatescript for 8.34

Kenric Nugteren 4 months ago
parent
commit
9a0e9ed9a7

+ 5 - 1
prs.classes/Entities/Events/Event.cs

@@ -39,11 +39,15 @@ namespace Comal.Classes
         [ExpressionEditor(null)]
         public string NotificationExpression { get; set; } = "";
 
+        [Comment("Sets whether notifications are automatically sent when the event triggers. If false, the notifications must be manually sent by the event actions.")]
         [EditorSequence(5)]
+        public bool NotificationsEnabled { get; set; } = true;
+
+        [EditorSequence(6)]
         public bool Enabled { get; set; } = true;
 
         [Comment("Marks whether non-managers (users without the CanManageEvents security token) can view this event.")]
-        [EditorSequence(6)]
+        [EditorSequence(7)]
         public bool Visible { get; set; } = true;
 
         static Event()

+ 1 - 0
prs.shared/Database Update Scripts/DatabaseUpdateScripts.cs

@@ -59,5 +59,6 @@ public static class DatabaseUpdateScripts
         DataUpdater.RegisterUpdateScript<Update_8_25>();
         DataUpdater.RegisterUpdateScript<Update_8_32>();
         DataUpdater.RegisterUpdateScript<Update_8_33>();
+        DataUpdater.RegisterUpdateScript<Update_8_34>();
     }
 }

+ 39 - 0
prs.shared/Database Update Scripts/Update_8_34.cs

@@ -0,0 +1,39 @@
+using Comal.Classes;
+using InABox.Core;
+using InABox.Database;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PRS.Shared.Database_Update_Scripts;
+
+internal class Update_8_34 : DatabaseUpdateScript
+{
+    public override VersionNumber Version => new(8, 34);
+
+    private static void DoEventNotificationsEnabled()
+    {
+        var provider = DbFactory.NewProvider(Logger.Main);
+
+        Logger.Send(LogType.Information, "", $"Initialising Event.NotificationsEnabled to true...");
+        var events = provider.Query(
+            null,
+            Columns.None<Event>().Add(x => x.ID).Add(x => x.NotificationsEnabled))
+            .ToArray<Event>();
+        foreach(var ev in events)
+        {
+            ev.NotificationsEnabled = true;
+        }
+        provider.Save(events);
+        Logger.Send(LogType.Information, "", $"Initialised Event.NotificationsEnabled to true");
+    }
+
+    public override bool Update()
+    {
+        DoEventNotificationsEnabled();
+
+        return true;
+    }
+}

+ 37 - 27
prs.stores/Events/Event.cs

@@ -178,10 +178,41 @@ public static class EventUtils
         NotifySubscribers(store, ev, evData, dataModel);
     }
 
+    public static void SendNotifications(IStore store, IEnumerable<(EventSubscriberType type, Notification notification)> notifications)
+    {
+        var toSave = new List<Notification>();
+        foreach(var (type, notification) in notifications)
+        {
+            if(type == EventSubscriberType.Notification)
+            {
+                toSave.Add(notification);
+            }
+            else if(type == EventSubscriberType.Email)
+            {
+                var mailer = DbFactory.Mailer;
+                if(mailer is not null)
+                {
+                    if (mailer.Connect())
+                    {
+                        var msg = mailer.CreateMessage();
+                        // msg.From = DbFactory.EmailAddress;
+                        msg.To = [notification.Employee.Email];
+                        msg.Subject = notification.Title;
+                        msg.Body = notification.Description;
+                        var bOK = mailer.SendMessage(msg);
+                    }
+                }
+            }
+        }
+        store.Provider.Save(toSave);
+    }
+
     private static void NotifySubscribers<T, TDataModel>(IStore store, Event ev, EventData<T, TDataModel> evData, TDataModel dataModel)
         where T : IEvent<TDataModel>
         where TDataModel : IEventDataModel
     {
+        if (!ev.NotificationsEnabled) return;
+
         string? description;
         if (ev.NotificationExpression.IsNullOrWhiteSpace())
         {
@@ -202,37 +233,16 @@ public static class EventUtils
             Columns.None<EventSubscriber>().Add(x => x.Employee.ID).Add(x => x.Employee.Email).Add(x => x.SubscriberType))
             .ToArray<EventSubscriber>();
 
-        var notifications = new List<Notification>();
-        foreach(var subscriber in subscribers)
+        SendNotifications(store, subscribers.Select(x =>
         {
             var notification = evData.GenerateNotification(dataModel);
-            notification.Employee.CopyFrom(subscriber.Employee);
-            if(description is not null)
+            notification.Employee.CopyFrom(x.Employee);
+            if (description is not null)
             {
                 notification.Description = description;
             }
-            if(subscriber.SubscriberType == EventSubscriberType.Notification)
-            {
-                notifications.Add(notification);
-            }
-            else
-            {
-                var mailer = DbFactory.Mailer;
-                if(mailer is not null)
-                {
-                    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);
+            return (x.SubscriberType, notification);
+        }));
     }
 
     #endregion
@@ -268,7 +278,7 @@ public static class EventUtils
         _genericEventMap.Clear();
         var events = provider.Query(
             new Filter<Event>(x => x.Enabled).IsEqualTo(true),
-            Columns.None<Event>().Add(x => x.ID).Add(x => x.Code).Add(x => x.EventType).Add(x => x.Data).Add(x => x.NotificationExpression))
+            Columns.None<Event>().Add(x => x.ID).Add(x => x.Code).Add(x => x.EventType).Add(x => x.Data).Add(x => x.NotificationExpression).Add(x => x.NotificationsEnabled))
             .ToObjects<Event>();
         foreach(var ev in events)
         {