From 36d3bfedfd29a5b2a74556cedb64befcaacbdd0f Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 23 Aug 2021 23:25:20 +0000 Subject: [PATCH] --- .../ayanova/docs/home-notify-subscriptions.md | 2 +- server/AyaNova/biz/NotifySubscriptionBiz.cs | 16 +++++ server/AyaNova/biz/UnitBiz.cs | 13 +--- server/AyaNova/biz/UnitMeterReadingBiz.cs | 60 +++++++++++++++++++ 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/docs/8.0/ayanova/docs/home-notify-subscriptions.md b/docs/8.0/ayanova/docs/home-notify-subscriptions.md index bef76d66..728d806a 100644 --- a/docs/8.0/ayanova/docs/home-notify-subscriptions.md +++ b/docs/8.0/ayanova/docs/home-notify-subscriptions.md @@ -97,7 +97,7 @@ There are no settings adjustable for in app General notifications, however Users | WorkorderTotalExceedsThreshold | The balance of a Work order has exceeded a threshold (the "Andy") | | WorkorderStatusAge | A Workorder has been sitting at the selected status for longer than the selected time frame | | UnitWarrantyExpiry | A Unit's warranty expiration date is reached | -| UnitMeterReadingMultipleExceeded | A meter readingn *multiple* exceeds selected threshold (e.g. every 10,000 etc) | +| UnitMeterReadingMultipleExceeded | A meter reading has a difference from the last reading by the selected amount (negative OR positive to cover count *down* meters or rollover / reset meters) | | GeneralNotification | Any general notification including direct text notifications between users (built in but provided for email alternative delivery) | | ServerOperationsProblem | Any timely and serious issue related to internal AyaNova Server operations requiring attention | | QuoteStatusAge | A Quote has been sitting at the selected status for longer than the selected time frame | diff --git a/server/AyaNova/biz/NotifySubscriptionBiz.cs b/server/AyaNova/biz/NotifySubscriptionBiz.cs index 92dfd9e7..0ea57d59 100644 --- a/server/AyaNova/biz/NotifySubscriptionBiz.cs +++ b/server/AyaNova/biz/NotifySubscriptionBiz.cs @@ -229,6 +229,22 @@ namespace AyaNova.Biz AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "DeliveryAddress", "In app delivery should not specify a delivery address"); } + if (proposedObj.EventType == NotifyEventType.UnitMeterReadingMultipleExceeded) + { + //validate decvalue is a long + try + { + long lValue= System.Convert.ToInt64(proposedObj.DecValue); + if(lValue < 5){//arbitrary value + AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "DecValue", "Value is too small, must be 5 or higher"); + } + } + catch (System.OverflowException) + { + AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "DecValue", "Value is too large, must be less than 9,223,372,036,854,775,807"); + } + } + } diff --git a/server/AyaNova/biz/UnitBiz.cs b/server/AyaNova/biz/UnitBiz.cs index 2682c087..c4a9a376 100644 --- a/server/AyaNova/biz/UnitBiz.cs +++ b/server/AyaNova/biz/UnitBiz.cs @@ -558,18 +558,7 @@ namespace AyaNova.Biz } } }//Contract expiry event - - - //#todo: METER READING EVENT - //MIGRATE_OUTSTANDING need meter reading object to complete unit notification for UnitMeterReadingMultipleExceeded - //UnitMeterReadingMultipleExceeded = 26,//* UnitMeterReading object, Created, conditional on DecValue as the Multiple threshold, if passed then notifies - //{ - //first remove any existing, potentially stale notifyevents for this exact object and notifyeventtype - //await NotifyEventHelper.ClearPriorEventsForObject(ct, AyaType.Unit, o.Id, NotifyEventType.UnitMeterReadingMultipleExceeded); - - //then check if unit is still metered etc etc and do the rest once the unit meter reading is coded - - //} + } diff --git a/server/AyaNova/biz/UnitMeterReadingBiz.cs b/server/AyaNova/biz/UnitMeterReadingBiz.cs index 9afa80e6..c61be696 100644 --- a/server/AyaNova/biz/UnitMeterReadingBiz.cs +++ b/server/AyaNova/biz/UnitMeterReadingBiz.cs @@ -129,6 +129,12 @@ namespace AyaNova.Biz } } + //No negative amounts allowed, a meter can count down to zero or up to whatever but it can't go negative or the math breaks down + if (proposedObj.Meter < 0) + { + AddError(ApiErrorCode.VALIDATION_INVALID_VALUE, "Meter", "Negative meter readings not supported"); + } + } @@ -205,6 +211,60 @@ namespace AyaNova.Biz //then check if unit is still metered etc etc and do the rest once the unit meter reading is coded //} + /* + This could work as a threshold multiple value, i.e. if it's passing a multiple of 50,000 then notify, or notify every 10,000 or something. + In saving code it just checks to see if it has crossed the multiple threshold, i.e. calculate the old multiple, calculate the new multiple if over then send notification immediate. + Multiple is 10: + old was 5, new is 7 so 7-5 < 10 so do nothing + old was 5, new is 12 so 12-5=7 < 10 so do nothing + old was 5, new is 16 so 16-5==11 > 10 so do notification + + + */ + + { + //see if any subscribers + var subs = await ct.NotifySubscription.AsNoTracking().Where(z => z.EventType == NotifyEventType.UnitMeterReadingMultipleExceeded).ToListAsync(); + foreach (var sub in subs) + { + //not for inactive users + if (!await UserBiz.UserIsActive(sub.UserId)) continue; + //is multiple exceeded?? + //Formula is to retrieve last highest reading, if it's not found then it's zero + //subtract last meter reading from current meter reading and if it's equal or more than threshold trigger notification + + var lastMeterReading = await ct.UnitMeterReading.AsNoTracking() + .Where(x => x.UnitId == proposedObj.UnitId) + .OrderByDescending(z => z.MeterDate) + .Take(1).FirstOrDefaultAsync(); + long lastReading = 0; + if (lastMeterReading != null) + lastReading = lastMeterReading.Meter; + //NOTE: this will cover scenarios where the meter wraps back to zero or is reset to zero or a meter counts **down** instead of up + //meter readings are always positive due to biz rule + var diff = Math.Abs(proposedObj.Meter - lastReading); + + + if (sub.DecValue < diff) + { + //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(); + } + } + + } }//end of process notifications