diff --git a/server/AyaNova/biz/UnitBiz.cs b/server/AyaNova/biz/UnitBiz.cs index 7834d7fa..dd8c5ede 100644 --- a/server/AyaNova/biz/UnitBiz.cs +++ b/server/AyaNova/biz/UnitBiz.cs @@ -1,8 +1,9 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; +using System.Linq; using AyaNova.Util; using AyaNova.Api.ControllerHelpers; +using Microsoft.Extensions.Logging; using AyaNova.Models; namespace AyaNova.Biz @@ -283,6 +284,68 @@ namespace AyaNova.Biz await NotifyEventHelper.ProcessStandardObjectEvents(ayaEvent, proposedObj, ct); //SPECIFIC EVENTS FOR THIS OBJECT + /* + UnitWarrantyExpiry = 25,//* Unit object created, advance notice can be used, tag conditional + + //MIGRATE_OUTSTANDING + UnitMeterReadingMultipleExceeded = 26,//* UnitMeterReading object, Created, conditional on DecValue as the Multiple threshold, if passed then notifies + + */ + //## DELETED EVENTS + //TODO: there are no specific deleted events + //but any event added below needs to be removed, so + //just blanket remove any event for this object of eventtype that would be added below here + //do it regardless any time there's an update and then + //let this code below handle the refreshing addition that could have changes + + + //## CREATED EVENTS + if (ayaEvent == AyaEvent.Created || ayaEvent==AyaEvent.Modified) + { + + + Unit o = (Unit)proposedObj; + + //UNIT WARRANTY EXPIRY + { + //does the unit even have a model or warranty? + int effectiveWarrantyMonths=0; + //unit has own warranty terms + if(o.OverrideModelWarranty && !o.LifeTimeWarranty && o.WarrantyLength!=null && o.WarrantyLength>0){ + effectiveWarrantyMonths=(int)o.WarrantyLength; + }else{ + if(o.UnitModelId!=null){ +/todo: here is where I left off and some stuff above to delete once this below stuff is done. + } + + } + var subs = await ct.NotifySubscription.Where(z => z.EventType == NotifyEventType.UnitWarrantyExpiry).ToListAsync(); + foreach (var sub in subs) + { + //not for inactive users + if (!await UserBiz.UserIsActive(sub.UserId)) continue; + + //Tag match? (will be true if no sub tags so always safe to call this) + if (NotifyEventHelper.TagsMatch(o.Tags,sub.Tags)) + { + + NotifyEvent n = new NotifyEvent() + { + EventType = NotifyEventType.UnitWarrantyExpiry, + UserId = sub.UserId, + AyaType = o.AyaType, + ObjectId = o.Id, + NotifySubscriptionId = sub.Id, + Name = o.Serial, + }; + await ct.NotifyEvent.AddAsync(n); + log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]"); + await ct.SaveChangesAsync(); + } + } + } + } + }//end of process notifications