From da3b5078af4425d2096007900a704876bff7715e Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 3 Jul 2025 19:43:23 +0200 Subject: [PATCH] SendNotificationsWorker.cs --- API/Schema/NotificationsContext/Notification.cs | 6 +++++- API/Workers/SendNotificationsWorker.cs | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/API/Schema/NotificationsContext/Notification.cs b/API/Schema/NotificationsContext/Notification.cs index 3253fda..b719c43 100644 --- a/API/Schema/NotificationsContext/Notification.cs +++ b/API/Schema/NotificationsContext/Notification.cs @@ -19,6 +19,8 @@ public class Notification : Identifiable [Required] public DateTime Date { get; init; } + + public bool IsSent { get; internal set; } public Notification(string title, string message = "", NotificationUrgency urgency = NotificationUrgency.Normal, DateTime? date = null) : base(TokenGen.CreateToken("Notification")) @@ -27,18 +29,20 @@ public class Notification : Identifiable this.Message = message; this.Urgency = urgency; this.Date = date ?? DateTime.UtcNow; + this.IsSent = false; } /// /// EF ONLY!!! /// - public Notification(string key, string title, string message, NotificationUrgency urgency, DateTime date) + public Notification(string key, string title, string message, NotificationUrgency urgency, DateTime date, bool isSent) : base(key) { this.Title = title; this.Message = message; this.Urgency = urgency; this.Date = date; + this.IsSent = isSent; } public override string ToString() => $"{base.ToString()} {Urgency} {Title}"; diff --git a/API/Workers/SendNotificationsWorker.cs b/API/Workers/SendNotificationsWorker.cs index 8c12643..21e48df 100644 --- a/API/Workers/SendNotificationsWorker.cs +++ b/API/Workers/SendNotificationsWorker.cs @@ -1,4 +1,5 @@ using API.Schema.NotificationsContext; +using API.Schema.NotificationsContext.NotificationConnectors; namespace API.Workers; @@ -9,7 +10,20 @@ public class SendNotificationsWorker(IEnumerable? dependsOn = null) public TimeSpan Interval { get; set; } = TimeSpan.FromMinutes(1); protected override BaseWorker[] DoWorkInternal() { - throw new NotImplementedException(); + NotificationConnector[] connectors = DbContext.NotificationConnectors.ToArray(); + Notification[] notifications = DbContext.Notifications.Where(n => n.IsSent == false).ToArray(); + + foreach (Notification notification in notifications) + { + foreach (NotificationConnector connector in connectors) + { + connector.SendNotification(notification.Title, notification.Message); + notification.IsSent = true; + } + } + + DbContext.Sync(); + return []; } } \ No newline at end of file