diff --git a/server/AyaNova/Controllers/NotifyController.cs b/server/AyaNova/Controllers/NotifyController.cs index d9f7e29e..4ab3f627 100644 --- a/server/AyaNova/Controllers/NotifyController.cs +++ b/server/AyaNova/Controllers/NotifyController.cs @@ -1,5 +1,4 @@ using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Authorization; @@ -7,7 +6,8 @@ using Microsoft.Extensions.Logging; using AyaNova.Models; using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; - +using System.Linq; +using Microsoft.EntityFrameworkCore; namespace AyaNova.Api.Controllers { diff --git a/server/AyaNova/biz/UserBiz.cs b/server/AyaNova/biz/UserBiz.cs index 9b759ac2..be88b0c9 100644 --- a/server/AyaNova/biz/UserBiz.cs +++ b/server/AyaNova/biz/UserBiz.cs @@ -47,21 +47,39 @@ namespace AyaNova.Biz var TotalActiveTechs = await ActiveCountAsync(); int CountOfTechsToSetInactive = (int)(TotalActiveTechs - KeepThisManyActiveTechs); if (CountOfTechsToSetInactive < 1) return; - _log.LogInformation($"New license is a downgrade with fewer scheduleable resources, deactivating {CountOfTechsToSetInactive} scheduleable users with the oldest last login dates to make room for the new license"); - + _log.LogInformation($"New license is a downgrade with fewer scheduleable resources, deactivating {CountOfTechsToSetInactive} scheduleable users automatically so the license can be installed"); + //bugbug: exactly wrong, deactivating the most recently logged in over some who never logged in, could be that null login is causing it? using (AyContext ct = ServiceProviderProvider.DBContext) { - var ActiveTechList = await ct.User.Where(z => z.Active == true && ( - z.UserType == UserType.Service || - z.UserType == UserType.ServiceContractor)).OrderBy(z => z.LastLogin).Take(CountOfTechsToSetInactive).ToListAsync(); - foreach (User u in ActiveTechList) + + //Algorithm: For deactivation favor subcontractors first over servicetechs, favor ones that have no login records and finally favor by oldest last login + //theory is to catch the least likely to be currently active techs + + //TODO: notify OPSNOTIFY Notify about each and every tech that is set inactive to bizadmin users + + var NoLoginTechList = await ct.User.Where(z => z.Active == true && z.LastLogin == null && ( + z.UserType == UserType.Service || + z.UserType == UserType.ServiceContractor)).OrderByDescending(z => z.UserType).Take(CountOfTechsToSetInactive).ToListAsync(); + + CountOfTechsToSetInactive -= NoLoginTechList.Count(); + foreach (User u in NoLoginTechList) { u.Active = false; - _log.LogInformation($"User {u.Name} set to inactive"); + _log.LogInformation($"User {u.Name} with no prior login automatically set to inactive to free up license"); + } + await ct.SaveChangesAsync(); + + + var HasLoginTechList = await ct.User.Where(z => z.Active == true && z.LastLogin != null && ( + z.UserType == UserType.Service || + z.UserType == UserType.ServiceContractor)).OrderByDescending(z => z.UserType).ThenBy(z => z.LastLogin).Take(CountOfTechsToSetInactive).ToListAsync(); + foreach (User u in HasLoginTechList) + { + u.Active = false; + _log.LogInformation($"User {u.Name} who last logged in {u.LastLogin} automatically set to inactive to free up license"); } await ct.SaveChangesAsync(); } - } internal static void ResetSuperUserPassword()