From 84785dd6b97099a4d620ef68b255717dae3e8b99 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 20 Jul 2020 19:32:38 +0000 Subject: [PATCH] --- server/AyaNova/generator/CoreJobNotify.cs | 65 ++++++++++++++++----- server/AyaNova/models/NotifyEvent.cs | 4 +- server/AyaNova/models/NotifySubscription.cs | 4 +- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/server/AyaNova/generator/CoreJobNotify.cs b/server/AyaNova/generator/CoreJobNotify.cs index 311f29ef..0f7ce9aa 100644 --- a/server/AyaNova/generator/CoreJobNotify.cs +++ b/server/AyaNova/generator/CoreJobNotify.cs @@ -97,28 +97,49 @@ namespace AyaNova.Biz //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 items = await ct.NotifyEvent.Include(z => z.NotifySubscription).ToListAsync(); - + var events = await ct.NotifyEvent.Include(z => z.NotifySubscription).ToListAsync(); + log.LogTrace($"Found {events.Count} NotifyEvents to examine for potential delivery"); //iterate and deliver - foreach(var item in items){ - //Time delayed ones: - //when to deliver formula: NotifyEvent "EventDate"+NotifySubscription.AgeValue timespan - NotifySubscription AdvanceNotice timespan > utcNow - if(item.) - - //Immediate delivery items - // - if(item.EventDate) + foreach (var notifyevent in events) + { + //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? + if (notifyevent.NotifySubscription.AgeValue != TimeSpan.Zero) + { + var deliverAfter = notifyevent.EventDate + notifyevent.NotifySubscription.AgeValue - notifyevent.NotifySubscription.AdvanceNotice; + if (deliverAfter > DateTime.UtcNow) + { + if (notifyevent.NotifySubscription.DeliveryMethod == NotifyDeliveryMethod.App) + { + await DeliverInApp(notifyevent, ct); + } + if (notifyevent.NotifySubscription.DeliveryMethod == NotifyDeliveryMethod.SMTP) + { + await DeliverSMTP(notifyevent, ct); + } + } + } + else + { + //NORMAL IMMEDIATE DELIVERY EVENT + if (notifyevent.NotifySubscription.DeliveryMethod == NotifyDeliveryMethod.App) + { + await DeliverInApp(notifyevent, ct); + } + if (notifyevent.NotifySubscription.DeliveryMethod == NotifyDeliveryMethod.SMTP) + { + await DeliverSMTP(notifyevent, ct); + } + } } - //turn notifyEvent records into notifications for in app and deliver smtp - - - } } catch (Exception ex) { - + log.LogError(ex, $"Error processing notification event"); } finally { @@ -129,6 +150,20 @@ namespace AyaNova.Biz } + private static async Task DeliverInApp(NotifyEvent ne, AyContext ct) + { + log.LogTrace($"DeliverInApp deliving notify event: {ne}"); + await ct.Notification.AddAsync(new Notification() { UserId = ne.UserId, AyaType = ne.AyaType, ObjectId = ne.ObjectId, EventType = ne.EventType, NotifySubscriptionId = ne.NotifySubscriptionId, Message = ne.Message }); + ct.NotifyEvent.Remove(ne); + await ct.SaveChangesAsync(); + } + + private static async Task DeliverSMTP(NotifyEvent ne, AyContext ct) + { + log.LogTrace($"DeliverSMTP deliving notify event: {ne}"); + throw new System.NotImplementedException("DeliverSMTP: NOT IMPLEMENTED YET"); + + } ///////////////////////////////////////////////////////////////////// diff --git a/server/AyaNova/models/NotifyEvent.cs b/server/AyaNova/models/NotifyEvent.cs index 74ebae01..1c9efc25 100644 --- a/server/AyaNova/models/NotifyEvent.cs +++ b/server/AyaNova/models/NotifyEvent.cs @@ -28,7 +28,7 @@ namespace AyaNova.Models public long IdValue { get; set; } [Required] public decimal DecValue { get; set; } - + // [Required]// public TimeSpan AgeValue { get; set; } //date of the event actually occuring, e.g. WarrantyExpiry date. Compared with subscription to determine if deliverable or not @@ -43,7 +43,7 @@ namespace AyaNova.Models Created = DateTime.UtcNow; IdValue = 0; DecValue = 0; - // AgeValue = new TimeSpan(0, 0, 0); + // AgeValue = TimeSpan.Zero; AyaType = AyaType.NoType; ObjectId = 0; } diff --git a/server/AyaNova/models/NotifySubscription.cs b/server/AyaNova/models/NotifySubscription.cs index f369cd1f..e55ed60d 100644 --- a/server/AyaNova/models/NotifySubscription.cs +++ b/server/AyaNova/models/NotifySubscription.cs @@ -42,8 +42,8 @@ namespace AyaNova.Models AyaType = AyaType.NoType; IdValue = 0; DecValue = 0; - AgeValue = new TimeSpan(0, 0, 0); - AdvanceNotice = new TimeSpan(0, 0, 0); + AgeValue = TimeSpan.Zero; + AdvanceNotice = TimeSpan.Zero; AttachReportId = 0; }