This commit is contained in:
2021-01-01 16:07:07 +00:00
parent bdcca3ea2e
commit 31a5d4c0a7
3 changed files with 64 additions and 39 deletions

View File

@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using System.Linq;
@@ -300,47 +301,62 @@ namespace AyaNova.Biz
//## CREATED EVENTS
if (ayaEvent == AyaEvent.Created || ayaEvent==AyaEvent.Modified)
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;
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)
if (o.OverrideModelWarranty && !o.LifeTimeWarranty && o.WarrantyLength != null && o.WarrantyLength > 0)
{
//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))
effectiveWarrantyMonths = (int)o.WarrantyLength;
}
else
{
//unit has model based warranty terms
if (o.UnitModelId != null)
{
NotifyEvent n = new NotifyEvent()
UnitModel um = await ct.UnitModel.AsNoTracking().FirstOrDefaultAsync(z => z.Id == o.UnitModelId);
if (!um.LifeTimeWarranty && um.WarrantyLength != null && um.WarrantyLength > 0)
{
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();
effectiveWarrantyMonths = (int)um.WarrantyLength;
}
}
}
if (effectiveWarrantyMonths > 0)
{
var WarrantyExpirydate = DateTime.UtcNow.AddMonths(effectiveWarrantyMonths);
//notify users about warranty expiry (time delayed)
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,
EventDate = WarrantyExpirydate
};
await ct.NotifyEvent.AddAsync(n);
log.LogDebug($"Adding NotifyEvent: [{n.ToString()}]");
await ct.SaveChangesAsync();
}
}
}
}