From 221a8f8b1b14ef85a61aa50ae3fd0febc1abdfdd Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 8 Jun 2021 19:53:11 +0000 Subject: [PATCH] --- server/AyaNova/generator/CoreJobNotify.cs | 155 ++++++++-------------- 1 file changed, 56 insertions(+), 99 deletions(-) diff --git a/server/AyaNova/generator/CoreJobNotify.cs b/server/AyaNova/generator/CoreJobNotify.cs index 8a56c692..59dc6578 100644 --- a/server/AyaNova/generator/CoreJobNotify.cs +++ b/server/AyaNova/generator/CoreJobNotify.cs @@ -79,32 +79,7 @@ namespace AyaNova.Biz using (AyContext ct = AyaNova.Util.ServiceProviderProvider.DBContext) - { - //select all jobs with no deliver date or deliver date no longer in future - - //IMPLEMENTATION NOTES: This code - // finds the events in NotifyEvent - // check subscription to determine if the age related ones are deliverable now - // check subscriptions for each delivery type, i.e. there might be both smtp and inapp separately for the same event so it needs to know that and deliver accordingly - // remove the notify event after it's processed - - - - //Older notes: - //### PLAN if it's an smtp delivery that fails and it's to someone who can be delivered in app then it should send an inapp notification of - //delivery failure and still delete the smtp delivery - //If it's not possible to notify the person via in app of the failed smtp then perhaps it notifies OPS personnel and biz admin personnel - //NEW NOTIFICATION SUBSCRIPTION EVENT TYPE: - //OPERATIONS_PROBLEMS - backup, notifications, out of memory, what have you, anyone can subscribe to it regardless of rights - //this is just to let people know there is a problem - //todo: create message here if not already set? - //todo: Link to open report in here - //CLIENT EXPECTS: open report links to have a query string [CLIENTAPPURL]/viewreport?oid=[objectid]&rid=[reportid] - - - //All items have an event date, for non time delayed events it's just the moment it was created - //which will predate this moment now if it's pre-existing - //var events = await ct.NotifyEvent.AsNoTracking().Include(z => z.User).Include(z => z.NotifySubscription).ToListAsync(); + { var events = await ct.NotifyEvent.AsNoTracking().ToListAsync(); log.LogTrace($"Found {events.Count} NotifyEvents to examine for potential delivery"); @@ -130,8 +105,7 @@ namespace AyaNova.Biz //iterate and deliver foreach (var notifyevent in events) { - //no notifications for inactive users, just delete it as if it was delivered - // if (!notifyevent.User.Active) + //no notifications for inactive users, just delete it as if it was delivered var UserInfo = await ct.User.AsNoTracking().Where(x => x.Id == notifyevent.UserId).Select(x => new { Active = x.Active, Name = x.Name }).FirstOrDefaultAsync(); if (!UserInfo.Active) { @@ -141,21 +115,12 @@ namespace AyaNova.Biz continue; } - //Get subscription for delivery var Subscription = await ct.NotifySubscription.AsNoTracking().FirstOrDefaultAsync(x => x.Id == notifyevent.NotifySubscriptionId); - //TIME DELAYED AGED EVENT? - //when to time delay deliver formula:If sub.agevalue!= timespan.zero then deliver on = - //NotifyEvent "EventDate"+NotifySubscription.AgeValue timespan - NotifySubscription AdvanceNotice timespan > utcNow - //Is it time delayed? - //(NOTE: some direct notifications in app e.g. overdue review will have a future date set as a deadman switch to deliver after unless completed before then - //their EventDate will be that future date, however regular in app notifications will go through here too but their evendate will be the moment they are created so should still - //deliver immediately-ish) - //NOTE: There is no need to separate out future delivery and immediate delivery because // All events have an event date, it's either immediate upon creation or it's future - //but not all events ahve an age value including ones with future event dates, + // but not all events have an age value including ones with future event dates, // and the default agevalue and advancenotice are both zero regardless so the block below works for either future or immediate deliveries var deliverAfter = notifyevent.EventDate + Subscription.AgeValue - Subscription.AdvanceNotice; @@ -164,12 +129,9 @@ namespace AyaNova.Biz if (Subscription.DeliveryMethod == NotifyDeliveryMethod.App) await DeliverInApp(notifyevent, ct); else if (Subscription.DeliveryMethod == NotifyDeliveryMethod.SMTP) - await DeliverSMTP(notifyevent, ct); - + await DeliverSMTP(notifyevent, Subscription, ct); } - } - } } catch (Exception ex) @@ -222,9 +184,6 @@ namespace AyaNova.Biz private static async Task DeliverInApp(NotifyEvent ne, AyContext ct) { log.LogTrace($"DeliverInApp notify event: {ne}"); -#if (DEBUG) - log.LogInformation($"DeliverInApp notify event: {ne}"); -#endif await ct.Notification.AddAsync(new Notification() { UserId = ne.UserId, AyaType = ne.AyaType, ObjectId = ne.ObjectId, EventType = ne.EventType, NotifySubscriptionId = ne.NotifySubscriptionId, Message = ne.Message, Name = ne.Name }); await ct.SaveChangesAsync(); ct.NotifyEvent.Remove(ne); @@ -234,75 +193,73 @@ namespace AyaNova.Biz - private static async Task DeliverSMTP(NotifyEvent ne, AyContext ct) + private static async Task DeliverSMTP(NotifyEvent ne, NotifySubscription sub, AyContext ct) { - await Task.CompletedTask; - throw new NotSupportedException("NEW CACHING CODE NEEDS TO BE ADDED BEFORE SMTP DELIVERIES CAN RESUME TESTING"); - + //TODO: UNCOMMENT THE FOLLOWING AND ADD CACHING AS PER ABOVE - // log.LogTrace($"DeliverSMTP delivering notify event: {ne}"); - // if (string.IsNullOrWhiteSpace(ne.NotifySubscription.DeliveryAddress)) - // await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification, $"No email address is set in subscription to deliver email notification for this event:{ne}", "Error", null, ne.UserId); + log.LogTrace($"DeliverSMTP delivering notify event: {ne}"); + if (string.IsNullOrWhiteSpace(sub.DeliveryAddress)) + await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification, $"No email address is set in subscription to deliver email notification for this event:{ne}", "Error", null, ne.UserId); - // var transid = _UserTranslationIdCache[ne.NotifySubscription.UserId]; - // var name = ne.Name; - // if (name == "~SERVER~") - // name = _ServerTheWordTranslations.First(z => z.Key == transid).Value; + var transid = _UserTranslationIdCache[sub.UserId]; + var name = ne.Name; + if (name == "~SERVER~") + name = _ServerTheWordTranslations.First(z => z.Key == transid).Value; - // //AyaType translation - // var AyaTypeTranslated = string.Empty; - // if (ne.AyaType != AyaType.NoType) - // AyaTypeTranslated = $":{GetTranslatedAyaTypeName(ne.AyaType, transid)}"; + //AyaType translation + var AyaTypeTranslated = string.Empty; + if (ne.AyaType != AyaType.NoType) + AyaTypeTranslated = $":{GetTranslatedAyaTypeName(ne.AyaType, transid)}"; - // var subject = $"AY{AyaTypeTranslated}:{GetTranslatedNotifyEventName(ne.EventType, transid)}:{name}"; + var subject = $"AY{AyaTypeTranslated}:{GetTranslatedNotifyEventName(ne.EventType, transid)}:{name}"; - // //subscription link translation - // var SubscriptionTypeName = GetTranslatedAyaTypeName(AyaType.NotifySubscription, transid); + //subscription link translation + var SubscriptionTypeName = GetTranslatedAyaTypeName(AyaType.NotifySubscription, transid); - // IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer; - // try - // { - // var body = ""; - // //NOTE: if need any other exemptions besides backup status make a separate static function "CanOpen(NotifyEventType)" + IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer; + try + { + var body = ""; + //NOTE: if need any other exemptions besides backup status make a separate static function "CanOpen(NotifyEventType)" - // if (ne.ObjectId != 0 || ne.EventType == NotifyEventType.BackupStatus) - // { - // body = OpenObjectUrlBuilder(ne.AyaType, ne.ObjectId, ne.EventType) + "\n"; - // } - // body += ne.Message; + if (ne.ObjectId != 0 || ne.EventType == NotifyEventType.BackupStatus) + { + body = OpenObjectUrlBuilder(ne.AyaType, ne.ObjectId, ne.EventType) + "\n"; + } + body += ne.Message; - // //Add link to subscription - // //http://localhost:8080/open/51/1 //add subscription link, notifysub is object type 51 - // if (!body.EndsWith('\n')) - // body += "\n"; + //Add link to subscription + //http://localhost:8080/open/51/1 //add subscription link, notifysub is object type 51 + if (!body.EndsWith('\n')) + body += "\n"; - // body += $"-----\n({SubscriptionTypeName}: {OpenSubscriptionUrlBuilder(ne.NotifySubscriptionId)} )\n"; + body += $"-----\n({SubscriptionTypeName}: {OpenSubscriptionUrlBuilder(ne.NotifySubscriptionId)} )\n"; - // if (!ServerGlobalOpsSettingsCache.Notify.SmtpDeliveryActive) - // { - // await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification, $"Email notifications are set to OFF at server, unable to send email notification for this event:{ne}", "Error", null, ne.UserId); - // log.LogInformation($"** WARNING: SMTP notification is currently set to Active=False; unable to deliver email notification, re-routed to in-app notification instead [UserId={ne.UserId}, Notify subscription={ne.NotifySubscriptionId}]. Change this setting or have users remove email delivery notifications if this is permanent **"); - // } - // else - // { - // await m.SendEmailAsync(ne.NotifySubscription.DeliveryAddress, subject, body, ServerGlobalOpsSettingsCache.Notify); - // } - // } - // catch (Exception ex) - // { - // await NotifyEventHelper.AddOpsProblemEvent("SMTP Notification failed", ex); - // await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification, $"An error prevented delivering the following notification via email. System operator users have been notified:{ne}", "Error", null, ne.UserId); - // } - // finally - // { - // //remove event no matter what - // ct.NotifyEvent.Remove(ne); - // await ct.SaveChangesAsync(); - // } + if (!ServerGlobalOpsSettingsCache.Notify.SmtpDeliveryActive) + { + await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification, $"Email notifications are set to OFF at server, unable to send email notification for this event:{ne}", "Error", null, ne.UserId); + log.LogInformation($"** WARNING: SMTP notification is currently set to Active=False; unable to deliver email notification, re-routed to in-app notification instead [UserId={ne.UserId}, Notify subscription={ne.NotifySubscriptionId}]. Change this setting or have users remove email delivery notifications if this is permanent **"); + } + else + { + await m.SendEmailAsync(sub.DeliveryAddress, subject, body, ServerGlobalOpsSettingsCache.Notify); + } + } + catch (Exception ex) + { + await NotifyEventHelper.AddOpsProblemEvent("SMTP Notification failed", ex); + await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification, $"An error prevented delivering the following notification via email. System operator users have been notified:{ne}", "Error", null, ne.UserId); + } + finally + { + //remove event no matter what + ct.NotifyEvent.Remove(ne); + await ct.SaveChangesAsync(); + } } //Called from ops notification settings to test smtp setup by delivering to address of choosing