From 9a9dae5aea119fdc2c7b25492472eaef2062df4a Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 16 Jun 2021 19:30:19 +0000 Subject: [PATCH] --- server/AyaNova/Controllers/AuthController.cs | 2 +- server/AyaNova/biz/UserBiz.cs | 2 +- server/AyaNova/biz/WorkOrderBiz.cs | 93 +++++++++++++++++++- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/server/AyaNova/Controllers/AuthController.cs b/server/AyaNova/Controllers/AuthController.cs index a84f3e2f..0485c87e 100644 --- a/server/AyaNova/Controllers/AuthController.cs +++ b/server/AyaNova/Controllers/AuthController.cs @@ -351,7 +351,7 @@ namespace AyaNova.Api.Controllers if (u.UserType == UserType.Customer | u.UserType == UserType.HeadOffice) { //customer type has special rights restrictions for UI features so return them here so client UI can enable or disable - var effectiveRights = await UserBiz.CustomerUserEffectiveRights(u.Id); + var effectiveRights = await UserBiz.CustomerUserEffectiveRightsAsync(u.Id); return Ok(ApiOkResponse.Response(new { token = token, diff --git a/server/AyaNova/biz/UserBiz.cs b/server/AyaNova/biz/UserBiz.cs index 004cd1b8..fc7e3b5e 100644 --- a/server/AyaNova/biz/UserBiz.cs +++ b/server/AyaNova/biz/UserBiz.cs @@ -107,7 +107,7 @@ namespace AyaNova.Biz } } - internal static async Task CustomerUserEffectiveRights(long userId) + internal static async Task CustomerUserEffectiveRightsAsync(long userId) { using (AyContext ct = ServiceProviderProvider.DBContext) { diff --git a/server/AyaNova/biz/WorkOrderBiz.cs b/server/AyaNova/biz/WorkOrderBiz.cs index 63050ef8..ea95ebc3 100644 --- a/server/AyaNova/biz/WorkOrderBiz.cs +++ b/server/AyaNova/biz/WorkOrderBiz.cs @@ -1310,7 +1310,7 @@ namespace AyaNova.Biz bool isNew = currentObj == null; WorkOrderState oProposed = (WorkOrderState)proposedObj; - var WorkorderInfo = await ct.WorkOrder.AsNoTracking().Where(x => x.Id == oProposed.WorkOrderId).Select(x => new { Serial = x.Serial, Tags = x.Tags }).FirstOrDefaultAsync(); + var WorkorderInfo = await ct.WorkOrder.AsNoTracking().Where(x => x.Id == oProposed.WorkOrderId).Select(x => new { x.Serial, x.Tags, x.CustomerId }).FirstOrDefaultAsync(); WorkOrderStatus wos = await ct.WorkOrderStatus.AsNoTracking().FirstOrDefaultAsync(x => x.Id == oProposed.WorkOrderStatusId); oProposed.Name = WorkorderInfo.Serial.ToString(); oProposed.Tags = WorkorderInfo.Tags; @@ -1479,6 +1479,97 @@ namespace AyaNova.Biz + + + + + //# WorkorderCompleted - Customer AND User but customer only notifies if it's their workorder + { + if (wos.Completed) + { + + //look for potential subscribers + var subs = await ct.NotifySubscription.AsNoTracking().Where(z => z.EventType == NotifyEventType.WorkorderCompleted).ToListAsync(); + foreach (var sub in subs) + { + //not for inactive users + if (!await UserBiz.UserIsActive(sub.UserId)) continue; + + //Ok, is this a customer user and if so do they have rights to subscribe to this and if so is it their related workorder + var UserInfo = await ct.User.AsNoTracking().Where(x => x.Id == sub.UserId).Select(x => new { x.CustomerId, x.UserType, x.HeadOfficeId }).FirstOrDefaultAsync(); + if (UserInfo.UserType == UserType.Customer || UserInfo.UserType == UserType.HeadOffice) + { + //Quick short circuit: if workorder doesn't have a customer id then it's not going to match no matter what + if (WorkorderInfo.CustomerId == 0) continue; + + //CUSTOMER USER + var customerUserRights = await UserBiz.CustomerUserEffectiveRightsAsync(sub.UserId); + + //Are they allowed right now to use this type of notification? + if (!customerUserRights.NotifyWOCompleted) continue; + + //is this their related work order? + if (UserInfo.CustomerId != WorkorderInfo.CustomerId) + { + //not the same customer but might be the same head office which is kosher here + if (UserInfo.HeadOfficeId == null) continue;//can't match so no need to go further + + //see if workorder customer's head office is the same id as the user's headofficeid (note that a customer user with the same head office as a *different* customer workorder doesn't qualify) + var CustomerInfo = await ct.Customer.AsNoTracking().Where(x => x.Id == WorkorderInfo.CustomerId).Select(x => new { x.HeadOfficeId, x.BillHeadOffice }).FirstOrDefaultAsync(); + if (!CustomerInfo.BillHeadOffice) continue;//can't possibly match so no need to go further + if (UserInfo.HeadOfficeId != CustomerInfo.HeadOfficeId) continue; + } + + //Ok, we're here so it must be a related workorder so notify away + + + + + } + else + { + //INSIDE USER + + //Tag match? (will be true if no sub tags so always safe to call this) + //check early to avoid cost of fetching and calculating total if unnecessary + if (!NotifyEventHelper.ObjectHasAllSubscriptionTags(WorkorderInfo.Tags, sub.Tags)) continue; + + //get the total because we have at least one subscriber and matching tags + if (haveTotal == false) + { + GrandTotal = await WorkorderGrandTotalAsync(oProposed.WorkOrderId, ct); + haveTotal = true; + + //Note: not a time delayed notification, however user could be flipping states quickly triggering multiple notifications that are in queue temporarily + //so this will prevent that: + await NotifyEventHelper.ClearPriorEventsForObject(ct, AyaType.WorkOrder, oProposed.WorkOrderId, NotifyEventType.WorkorderTotalExceedsThreshold); + } + //Ok, we're here because there is a subscriber who is active and tags match so only check left is total against decvalue + if (sub.DecValue < GrandTotal) + { + //notification is a go + NotifyEvent n = new NotifyEvent() + { + EventType = NotifyEventType.WorkorderTotalExceedsThreshold, + UserId = sub.UserId, + AyaType = AyaType.WorkOrder, + ObjectId = oProposed.WorkOrderId, + NotifySubscriptionId = sub.Id, + Name = $"{WorkorderInfo.Serial.ToString()}", + DecValue = GrandTotal + }; + await ct.NotifyEvent.AddAsync(n); + log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); + await ct.SaveChangesAsync(); + } + } + } + } + }//WorkorderCompleted + + + + } }//end of process notifications