feature: batch device decommissioning
This commit is contained in:
@@ -23,6 +23,25 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
public override string FriendlyValue { get { return friendlyValue; } }
|
||||
public override string FriendlyPreviousValue { get { return friendlyPreviousValue; } }
|
||||
|
||||
public static AssignedUserIdImportField CreateUnassigned(Device device)
|
||||
{
|
||||
var field = new AssignedUserIdImportField()
|
||||
{
|
||||
parsedValue = null,
|
||||
friendlyValue = null,
|
||||
};
|
||||
if (device.AssignedUser != null)
|
||||
{
|
||||
field.friendlyPreviousValue = $"{device.AssignedUser.DisplayName} [{device.AssignedUser.UserId}]";
|
||||
field.Success(EntityState.Modified);
|
||||
}
|
||||
else
|
||||
{
|
||||
field.Success(EntityState.Unchanged);
|
||||
}
|
||||
return field;
|
||||
}
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
|
||||
{
|
||||
var value = friendlyValue = DataReader.GetString(ColumnIndex);
|
||||
|
||||
@@ -158,36 +158,48 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
return possibleColumns.Select(h => (int?)h.Index).FirstOrDefault();
|
||||
}
|
||||
|
||||
public static bool CanDecommissionDevice(Device Device, IDeviceImportContext Context, IDeviceImportDataReader DataReader, out string ErrorMessage)
|
||||
public static bool CanDecommissionDevice(Device device, IDeviceImportContext context, IDeviceImportDataReader dataReader, out string errorMessage)
|
||||
{
|
||||
if (Device == null)
|
||||
var isAssigningUser = false;
|
||||
var assigningUserId = default(string);
|
||||
var assignedUserIndex = context.GetColumnByType(DeviceImportFieldTypes.AssignedUserId);
|
||||
if (assignedUserIndex.HasValue)
|
||||
{
|
||||
ErrorMessage = "Cannot decommission new devices";
|
||||
isAssigningUser = true;
|
||||
assigningUserId = dataReader.GetString(assignedUserIndex.Value);
|
||||
}
|
||||
var hasOpenJobs = device.Jobs.Any(j => !j.ClosedDate.HasValue);
|
||||
|
||||
return CanDecommissionDevice(device, isAssigningUser, assigningUserId, hasOpenJobs, out errorMessage);
|
||||
}
|
||||
|
||||
public static bool CanDecommissionDevice(Device device, bool isAssigningUser, string assigningUserId, bool hasOpenJobs, out string errorMessage)
|
||||
{
|
||||
if (device == null)
|
||||
{
|
||||
errorMessage = "Cannot decommission new devices";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check device is assigned (or being removed in this import)
|
||||
|
||||
var assignedUserIndex = Context.GetColumnByType(DeviceImportFieldTypes.AssignedUserId);
|
||||
if ((!assignedUserIndex.HasValue && Device.AssignedUserId != null) ||
|
||||
(assignedUserIndex.HasValue && !string.IsNullOrWhiteSpace(DataReader.GetString(assignedUserIndex.Value))))
|
||||
if (isAssigningUser && !string.IsNullOrEmpty(assigningUserId) ||
|
||||
(!isAssigningUser && device.AssignedUserId != null))
|
||||
{
|
||||
if (Device.AssignedUserId != null)
|
||||
ErrorMessage = $"The device is assigned to a user ({Device.AssignedUser.DisplayName} [{Device.AssignedUser.UserId}]) and cannot be decommissioned";
|
||||
if (!isAssigningUser)
|
||||
errorMessage = $"The device is assigned to a user ({device.AssignedUser.DisplayName} [{device.AssignedUser.UserId}]) and cannot be decommissioned";
|
||||
else
|
||||
ErrorMessage = $"The device is being assigned to a user ({DataReader.GetString(assignedUserIndex.Value)}) and cannot be decommissioned";
|
||||
errorMessage = $"The device is being assigned to a user ({assigningUserId}) and cannot be decommissioned";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check device doesn't have any open jobs
|
||||
var openJobCount = Device.Jobs.Count(j => !j.ClosedDate.HasValue);
|
||||
if (openJobCount > 0)
|
||||
if (hasOpenJobs)
|
||||
{
|
||||
ErrorMessage = $"The device is associated with {openJobCount} open job{(openJobCount == 1 ? null : "s")} and cannot be decommissioned";
|
||||
errorMessage = $"The device is associated with an open job and cannot be decommissioned";
|
||||
return false;
|
||||
}
|
||||
|
||||
ErrorMessage = null;
|
||||
errorMessage = null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,26 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
public override string FriendlyValue { get { return parsedValue.HasValue ? parsedValue.Value.ToString() : rawValue; } }
|
||||
public override string FriendlyPreviousValue { get { return previousValue.HasValue ? previousValue.Value.ToString() : null; } }
|
||||
|
||||
public static DeviceDecommissionedReasonImportField Create(Device device, DecommissionReasons? decommissionReason, bool setDate, bool isUnassigningUser)
|
||||
{
|
||||
var field = new DeviceDecommissionedReasonImportField()
|
||||
{
|
||||
rawValue = decommissionReason?.ToString(),
|
||||
parsedValue = decommissionReason,
|
||||
previousValue = device.DecommissionReason,
|
||||
setDate = setDate
|
||||
};
|
||||
var hasOpenJobs = device.Jobs.Any(j => !j.ClosedDate.HasValue);
|
||||
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(device, isUnassigningUser, null, hasOpenJobs, out var errorMessage))
|
||||
field.Error(errorMessage);
|
||||
else if (device.DecommissionReason == decommissionReason)
|
||||
field.Success(EntityState.Unchanged);
|
||||
else
|
||||
field.Success(EntityState.Modified);
|
||||
|
||||
return field;
|
||||
}
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
|
||||
{
|
||||
var value = DataReader.GetString(ColumnIndex);
|
||||
@@ -50,8 +70,7 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
var decommissionedDateIndex = Context.GetColumnByType(DeviceImportFieldTypes.DeviceDecommissionedDate);
|
||||
if (parsedValue.HasValue && !decommissionedDateIndex.HasValue)
|
||||
{
|
||||
string errorMessage;
|
||||
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(ExistingDevice, Context, DataReader, out errorMessage))
|
||||
if (!DeviceDecommissionedDateImportField.CanDecommissionDevice(ExistingDevice, Context, DataReader, out var errorMessage))
|
||||
return Error(errorMessage);
|
||||
|
||||
setDate = true;
|
||||
|
||||
@@ -19,6 +19,16 @@ namespace Disco.Services.Devices.Importing.Fields
|
||||
public override string FriendlyValue { get { return parsedValue; } }
|
||||
public override string FriendlyPreviousValue { get { return parsedValue; } }
|
||||
|
||||
public static DeviceSerialNumberImportField Create(Device device)
|
||||
{
|
||||
var field = new DeviceSerialNumberImportField()
|
||||
{
|
||||
parsedValue = device.SerialNumber,
|
||||
};
|
||||
field.Success(EntityState.Unchanged);
|
||||
return field;
|
||||
}
|
||||
|
||||
public override bool Parse(DiscoDataContext Database, IDeviceImportCache Cache, IDeviceImportContext Context, string DeviceSerialNumber, Device ExistingDevice, List<IDeviceImportRecord> PreviousRecords, IDeviceImportDataReader DataReader, int ColumnIndex)
|
||||
{
|
||||
var value = DataReader.GetString(ColumnIndex);
|
||||
|
||||
Reference in New Issue
Block a user