This commit is contained in:
2020-12-22 21:02:07 +00:00
parent 3c5585b2ec
commit 1e189ee74f
2 changed files with 233 additions and 144 deletions

View File

@@ -119,28 +119,6 @@ namespace AyaNova.Biz
}//eom
private static async Task<NotifySubscription> 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)
{
//NOTE: agevalue and advanced notice settings here will ensure that direct in app notifications with a future delivery date ("deadman" switch deliveries) set in their
//notifyevent.eventdate will deliver on that date and not immediately to support all the things that are direct built in notifications for future dates
//such as for an overdue Review which doesn't have or need it's own notifyeventtype and subscription independently
defaultsub = new NotifySubscription()
{
UserId = userId,
EventType = NotifyEventType.GeneralNotification,
DeliveryMethod = NotifyDeliveryMethod.App,
AgeValue = new TimeSpan(0, 0, 1),
AdvanceNotice = new TimeSpan(0, 0, 1)
};
await ct.NotifySubscription.AddAsync(defaultsub);
await ct.SaveChangesAsync();
}
return defaultsub;
}
@@ -299,56 +277,38 @@ namespace AyaNova.Biz
//-----------------------------------------------
//ObjectAge
//
{
var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectAge && z.AyaType == newObject.AyaType).ToListAsync();
foreach (var sub in subs)
{
if (TagsMatch(newObject.Tags, sub.Tags))
{
//Note: age is set by advance notice which is consulted by CoreJobNotify in it's run so the deliver date is not required here only the reference EventDate to check for deliver
//ObjectAge is determined by subscription AgeValue in combo with the EventDate NotifyEvent parameter which together determines at what age from notifyevent.EventDate it's considered for the event to have officially occured
//However delivery is determined by sub.advancenotice so all three values play a part
//
NotifyEvent n = new NotifyEvent()
{
EventType = NotifyEventType.ObjectAge,
UserId = sub.UserId,
AyaType = newObject.AyaType,
ObjectId = newObject.Id,
NotifySubscriptionId = sub.Id,
Name = newObject.Name
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
SaveContext = true;
}
}
}
// {
// var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectAge && z.AyaType == newObject.AyaType).ToListAsync();
// foreach (var sub in subs)
// {
// if (TagsMatch(newObject.Tags, sub.Tags))
// {
// //Note: age is set by advance notice which is consulted by CoreJobNotify in it's run so the deliver date is not required here only the reference EventDate to check for deliver
// //ObjectAge is determined by subscription AgeValue in combo with the EventDate NotifyEvent parameter which together determines at what age from notifyevent.EventDate it's considered for the event to have officially occured
// //However delivery is determined by sub.advancenotice so all three values play a part
// //
// NotifyEvent n = new NotifyEvent()
// {
// EventType = NotifyEventType.ObjectAge,
// UserId = sub.UserId,
// AyaType = newObject.AyaType,
// ObjectId = newObject.Id,
// NotifySubscriptionId = sub.Id,
// Name = newObject.Name
// };
// await ct.NotifyEvent.AddAsync(n);
// log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
// SaveContext = true;
// }
// }
// }
//------------------------------------------
//ObjectCreated
//
// [USER for any type, CUSTOMER for workorder only] / if customer then CopyOfCustomerNotification https://rockfish.ayanova.com/default.htm#!/rfcaseEdit/3398
{
var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectCreated && z.AyaType == newObject.AyaType).ToListAsync();
foreach (var sub in subs)
{
if (TagsMatch(newObject.Tags, sub.Tags))
{
NotifyEvent n = new NotifyEvent()
{
EventType = NotifyEventType.ObjectCreated,
UserId = sub.UserId,
AyaType = newObject.AyaType,
ObjectId = newObject.Id,
NotifySubscriptionId = sub.Id,
Name = newObject.Name
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
SaveContext = true;
}
}
}
// {
// await ProcessStandardObjectCreatedEvents(newObject, ct);
// }
#endregion created processing
break;
case AyaEvent.Modified:
@@ -567,35 +527,7 @@ namespace AyaNova.Biz
//------------------------------
// Delete any NotifyEvent records for this exact object
// It's gone and shouldn't have any events left for it
await ClearPriorEventsForObject(ct, newObject.AyaType, newObject.Id);
//------------------------------------------
//ObjectDeleted notification
//
{
var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectDeleted && z.AyaType == newObject.AyaType).ToListAsync();
foreach (var sub in subs)
{
if (TagsMatch(newObject.Tags, sub.Tags))
{
//TODO: On deliver should point to history event log record or take from there and insert into delivery message?
NotifyEvent n = new NotifyEvent()
{
EventType = NotifyEventType.ObjectDeleted,
UserId = sub.UserId,
AyaType = newObject.AyaType,
ObjectId = newObject.Id,
NotifySubscriptionId = sub.Id,
Name = newObject.Name
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
SaveContext = true;
}
}
}
await ProcessStandardObjectDeletedEvents(newObject, ct);
#endregion deleted processing
break;
@@ -625,6 +557,159 @@ namespace AyaNova.Biz
}//eom
///////////////////////////////////////////
// ENSURE USER HAS IN APP NOTIFICATION
//
//
public static async Task<NotifySubscription> 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)
{
//NOTE: agevalue and advanced notice settings here will ensure that direct in app notifications with a future delivery date ("deadman" switch deliveries) set in their
//notifyevent.eventdate will deliver on that date and not immediately to support all the things that are direct built in notifications for future dates
//such as for an overdue Review which doesn't have or need it's own notifyeventtype and subscription independently
defaultsub = new NotifySubscription()
{
UserId = userId,
EventType = NotifyEventType.GeneralNotification,
DeliveryMethod = NotifyDeliveryMethod.App,
AgeValue = new TimeSpan(0, 0, 1),
AdvanceNotice = new TimeSpan(0, 0, 1)
};
await ct.NotifySubscription.AddAsync(defaultsub);
await ct.SaveChangesAsync();
}
return defaultsub;
}
/////////////////////////////////////////
// PROCESS STANDARD EVENTS
//
//
public static async Task ProcessStandardObjectEvents(AyaEvent ayaEvent, ICoreBizObjectModel newObject, AyContext ct)
{
switch(ayaEvent){
case AyaEvent.Created:
await ProcessStandardObjectCreatedEvents(newObject,ct);
break;
case AyaEvent.Deleted:
await ProcessStandardObjectDeletedEvents(newObject,ct);
break;
case AyaEvent.Modified:
await ProcessStandardObjectModifiedEvents(newObject,ct);
break;
}
}
/////////////////////////////////////////
// PROCESS STANDARD CREATE NOTIFICATION
//
//
public static async Task ProcessStandardObjectCreatedEvents(ICoreBizObjectModel newObject, AyContext ct)
{
//CREATED SUBSCRIPTIONS
{
var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectCreated && z.AyaType == newObject.AyaType).ToListAsync();
foreach (var sub in subs)
{
if (TagsMatch(newObject.Tags, sub.Tags))
{
NotifyEvent n = new NotifyEvent()
{
EventType = NotifyEventType.ObjectCreated,
UserId = sub.UserId,
AyaType = newObject.AyaType,
ObjectId = newObject.Id,
NotifySubscriptionId = sub.Id,
Name = newObject.Name
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
await ct.SaveChangesAsync();
}
}
}
//AGE SUBSCRIPTIONS
{
var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectAge && z.AyaType == newObject.AyaType).ToListAsync();
foreach (var sub in subs)
{
if (TagsMatch(newObject.Tags, sub.Tags))
{
//Note: age is set by advance notice which is consulted by CoreJobNotify in it's run so the deliver date is not required here only the reference EventDate to check for deliver
//ObjectAge is determined by subscription AgeValue in combo with the EventDate NotifyEvent parameter which together determines at what age from notifyevent.EventDate it's considered for the event to have officially occured
//However delivery is determined by sub.advancenotice so all three values play a part
//
NotifyEvent n = new NotifyEvent()
{
EventType = NotifyEventType.ObjectAge,
UserId = sub.UserId,
AyaType = newObject.AyaType,
ObjectId = newObject.Id,
NotifySubscriptionId = sub.Id,
Name = newObject.Name
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
await ct.SaveChangesAsync();
}
}
}
}
/////////////////////////////////////////
// PROCESS STANDARD DELETE NOTIFICATION
//
//
public static async Task ProcessStandardObjectDeletedEvents(ICoreBizObjectModel bizObject, AyContext ct)
{
// It's gone and shouldn't have any events left for it
await ClearPriorEventsForObject(ct, bizObject.AyaType, bizObject.Id);
//------------------------------------------
//ObjectDeleted notification
//
{
var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectDeleted && z.AyaType == bizObject.AyaType).ToListAsync();
foreach (var sub in subs)
{
if (TagsMatch(bizObject.Tags, sub.Tags))
{
//TODO: On deliver should point to history event log record or take from there and insert into delivery message?
NotifyEvent n = new NotifyEvent()
{
EventType = NotifyEventType.ObjectDeleted,
UserId = sub.UserId,
AyaType = bizObject.AyaType,
ObjectId = bizObject.Id,
NotifySubscriptionId = sub.Id,
Name = bizObject.Name
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
await ct.SaveChangesAsync();
}
}
}
}
//////////////////////////////////
// CLEAN OUT OLD EVENTS
//