From 797b87a1fe1d47752d6c6c18b39979ca965b67e9 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 16 Jun 2021 20:05:05 +0000 Subject: [PATCH] --- server/AyaNova/biz/WorkOrderBiz.cs | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/server/AyaNova/biz/WorkOrderBiz.cs b/server/AyaNova/biz/WorkOrderBiz.cs index 8a8840d2..f8977267 100644 --- a/server/AyaNova/biz/WorkOrderBiz.cs +++ b/server/AyaNova/biz/WorkOrderBiz.cs @@ -1152,6 +1152,64 @@ namespace AyaNova.Biz }//CustomerServiceImminent + //# WorkorderCreatedForCustomer - Customer / headoffice notification + if (oProposed.CustomerId != 0 && (ayaEvent == AyaEvent.Created + || (ayaEvent == AyaEvent.Modified && oCurrent.CustomerId != oProposed.CustomerId))) + { + + //look for potential subscribers + var subs = await ct.NotifySubscription.AsNoTracking().Where(z => z.EventType == NotifyEventType.WorkorderCreatedForCustomer).ToListAsync(); + foreach (var sub in subs) + { + //not for inactive users + if (!await UserBiz.UserIsActive(sub.UserId)) continue; + + //Customer User? + 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) + { + //CUSTOMER USER + + //Quick short circuit: if workorder doesn't have a customer id then it's not going to match no matter what + if (oProposed.CustomerId == 0) continue; + + var customerUserRights = await UserBiz.CustomerUserEffectiveRightsAsync(sub.UserId); + + //Are they allowed right now to use this type of notification? + if (!customerUserRights.NotifyWOCreated) continue; + + //is this their related work order? + if (UserInfo.CustomerId != oProposed.CustomerId) + { + //not the same customer but might be a head office user and this is one of their customers so check for that + if (UserInfo.HeadOfficeId == null) continue;//can't match any head office 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 == oProposed.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; + } + } + else + continue;//only customers can subscribe to this particular notification + + + //Ok, we're here so it must be ok to notify user + NotifyEvent n = new NotifyEvent() + { + EventType = NotifyEventType.WorkorderCreatedForCustomer, + UserId = sub.UserId, + AyaType = AyaType.WorkOrder, + ObjectId = oProposed.Id, + NotifySubscriptionId = sub.Id, + Name = $"{oProposed.Serial.ToString()}" + }; + await ct.NotifyEvent.AddAsync(n); + log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); + await ct.SaveChangesAsync(); + } + }//WorkorderCreatedForCustomer + #endregion }//end of process notifications