This commit is contained in:
2021-08-03 15:58:50 +00:00
parent ce663cc36b
commit 0effc2e85f

View File

@@ -4748,26 +4748,83 @@ namespace AyaNova.Biz
#region GENERATION
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// Process generation of pms to workorders
//
internal static async Task ProcessInsufficientInventoryNotificationAsync(AyContext ct, ILogger log)
{
//TODO: how to know when to stop, won't this just keep repeating??
//check log? Has it's own frequency unlike 12 hour thing??
//ideally sb once only, perhaps once only every 90 days or however long the log is kept for
//quick check if *anyone* is subscribed to this early exit if not
//still here, check list again to see if it's been notified for that subscription id and remove it if yes
//some kind of collection + query magic here?
//check log? Has it's own frequency unlike 12 hour thing??
//ideally sb once only, perhaps once only every 90 days or however long the log is kept for
//still here? subscriber List not empty? Then get a list of all upcoming PM's by id and date
//iterate PM's
//each PM
//iterate list of subscribers and see if any pm's are within the threshold
//if so, iterate pm examine inventory and determine if short of anything and build a notification for it line by line to send as message:
//quick check if *anyone* is subscribed to this early exit if not
var subs = await ct.NotifySubscription.AsNoTracking().Where(z => z.EventType == NotifyEventType.PMInsufficientInventory).ToListAsync();
if (subs.Count == 0) return;
foreach (var sub in subs)
{
//not for inactive users
if (!await UserBiz.UserIsActive(sub.UserId)) continue;
//ok, active user with active subscription
//let's check for pm's within their range which is any pm's from NOW to NOW + AdvanceNotice
var checkUpToDate = DateTime.UtcNow + sub.AdvanceNotice;
//get the id's of pm's within the advance notification window for this user that are active
var l = await ct.PM.AsNoTracking()
.Where(z => z.GenerateDate < checkUpToDate && z.Active == true)
.Select(z => z.Id)
.ToListAsync();
#if (DEBUG)
if (l.Count > 0)
log.LogInformation($"Found {l.Count} inventory checkable PM orders for subscription id {sub.Id}");
#endif
//process those pms
foreach (long pmid in l)
{
#if (DEBUG)
log.LogInformation($"processing pm id {pmid}");
#endif
//look for same delivery already made and skip if already notified (sb one time only but will repeat for > 90 days as delivery log gets pruned)
if (await ct.NotifyDeliveryLog.AnyAsync(z => z.NotifySubscriptionId == sub.Id && z.ObjectId == pmid))
{
log.LogTrace($"PM {pmid} insufficient inventory already notified to subscriber within last 90 days, no need to send again, skipping");
#if (DEBUG)
log.LogInformation($"PM {pmid} insufficient inventory already notified to subscriber within last 90 days, no need to send again, skipping");
#endif
continue;
}
//Ok, it's worth checking out and could be a potential notification
//get the relevant bits of the PM
var p = await ct.PM.AsSplitQuery()
.Include(w => w.Items.OrderBy(item => item.Sequence))
.ThenInclude(wi => wi.Parts)
.SingleOrDefaultAsync(z => z.Id == pmid);
if (p == null)
{
//extremely unlikely to happen but just in case...
log.LogError($"PM was not fetchable when attempting to process PM id: {pmid}, deleted during processing?");
continue;
}
//check inventory and notify
//if so, iterate pm examine inventory and determine if short of anything and build a notification for it line by line to send as message:
//title = pm insufficient and id and object link to pm
//message = list: part name, -xx (short quantity), no text if possible so no translate needed
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
@@ -4785,14 +4842,14 @@ namespace AyaNova.Biz
.ToListAsync();
#if (DEBUG)
if (l.Count > 0)
log.LogInformation($"PMBiz - Found {l.Count} ready to generate PM items");
#endif
log.LogInformation($"Found {l.Count} ready to generate PM orders");
#endif
//process those items
//process those pms
foreach (long pmid in l)
{
#if (DEBUG)
log.LogInformation($"PMBiz - processing pm id {pmid}");
log.LogInformation($"processing pm id {pmid}");
#endif
var p = await ct.PM.AsSplitQuery()
.Include(w => w.Items.OrderBy(item => item.Sequence))
@@ -4819,12 +4876,11 @@ namespace AyaNova.Biz
if (p == null)
{
//extremely unlikely to happen but just in case...
log.LogError($"PMBiz - PM was not fetchable when attempting to process PM id: {pmid}, deleted during processing?");
log.LogError($"PM was not fetchable when attempting to process PM id: {pmid}, deleted during processing?");
continue;
}
try
{
throw new Exception("TEST EXCEPTION IN PM GENERATION");
//make new workorder
if (await NewServiceWorkOrderFromPMAsync(p, ct, log))
{
@@ -4857,7 +4913,7 @@ namespace AyaNova.Biz
{
p.Active = false;
await ct.SaveChangesAsync();
log.LogTrace($"PMBiz:: PM {p.Serial} has reached it's stop generating date and has been automatically deactivated");
log.LogTrace($"PM {p.Serial} has reached it's stop generating date and has been automatically deactivated");
continue;
}
@@ -4904,12 +4960,12 @@ namespace AyaNova.Biz
{
await ct.SaveChangesAsync();
#if (DEBUG)
log.LogInformation($"PMBiz::GenerateAsync updated PM after successful generation {p.Serial}");
log.LogInformation($"updated PM after successful generation {p.Serial}");
#endif
}
catch (Exception ex)
{
log.LogError(ex, $"PMBiz::GenerateAsync error updating PM after generation {p.Serial}");
log.LogError(ex, $"error updating PM after generation {p.Serial}");
await NotifyEventHelper.AddGeneralNotifyEvent(AyaType.PM, p.Id, NotifyEventType.PMGenerationFailed, $"Error updating PM after generation {p.Serial}", "Preventive Maintenance", ex);
continue;
}
@@ -4918,7 +4974,7 @@ namespace AyaNova.Biz
}
catch (Exception ex)
{
log.LogError(ex, $"PMBiz::GenerateAsync error generating Work order from PM {p.Serial}");
log.LogError(ex, $"error generating Work order from PM {p.Serial}");
await NotifyEventHelper.AddGeneralNotifyEvent(AyaType.PM, p.Id, NotifyEventType.PMGenerationFailed, $"Error generating Work order from PM {p.Serial}", "Preventive Maintenance", ex);
continue;
}