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 }//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 //ObjectAge
// //
{ // {
var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectAge && z.AyaType == newObject.AyaType).ToListAsync(); // var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.ObjectAge && z.AyaType == newObject.AyaType).ToListAsync();
foreach (var sub in subs) // foreach (var sub in subs)
{ // {
if (TagsMatch(newObject.Tags, sub.Tags)) // 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 // //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 // //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 // //However delivery is determined by sub.advancenotice so all three values play a part
// // //
NotifyEvent n = new NotifyEvent() // NotifyEvent n = new NotifyEvent()
{ // {
EventType = NotifyEventType.ObjectAge, // EventType = NotifyEventType.ObjectAge,
UserId = sub.UserId, // UserId = sub.UserId,
AyaType = newObject.AyaType, // AyaType = newObject.AyaType,
ObjectId = newObject.Id, // ObjectId = newObject.Id,
NotifySubscriptionId = sub.Id, // NotifySubscriptionId = sub.Id,
Name = newObject.Name // Name = newObject.Name
}; // };
await ct.NotifyEvent.AddAsync(n); // await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); // log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
SaveContext = true; // SaveContext = true;
} // }
} // }
} // }
//------------------------------------------ //------------------------------------------
//ObjectCreated //ObjectCreated
// //
// [USER for any type, CUSTOMER for workorder only] / if customer then CopyOfCustomerNotification https://rockfish.ayanova.com/default.htm#!/rfcaseEdit/3398 // [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(); // await ProcessStandardObjectCreatedEvents(newObject, ct);
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;
}
}
}
#endregion created processing #endregion created processing
break; break;
case AyaEvent.Modified: case AyaEvent.Modified:
@@ -567,35 +527,7 @@ namespace AyaNova.Biz
//------------------------------ //------------------------------
// Delete any NotifyEvent records for this exact object // Delete any NotifyEvent records for this exact object
// It's gone and shouldn't have any events left for it await ProcessStandardObjectDeletedEvents(newObject, ct);
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;
}
}
}
#endregion deleted processing #endregion deleted processing
break; break;
@@ -625,6 +557,159 @@ namespace AyaNova.Biz
}//eom }//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 // CLEAN OUT OLD EVENTS
// //

View File

@@ -465,7 +465,7 @@ namespace AyaNova.Biz
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
// NOTIFICATION PROCESSING // NOTIFICATION PROCESSING
// //
public async Task HandlePotentialNotificationEvent(AyaEvent ayaEvent, Review proposedObj, Review currentObj) public async Task HandlePotentialNotificationEvent(AyaEvent ayaEvent, ICoreBizObjectModel proposedObj, ICoreBizObjectModel currentObj)
{ {
ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger<ReviewBiz>(); ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger<ReviewBiz>();
if (ServerBootConfig.SEEDING) return; if (ServerBootConfig.SEEDING) return;
@@ -473,62 +473,66 @@ namespace AyaNova.Biz
bool isNew = currentObj == null; bool isNew = currentObj == null;
if (ayaEvent == AyaEvent.Deleted)
{
return; //STANDARD EVENTS FOR ALL OBJECTS
} await NotifyEventHelper.ProcessStandardObjectEvents(ayaEvent, proposedObj, ct);
//SPECIFIC EVENTS FOR THIS OBJECT
//CREATED / MODIFIED
if (ayaEvent == AyaEvent.Created || ayaEvent == AyaEvent.Modified) if (ayaEvent == AyaEvent.Created || ayaEvent == AyaEvent.Modified)
{ {
//Remove prior //OVERDUE pseudo event
await NotifyEventHelper.ClearPriorEventsForObject(ct, newObject.AyaType, newObject.Id, NotifyEventType.GeneralNotification);//assumes only general event for this object type is overdue here
//set a deadman automatic internal notification if goes past due
var r = (Review)newObject;
//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 EnsureDefaultInAppUserNotificationSubscriptionExists(r.UserId, ct); //Remove prior
NotifySubscription supervisorNotifySub = null; await NotifyEventHelper.ClearPriorEventsForObject(ct, proposedObj.AyaType, proposedObj.Id, NotifyEventType.GeneralNotification);//assumes only general event for this object type is overdue here
if (r.UserId != r.AssignedByUserId)
{
supervisorNotifySub = await EnsureDefaultInAppUserNotificationSubscriptionExists(r.AssignedByUserId, ct);
}
{ //set a deadman automatic internal notification if goes past due
NotifyEvent n = new NotifyEvent() var r = (Review)proposedObj;
{
EventType = NotifyEventType.GeneralNotification,
UserId = r.UserId,
ObjectId = newObject.Id,
AyaType = AyaType.Review,
NotifySubscriptionId = userNotifySub.Id,
Name = "LT:ReviewOverDue - " + newObject.Name,
EventDate = r.DueDate
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
}
if (supervisorNotifySub != null) //it not completed yet and not overdue already (which could indicate an import or something)
if (r.CompletedDate == null && r.DueDate > DateTime.UtcNow)
{ {
NotifyEvent n = new NotifyEvent() var userNotifySub = await NotifyEventHelper.EnsureDefaultInAppUserNotificationSubscriptionExists(r.UserId, ct);
NotifySubscription supervisorNotifySub = null;
if (r.UserId != r.AssignedByUserId)
{ {
EventType = NotifyEventType.GeneralNotification, supervisorNotifySub = await NotifyEventHelper.EnsureDefaultInAppUserNotificationSubscriptionExists(r.AssignedByUserId, ct);
UserId = r.AssignedByUserId, }
ObjectId = newObject.Id,
AyaType = AyaType.Review, {
NotifySubscriptionId = supervisorNotifySub.Id, NotifyEvent n = new NotifyEvent()
Name = "LT:ReviewOverDue - " + newObject.Name, {
EventDate = r.DueDate EventType = NotifyEventType.GeneralNotification,
}; UserId = r.UserId,
await ct.NotifyEvent.AddAsync(n); ObjectId = proposedObj.Id,
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); AyaType = AyaType.Review,
NotifySubscriptionId = userNotifySub.Id,
Name = "LT:ReviewOverDue - " + proposedObj.Name,
EventDate = r.DueDate
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
}
if (supervisorNotifySub != null)
{
NotifyEvent n = new NotifyEvent()
{
EventType = NotifyEventType.GeneralNotification,
UserId = r.AssignedByUserId,
ObjectId = proposedObj.Id,
AyaType = AyaType.Review,
NotifySubscriptionId = supervisorNotifySub.Id,
Name = "LT:ReviewOverDue - " + proposedObj.Name,
EventDate = r.DueDate
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
}
} }
} }//overdue event
} }//custom events for created / modified
} }