From d9e17662103815094c4033675db098690e0238b1 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 9 Jun 2021 18:43:46 +0000 Subject: [PATCH] --- devdocs/specs/core-notification.txt | 2 +- server/AyaNova/biz/NotifyEventHelper.cs | 22 ++++--- server/AyaNova/biz/ReviewBiz.cs | 82 +++++++++++++------------ 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/devdocs/specs/core-notification.txt b/devdocs/specs/core-notification.txt index c5567948..c989922e 100644 --- a/devdocs/specs/core-notification.txt +++ b/devdocs/specs/core-notification.txt @@ -327,7 +327,7 @@ Internal workings: One enum list of NotificationType for any possible notification that can be processed Notify type is divided by actual type and then by actual delivery method No delivery schedule anymore, let user decide how to handle at their device end, not our concern, just deliver immediately always -Notify BEfore shoudl support multiple time frames, not just one +Notify BEfore should support multiple time frames, not just one Users not involved should not be notified, have choice on that` e.g. if a user is not booked on a workorder and is just a lowly tech they don't care about other workorder status changes diff --git a/server/AyaNova/biz/NotifyEventHelper.cs b/server/AyaNova/biz/NotifyEventHelper.cs index 86d5e95a..22996d8f 100644 --- a/server/AyaNova/biz/NotifyEventHelper.cs +++ b/server/AyaNova/biz/NotifyEventHelper.cs @@ -23,7 +23,7 @@ namespace AyaNova.Biz // ENSURE USER HAS IN APP NOTIFICATION // // - public static async Task EnsureDefaultInAppUserNotificationSubscriptionExists(long userId, AyContext ct) + public static async Task EnsureDefaultInAppUserNotificationSubscriptionExists(long userId, AyContext ct) { var defaultsub = await ct.NotifySubscription.FirstOrDefaultAsync(z => z.EventType == NotifyEventType.GeneralNotification && z.UserId == userId && z.DeliveryMethod == NotifyDeliveryMethod.App); if (defaultsub == null) @@ -42,7 +42,7 @@ namespace AyaNova.Biz await ct.NotifySubscription.AddAsync(defaultsub); await ct.SaveChangesAsync(); } - return defaultsub; + return; } @@ -325,8 +325,7 @@ namespace AyaNova.Biz using (AyContext ct = AyaNova.Util.ServiceProviderProvider.DBContext) { - //General notification goes to a specific user only - //no need to consult subscriptions + //General notification goes to one specific user only if (eventType == NotifyEventType.GeneralNotification) { if (userId == 0) @@ -340,16 +339,19 @@ namespace AyaNova.Biz var UserName = await ct.User.AsNoTracking().Where(z => z.Id == userId).Select(z => z.Name).FirstOrDefaultAsync(); //if they don't have a regular inapp subscription create one now - NotifySubscription defaultsub = await EnsureDefaultInAppUserNotificationSubscriptionExists(userId, ct); + await EnsureDefaultInAppUserNotificationSubscriptionExists(userId, ct); if (string.IsNullOrWhiteSpace(name)) - { name = UserName; - } - NotifyEvent n = new NotifyEvent() { EventType = eventType, UserId = userId, Message = message, NotifySubscriptionId = defaultsub.Id, Name = name }; - await ct.NotifyEvent.AddAsync(n); - await ct.SaveChangesAsync(); + var gensubs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.GeneralNotification && z.UserId == userId).ToListAsync(); + foreach (var sub in gensubs) + { + NotifyEvent n = new NotifyEvent() { EventType = eventType, UserId = userId, Message = message, NotifySubscriptionId = sub.Id, Name = name }; + await ct.NotifyEvent.AddAsync(n); + } + if (gensubs.Count > 0) + await ct.SaveChangesAsync(); return; } diff --git a/server/AyaNova/biz/ReviewBiz.cs b/server/AyaNova/biz/ReviewBiz.cs index 7bd5cac3..cc866a8e 100644 --- a/server/AyaNova/biz/ReviewBiz.cs +++ b/server/AyaNova/biz/ReviewBiz.cs @@ -493,7 +493,6 @@ namespace AyaNova.Biz bool isNew = currentObj == null; - //STANDARD EVENTS FOR ALL OBJECTS await NotifyEventHelper.ProcessStandardObjectEvents(ayaEvent, proposedObj, ct); @@ -513,47 +512,54 @@ namespace AyaNova.Biz //it not completed yet and not overdue already (which could indicate an import or something) if (r.CompletedDate == null && r.DueDate > DateTime.UtcNow) { - var userNotifySub = await NotifyEventHelper.EnsureDefaultInAppUserNotificationSubscriptionExists(r.UserId, ct); - NotifySubscription supervisorNotifySub = null; + //Notify user + await NotifyEventHelper.EnsureDefaultInAppUserNotificationSubscriptionExists(r.UserId, ct); + { + var gensubs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.GeneralNotification && z.UserId == r.UserId).ToListAsync(); + foreach (var sub in gensubs) + { + var eventNameTranslated = await TranslationBiz.GetTranslationForUserStaticAsync("ReviewOverDue", r.UserId); + NotifyEvent n = new NotifyEvent() + { + EventType = NotifyEventType.GeneralNotification, + UserId = r.UserId, + ObjectId = proposedObj.Id, + AyaType = AyaType.Review, + NotifySubscriptionId = sub.Id, + Name = $"{eventNameTranslated} - {proposedObj.Name}", + EventDate = r.DueDate + }; + await ct.NotifyEvent.AddAsync(n); + log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); + } + if (gensubs.Count > 0) + await ct.SaveChangesAsync(); + } + + //Notify supervisor if (r.UserId != r.AssignedByUserId) { - supervisorNotifySub = await NotifyEventHelper.EnsureDefaultInAppUserNotificationSubscriptionExists(r.AssignedByUserId, ct); - } - - { - var eventNameTranslated = await TranslationBiz.GetTranslationForUserStaticAsync("ReviewOverDue", r.UserId); - - NotifyEvent n = new NotifyEvent() + await NotifyEventHelper.EnsureDefaultInAppUserNotificationSubscriptionExists(r.AssignedByUserId, ct); + var gensubs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.GeneralNotification && z.UserId == r.AssignedByUserId).ToListAsync(); + foreach (var sub in gensubs) { - EventType = NotifyEventType.GeneralNotification, - UserId = r.UserId, - ObjectId = proposedObj.Id, - AyaType = AyaType.Review, - NotifySubscriptionId = userNotifySub.Id, - Name = $"{eventNameTranslated} - {proposedObj.Name}", - EventDate = r.DueDate - }; - await ct.NotifyEvent.AddAsync(n); - log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); + var eventNameTranslated = await TranslationBiz.GetTranslationForUserStaticAsync("ReviewOverDue", r.AssignedByUserId); + NotifyEvent n = new NotifyEvent() + { + EventType = NotifyEventType.GeneralNotification, + UserId = r.AssignedByUserId, + ObjectId = proposedObj.Id, + AyaType = AyaType.Review, + NotifySubscriptionId = sub.Id, + Name = $"{eventNameTranslated} - {proposedObj.Name}", + EventDate = r.DueDate + }; + await ct.NotifyEvent.AddAsync(n); + log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); + } + if (gensubs.Count > 0) + await ct.SaveChangesAsync(); } - - if (supervisorNotifySub != null) - { - var eventNameTranslated = await TranslationBiz.GetTranslationForUserStaticAsync("ReviewOverDue", r.AssignedByUserId); - NotifyEvent n = new NotifyEvent() - { - EventType = NotifyEventType.GeneralNotification, - UserId = r.AssignedByUserId, - ObjectId = proposedObj.Id, - AyaType = AyaType.Review, - NotifySubscriptionId = supervisorNotifySub.Id, - Name = $"{eventNameTranslated} - {proposedObj.Name}", - EventDate = r.DueDate - }; - await ct.NotifyEvent.AddAsync(n); - log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); - } - await ct.SaveChangesAsync(); } }//overdue event }//custom events for created / modified