feature: scheduled flag removal

This commit is contained in:
Gary Sharp
2025-09-19 12:18:45 +10:00
parent 356762c811
commit 7603cac01a
34 changed files with 2210 additions and 1055 deletions
@@ -1,5 +1,4 @@
using Disco.Models.Repository;
using Disco.Services;
using Disco.Services;
using Disco.Services.Web;
using System;
using System.Data.Entity;
@@ -10,34 +9,25 @@ namespace Disco.Web.Areas.API.Controllers
{
public partial class DeviceFlagAssignmentController : AuthorizedDatabaseController
{
const string pComments = "comments";
[HttpPost, ValidateAntiForgeryToken]
public virtual ActionResult Update(int id, string key, string value = null, bool? redirect = null)
public virtual ActionResult Edit(int id, string comments, DateTime? removeDate, bool? redirect = null)
{
try
{
if (id < 0)
throw new ArgumentOutOfRangeException(nameof(id));
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException(nameof(key));
var assignment = Database.DeviceFlagAssignments
.Include(a => a.DeviceFlag)
.FirstOrDefault(a => a.Id == id);
if (assignment != null)
{
switch (key.ToLower())
{
case pComments:
UpdateComments(assignment, value);
break;
default:
throw new Exception("Invalid Update Key");
}
}
else
{
throw new Exception("Invalid Device Flag Assignment Id");
}
.FirstOrDefault(a => a.Id == id)
?? throw new Exception("Invalid Device Flag Assignment Id");
if (!assignment.CanEdit())
throw new InvalidOperationException("Editing comments for device flags is denied");
assignment.OnEdit(comments, removeDate);
Database.SaveChanges();
if (redirect.HasValue && redirect.Value)
return Redirect($"{Url.Action(MVC.Device.Show(assignment.DeviceSerialNumber))}#DeviceDetailTab-Flags");
else
@@ -52,43 +42,30 @@ namespace Disco.Web.Areas.API.Controllers
}
}
#region Update Shortcut Methods
[HttpPost, ValidateAntiForgeryToken]
public virtual ActionResult UpdateComments(int id, string Comments = null, bool? redirect = null)
{
return Update(id, pComments, Comments, redirect);
}
#endregion
#region Update Properties
private void UpdateComments(DeviceFlagAssignment assignment, string comments)
{
if (!assignment.CanEdit())
throw new InvalidOperationException("Editing comments for device flags is denied");
assignment.OnEdit(comments);
Database.SaveChanges();
}
#endregion
#region Actions
[HttpPost, ValidateAntiForgeryToken]
public virtual ActionResult AddDevice(int id, string deviceSerialNumber, string comments)
public virtual ActionResult AddDevice(int id, string deviceSerialNumber, string comments, DateTime? removeDate)
{
Database.Configuration.LazyLoadingEnabled = true;
var flag = Database.DeviceFlags.Find(id);
if (flag == null)
throw new ArgumentException("Invalid Device Flag Id", nameof(id));
var flag = Database.DeviceFlags.Find(id)
?? throw new ArgumentException("Invalid Device Flag Id", nameof(id));
var device = Database.Devices.Include(u => u.DeviceFlagAssignments).FirstOrDefault(d => d.SerialNumber == deviceSerialNumber);
if (device == null)
throw new ArgumentException("Invalid Device Serial Number", nameof(deviceSerialNumber));
var device = Database.Devices
.Include(u => u.DeviceFlagAssignments)
.FirstOrDefault(d => d.SerialNumber == deviceSerialNumber)
?? throw new ArgumentException("Invalid Device Serial Number", nameof(deviceSerialNumber));
if (!device.CanAddDeviceFlag(flag))
return Unauthorized("Adding device flag is denied");
var assignment = device.OnAddDeviceFlag(Database, flag, comments);
if (removeDate.HasValue && removeDate.Value < DateTime.Today.AddDays(1))
removeDate = null;
if (device.CanRemoveDeviceFlag(flag))
device.OnAddDeviceFlag(Database, flag, comments, removeDate);
else
device.OnAddDeviceFlag(Database, flag, comments);
Database.SaveChanges();
@@ -102,9 +79,8 @@ namespace Disco.Web.Areas.API.Controllers
var assignment = Database.DeviceFlagAssignments
.Include(a => a.DeviceFlag)
.FirstOrDefault(a => a.Id == id);
if (assignment == null)
throw new ArgumentException("Invalid Device Flag Assignment Id", nameof(id));
.FirstOrDefault(a => a.Id == id)
?? throw new ArgumentException("Invalid Device Flag Assignment Id", nameof(id));
if (!assignment.CanRemove())
return Unauthorized("Removing device flag assignment is denied");