Skip to content

Commit

Permalink
feature: bulk generate documents for device batches, models and profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
garysharp committed May 4, 2023
1 parent 473b02f commit 0a4a281
Show file tree
Hide file tree
Showing 29 changed files with 1,228 additions and 473 deletions.
1 change: 1 addition & 0 deletions Disco.Models/Disco.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<Compile Include="UI\Config\JobQueue\ConfigJobQueueIndexModel.cs" />
<Compile Include="UI\Config\JobQueue\ConfigJobQueueShowModel.cs" />
<Compile Include="UI\Config\Logging\ConfigLoggingIndexModel.cs" />
<Compile Include="UI\Config\Shared\ConfigSharedDeviceGroupDocumentTemplateBulkGenerate.cs" />
<Compile Include="UI\Config\Shared\ConfigSharedTaskStatusModel.cs" />
<Compile Include="UI\Config\Organisation\ConfigOrganisationIndexModel.cs" />
<Compile Include="UI\Config\UserFlag\ConfigUserFlagCreateModel.cs" />
Expand Down
13 changes: 5 additions & 8 deletions Disco.Models/UI/Config/DeviceBatch/ConfigDeviceBatchShowModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
using System;
using Disco.Models.UI.Config.Shared;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Disco.Models.UI.Config.DeviceBatch
{
public interface ConfigDeviceBatchShowModel : BaseUIModel
public interface ConfigDeviceBatchShowModel : BaseUIModel, ConfigSharedDeviceGroupDocumentTemplateBulkGenerate
{
Disco.Models.Repository.DeviceBatch DeviceBatch { get; set; }
Repository.DeviceBatch DeviceBatch { get; set; }

Disco.Models.Repository.DeviceModel DefaultDeviceModel { get; set; }
Repository.DeviceModel DefaultDeviceModel { get; set; }

List<Disco.Models.Repository.DeviceModel> DeviceModels { get; set; }
List<Repository.DeviceModel> DeviceModels { get; set; }

List<ConfigDeviceBatchShowModelMembership> DeviceModelMembers { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Disco.Models.UI.Config.Shared;

namespace Disco.Models.UI.Config.DeviceModel
{
public interface ConfigDeviceModelShowModel : BaseUIModel
public interface ConfigDeviceModelShowModel : BaseUIModel, ConfigSharedDeviceGroupDocumentTemplateBulkGenerate
{
Disco.Models.Repository.DeviceModel DeviceModel { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Disco.Models.BI.Config;
using Disco.Models.UI.Config.Shared;
using System.Collections.Generic;

namespace Disco.Models.UI.Config.DeviceProfile
{
public interface ConfigDeviceProfileShowModel : BaseUIModel
public interface ConfigDeviceProfileShowModel : BaseUIModel, ConfigSharedDeviceGroupDocumentTemplateBulkGenerate
{
Repository.DeviceProfile DeviceProfile { get; set; }
OrganisationAddress DefaultOrganisationAddress { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Disco.Models.UI.Config.Shared
{
public interface ConfigSharedDeviceGroupDocumentTemplateBulkGenerate : BaseUIModel
{
List<Repository.DocumentTemplate> BulkGenerateDocumentTemplates { get; set; }
int DeviceGroupId { get; }
}
}
8 changes: 1 addition & 7 deletions Disco.Models/UI/Config/Shared/ConfigSharedTaskStatusModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Disco.Models.UI.Config.Shared
namespace Disco.Models.UI.Config.Shared
{
public interface ConfigSharedTaskStatusModel : BaseUIModel
{
Expand Down
99 changes: 99 additions & 0 deletions Disco.Web/Areas/API/Controllers/DocumentTemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,105 @@ public virtual ActionResult ImporterUndetectedDelete(string id)
}
}

[DiscoAuthorizeAll(Claims.Config.DeviceModel.Show, Claims.Config.DocumentTemplate.BulkGenerate)]
public virtual ActionResult BulkGenerateDeviceModel(string id, int deviceGroupId)
{
var template = Database.DocumentTemplates.FirstOrDefault(t => t.Id == id);
if (template == null)
return HttpNotFound("Document Template not found");

var deviceModel = Database.DeviceModels.FirstOrDefault(m => m.Id == deviceGroupId);
if (deviceModel is null)
return HttpNotFound("Device Model not found");

List<string> dataIds;
switch (template.AttachmentType)
{
case AttachmentTypes.Device:
dataIds = Database.Devices.Where(d => d.DeviceModelId == deviceModel.Id && d.DecommissionedDate == null).Select(d => d.SerialNumber).ToList();
break;
case AttachmentTypes.Job:
dataIds = Database.Jobs.Where(j => j.ClosedDate == null && j.Device.DeviceModelId == deviceModel.Id).Select(j => j.Id).AsEnumerable().Select(j => j.ToString()).ToList();
break;
case AttachmentTypes.User:
dataIds = Database.Users.Where(u => u.DeviceUserAssignments.Any(a => a.UnassignedDate == null && a.Device.DeviceModelId == deviceModel.Id)).Select(u => u.UserId).ToList();
break;
default:
throw new NotSupportedException("The template type is not supported");
}

if (!dataIds.Any())
return HttpNotFound($"No {template.AttachmentType} targets in scope");

return BulkGenerate(template.Id, string.Join(Environment.NewLine, dataIds));
}

[DiscoAuthorizeAll(Claims.Config.DeviceProfile.Show, Claims.Config.DocumentTemplate.BulkGenerate)]
public virtual ActionResult BulkGenerateDeviceProfile(string id, int deviceGroupId)
{
var template = Database.DocumentTemplates.FirstOrDefault(t => t.Id == id);
if (template == null)
return HttpNotFound("Document Template not found");

var deviceProfile = Database.DeviceProfiles.FirstOrDefault(m => m.Id == deviceGroupId);
if (deviceProfile is null)
return HttpNotFound("Device Profile not found");

List<string> dataIds;
switch (template.AttachmentType)
{
case AttachmentTypes.Device:
dataIds = Database.Devices.Where(d => d.DeviceProfileId == deviceProfile.Id && d.DecommissionedDate == null).Select(d => d.SerialNumber).ToList();
break;
case AttachmentTypes.Job:
dataIds = Database.Jobs.Where(j => j.ClosedDate == null && j.Device.DeviceProfileId == deviceProfile.Id).Select(j => j.Id).AsEnumerable().Select(j => j.ToString()).ToList();
break;
case AttachmentTypes.User:
dataIds = Database.Users.Where(u => u.DeviceUserAssignments.Any(a => a.UnassignedDate == null && a.Device.DeviceProfileId == deviceProfile.Id)).Select(u => u.UserId).ToList();
break;
default:
throw new NotSupportedException("The template type is not supported");
}

if (!dataIds.Any())
return HttpNotFound($"No {template.AttachmentType} targets in scope");

return BulkGenerate(template.Id, string.Join(Environment.NewLine, dataIds));
}

[DiscoAuthorizeAll(Claims.Config.DeviceBatch.Show, Claims.Config.DocumentTemplate.BulkGenerate)]
public virtual ActionResult BulkGenerateDeviceBatch(string id, int deviceGroupId)
{
var template = Database.DocumentTemplates.FirstOrDefault(t => t.Id == id);
if (template == null)
return HttpNotFound("Document Template not found");

var deviceBatch = Database.DeviceBatches.FirstOrDefault(m => m.Id == deviceGroupId);
if (deviceBatch is null)
return HttpNotFound("Device Batch not found");

List<string> dataIds;
switch (template.AttachmentType)
{
case AttachmentTypes.Device:
dataIds = Database.Devices.Where(d => d.DeviceBatchId == deviceBatch.Id && d.DecommissionedDate == null).Select(d => d.SerialNumber).ToList();
break;
case AttachmentTypes.Job:
dataIds = Database.Jobs.Where(j => j.ClosedDate == null && j.Device.DeviceBatchId == deviceBatch.Id).Select(j => j.Id).AsEnumerable().Select(j => j.ToString()).ToList();
break;
case AttachmentTypes.User:
dataIds = Database.Users.Where(u => u.DeviceUserAssignments.Any(a => a.UnassignedDate == null && a.Device.DeviceBatchId == deviceBatch.Id)).Select(u => u.UserId).ToList();
break;
default:
throw new NotSupportedException("The template type is not supported");
}

if (!dataIds.Any())
return HttpNotFound($"No {template.AttachmentType} targets in scope");

return BulkGenerate(template.Id, string.Join(Environment.NewLine, dataIds));
}

[DiscoAuthorize(Claims.Config.DocumentTemplate.BulkGenerate)]
public virtual ActionResult BulkGenerate(string id, string DataIds = null, bool InsertBlankPage = false)
{
Expand Down
3 changes: 3 additions & 0 deletions Disco.Web/Areas/Config/Controllers/DeviceBatchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public virtual ActionResult Index(int? id)
m.DefaultDeviceModel = m.DeviceBatch.DefaultDeviceModelId.HasValue ? Database.DeviceModels.Find(m.DeviceBatch.DefaultDeviceModelId.Value) : null;
}

if (m.DeviceModelMembers.Any(g => g.DeviceCount - g.DeviceDecommissionedCount > 0))
m.BulkGenerateDocumentTemplates = Database.DocumentTemplates.Where(t => !t.IsHidden).ToList();

// UI Extensions
UIExtensions.ExecuteExtensions<ConfigDeviceBatchShowModel>(this.ControllerContext, m);

Expand Down
3 changes: 3 additions & 0 deletions Disco.Web/Areas/Config/Controllers/DeviceModelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public virtual ActionResult Index(int? id)

m.CanDelete = m.DeviceModel.CanDelete(Database);

if (m.DeviceCount - m.DeviceDecommissionedCount > 0)
m.BulkGenerateDocumentTemplates = Database.DocumentTemplates.Where(t => !t.IsHidden).ToList();

// UI Extensions
UIExtensions.ExecuteExtensions<ConfigDeviceModelShowModel>(this.ControllerContext, m);

Expand Down
3 changes: 3 additions & 0 deletions Disco.Web/Areas/Config/Controllers/DeviceProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public virtual ActionResult Index(int? id)
}
m.CanDelete = m.DeviceProfile.CanDelete(Database);

if (m.DeviceCount - m.DeviceDecommissionedCount > 0)
m.BulkGenerateDocumentTemplates = Database.DocumentTemplates.Where(t => !t.IsHidden).ToList();

// UI Extensions
UIExtensions.ExecuteExtensions<ConfigDeviceProfileShowModel>(this.ControllerContext, m);

Expand Down
5 changes: 4 additions & 1 deletion Disco.Web/Areas/Config/Models/DeviceBatch/ShowModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Disco.Models.UI.Config.DeviceBatch;
using Disco.Services.Devices.ManagedGroups;
using Disco.Web.Areas.Config.Models.Shared;
using System.Collections.Generic;

namespace Disco.Web.Areas.Config.Models.DeviceBatch
{
public class ShowModel : ConfigDeviceBatchShowModel
public class ShowModel : DeviceGroupDocumentTemplateBulkGenerateModel, ConfigDeviceBatchShowModel
{
public Disco.Models.Repository.DeviceBatch DeviceBatch { get; set; }

Expand All @@ -19,5 +20,7 @@ public class ShowModel : ConfigDeviceBatchShowModel
public int DeviceCount { get; set; }
public int DeviceDecommissionedCount { get; set; }
public bool CanDelete { get; set; }

public override int DeviceGroupId => DeviceBatch.Id;
}
}
12 changes: 6 additions & 6 deletions Disco.Web/Areas/Config/Models/DeviceModel/ShowModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Disco.Models.UI.Config.DeviceModel;
using Disco.Services.Plugins;
using Disco.Models.UI.Config.DeviceModel;
using Disco.Web.Areas.Config.Models.Shared;
using System.Collections.Generic;

namespace Disco.Web.Areas.Config.Models.DeviceModel
{
public class ShowModel : ConfigDeviceModelShowModel
public class ShowModel : DeviceGroupDocumentTemplateBulkGenerateModel, ConfigDeviceModelShowModel
{
public Disco.Models.Repository.DeviceModel DeviceModel { get; set; }

Expand All @@ -20,5 +18,7 @@ public class ShowModel : ConfigDeviceModelShowModel
public int DeviceDecommissionedCount { get; set; }

public bool CanDelete { get; set; }

public override int DeviceGroupId => DeviceModel.Id;
}
}
5 changes: 4 additions & 1 deletion Disco.Web/Areas/Config/Models/DeviceProfile/ShowModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
using Disco.Services.Devices.ManagedGroups;
using Disco.Services.Interop.ActiveDirectory;
using Disco.Services.Plugins;
using Disco.Web.Areas.Config.Models.Shared;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Disco.Web.Areas.Config.Models.DeviceProfile
{
public class ShowModel : ConfigDeviceProfileShowModel
public class ShowModel : DeviceGroupDocumentTemplateBulkGenerateModel, ConfigDeviceProfileShowModel
{
public Disco.Models.Repository.DeviceProfile DeviceProfile { get; set; }
public List<SelectListItem> DeviceProfileDistributionTypes { get; set; }
Expand Down Expand Up @@ -42,5 +43,7 @@ public string FriendlyOrganisationalUnitName
public int DeviceDecommissionedCount { get; set; }

public bool CanDelete { get; set; }

public override int DeviceGroupId => DeviceProfile.Id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Disco.Models.UI.Config.Shared;
using System.Collections.Generic;

namespace Disco.Web.Areas.Config.Models.Shared
{
public abstract class DeviceGroupDocumentTemplateBulkGenerateModel : ConfigSharedDeviceGroupDocumentTemplateBulkGenerate
{
public List<Disco.Models.Repository.DocumentTemplate> BulkGenerateDocumentTemplates { get; set; }

public abstract int DeviceGroupId { get; }
}
}
1 change: 1 addition & 0 deletions Disco.Web/Areas/Config/Views/DeviceBatch/Show.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@
</tr>
</table>
</div>
@Html.Partial(MVC.Config.Shared.Views._DeviceGroupDocumentBulkGenerate, Model);
<div class="actionBar">
@if (Model.CanDelete)
{
Expand Down
Loading

0 comments on commit 0a4a281

Please sign in to comment.