bulk generation refactoring
This commit is contained in:
@@ -733,6 +733,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
results.Add(new BulkGenerateUserModel()
|
||||
{
|
||||
Id = user.UserId,
|
||||
UserEmailAddress = user.EmailAddress,
|
||||
DisplayName = user.DisplayName,
|
||||
Scope = $"Matched '{dataId}'",
|
||||
IsError = false,
|
||||
@@ -763,6 +764,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
Id = adUser.Id,
|
||||
DisplayName = adUser.DisplayName,
|
||||
UserEmailAddress = adUser.Email,
|
||||
Scope = $"Group Member '{group.Name}'",
|
||||
IsError = false,
|
||||
});
|
||||
@@ -816,6 +818,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
Id = adUser.Id,
|
||||
DisplayName = adUser.DisplayName,
|
||||
UserEmailAddress = adUser.Email,
|
||||
Scope = $"Group Member '{group.Name}'",
|
||||
IsError = false,
|
||||
});
|
||||
@@ -827,6 +830,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
Id = user.Id,
|
||||
DisplayName = user.DisplayName,
|
||||
UserEmailAddress = user.Email,
|
||||
Scope = $"Matched '{groupId}'",
|
||||
IsError = false,
|
||||
});
|
||||
@@ -887,6 +891,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
results.Add(new BulkGenerateUserModel()
|
||||
{
|
||||
Id = assignment.UserId,
|
||||
UserEmailAddress = assignment.User.EmailAddress,
|
||||
DisplayName = assignment.User.DisplayName,
|
||||
Scope = $"Assigned User Flag '{flag.Name}'",
|
||||
IsError = false,
|
||||
@@ -941,6 +946,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
Id = assignment.AssignedUserId,
|
||||
DisplayName = assignment.AssignedUser.DisplayName,
|
||||
UserEmailAddress = assignment.AssignedUser.EmailAddress,
|
||||
Scope = $"Device Profile '{profile.Name}' Matches Assigned Device '{assignment.SerialNumber}'",
|
||||
IsError = false,
|
||||
});
|
||||
@@ -994,6 +1000,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
Id = assignment.AssignedUserId,
|
||||
DisplayName = assignment.AssignedUser.DisplayName,
|
||||
UserEmailAddress = assignment.AssignedUser.EmailAddress,
|
||||
Scope = $"Device Batch '{batch.Name}' Matches Assigned Device '{assignment.SerialNumber}'",
|
||||
IsError = false,
|
||||
});
|
||||
@@ -1040,6 +1047,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
Id = assignment.Device.AssignedUserId,
|
||||
DisplayName = assignment.Device.AssignedUser.DisplayName,
|
||||
UserEmailAddress = assignment.Device.AssignedUser.EmailAddress,
|
||||
Scope = $"Document Template '{template.Id}' Attachment Matches Assigned Device '{assignment.Device.SerialNumber}'",
|
||||
IsError = false,
|
||||
});
|
||||
@@ -1056,6 +1064,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
Id = assignment.Job.UserId,
|
||||
DisplayName = assignment.Job.User.DisplayName,
|
||||
UserEmailAddress = assignment.Job.User.EmailAddress,
|
||||
Scope = $"Document Template '{template.Id}' Attachment Matches Job '{assignment.Job.Id}'",
|
||||
IsError = false,
|
||||
});
|
||||
@@ -1072,6 +1081,7 @@ namespace Disco.Web.Areas.API.Controllers
|
||||
{
|
||||
Id = assignment.UserId,
|
||||
DisplayName = assignment.User.DisplayName,
|
||||
UserEmailAddress = assignment.User.EmailAddress,
|
||||
Scope = $"Document Template '{template.Id}' Attachment Matches User",
|
||||
IsError = false,
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
public class BulkGenerateUserModel
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string UserEmailAddress { get; set; }
|
||||
public string DisplayName { get; set; }
|
||||
public string Scope { get; set; }
|
||||
public bool IsError { get; set; }
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Disco.BI.Extensions;
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Models.UI.Config.DocumentTemplate;
|
||||
using Disco.Services;
|
||||
@@ -9,10 +10,8 @@ using Disco.Services.Expressions;
|
||||
using Disco.Services.Plugins.Features.UIExtension;
|
||||
using Disco.Services.Web;
|
||||
using Disco.Web.Areas.Config.Models.DocumentTemplate;
|
||||
using Disco.Web.Areas.Config.Views.DocumentTemplate;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Eventing.Reader;
|
||||
using System.Linq;
|
||||
using System.Web.Mvc;
|
||||
|
||||
@@ -213,59 +212,53 @@ namespace Disco.Web.Areas.Config.Controllers
|
||||
return View(model);
|
||||
}
|
||||
|
||||
[DiscoAuthorizeAll(Claims.Config.DocumentTemplate.BulkGenerate, Claims.User.Actions.GenerateDocuments)]
|
||||
public virtual ActionResult BulkGenerate(string id)
|
||||
public static ConfigDocumentTemplateBulkGenerate BuildBulkGenerateModel(DocumentTemplate documentTemplate, DiscoDataContext database, AuthorizationToken authorization)
|
||||
{
|
||||
var m = new BulkGenerateModel()
|
||||
var model = new BulkGenerateModel()
|
||||
{
|
||||
DocumentTemplate = Database.DocumentTemplates.FirstOrDefault(at => at.Id == id),
|
||||
DocumentTemplate = documentTemplate,
|
||||
};
|
||||
if (m.DocumentTemplate == null)
|
||||
throw new ArgumentException("Invalid Document Template Id", nameof(id));
|
||||
|
||||
if (m.DocumentTemplate.Scope != DocumentTemplate.DocumentTemplateScopes.User)
|
||||
throw new NotSupportedException("Only user-scoped document templates can be bulk generated using this method");
|
||||
|
||||
m.TemplatePageCount = m.DocumentTemplate.PdfPageHasAttachmentId(Database).Count;
|
||||
m.UserFlags = Database.UserFlags.Select(f => new BulkGenerateModel.ItemWithCount<UserFlag>()
|
||||
model.TemplatePageCount = model.DocumentTemplate.PdfPageHasAttachmentId(database).Count;
|
||||
model.UserFlags = database.UserFlags.Select(f => new ItemWithCount<UserFlag>()
|
||||
{
|
||||
Item = f,
|
||||
Count = f.UserFlagAssignments.Where(a => a.RemovedDate == null).Count(),
|
||||
}).ToList();
|
||||
m.DeviceProfiles = Database.DeviceProfiles.Select(p => new BulkGenerateModel.ItemWithCount<DeviceProfile>()
|
||||
model.DeviceProfiles = database.DeviceProfiles.Select(p => new ItemWithCount<DeviceProfile>()
|
||||
{
|
||||
Item = p,
|
||||
Count = p.Devices.Where(d => d.AssignedUserId != null).Count(),
|
||||
}).ToList();
|
||||
m.DeviceBatches = Database.DeviceBatches.Select(p => new BulkGenerateModel.ItemWithCount<DeviceBatch>()
|
||||
model.DeviceBatches = database.DeviceBatches.Select(p => new ItemWithCount<DeviceBatch>()
|
||||
{
|
||||
Item = p,
|
||||
Count = p.Devices.Where(d => d.AssignedUserId != null).Count(),
|
||||
}).ToList();
|
||||
m.DocumentTemplates = Database.DocumentTemplates.Select(dt => new BulkGenerateModel.ItemWithCount<DocumentTemplate>()
|
||||
model.DocumentTemplates = database.DocumentTemplates.Select(dt => new ItemWithCount<DocumentTemplate>()
|
||||
{
|
||||
Item = dt,
|
||||
}).ToList();
|
||||
foreach (var record in m.DocumentTemplates)
|
||||
foreach (var record in model.DocumentTemplates)
|
||||
{
|
||||
switch (record.Item.AttachmentType)
|
||||
{
|
||||
case AttachmentTypes.Device:
|
||||
record.Count = Database.DeviceAttachments.Where(a => a.DocumentTemplateId == record.Item.Id).Select(a => a.Device.AssignedUser).Distinct().Count();
|
||||
record.Count = database.DeviceAttachments.Where(a => a.DocumentTemplateId == record.Item.Id).Select(a => a.Device.AssignedUser).Distinct().Count();
|
||||
break;
|
||||
case AttachmentTypes.Job:
|
||||
record.Count = Database.JobAttachments.Where(a => a.DocumentTemplateId == record.Item.Id).Select(a => a.Job.User).Distinct().Count();
|
||||
record.Count = database.JobAttachments.Where(a => a.DocumentTemplateId == record.Item.Id).Select(a => a.Job.User).Distinct().Count();
|
||||
break;
|
||||
case AttachmentTypes.User:
|
||||
record.Count = Database.UserAttachments.Where(a => a.DocumentTemplateId == record.Item.Id).Select(a => a.User).Distinct().Count();
|
||||
record.Count = database.UserAttachments.Where(a => a.DocumentTemplateId == record.Item.Id).Select(a => a.User).Distinct().Count();
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
if (Authorization.Has(Claims.User.ShowDetails))
|
||||
if (authorization.Has(Claims.User.ShowDetails))
|
||||
{
|
||||
m.UserDetails = Database.UserDetails.Where(d => d.Scope == "Details").GroupBy(d => d.Key).Select(g => new BulkGenerateModel.ItemWithCount<string>()
|
||||
model.UserDetails = database.UserDetails.Where(d => d.Scope == "Details").GroupBy(d => d.Key).Select(g => new ItemWithCount<string>()
|
||||
{
|
||||
Item = g.Key,
|
||||
Count = g.Count(),
|
||||
@@ -273,13 +266,30 @@ namespace Disco.Web.Areas.Config.Controllers
|
||||
}
|
||||
else
|
||||
{
|
||||
m.UserDetails = new List<BulkGenerateModel.ItemWithCount<string>>();
|
||||
model.UserDetails = new List<ItemWithCount<string>>();
|
||||
}
|
||||
|
||||
// UI Extensions
|
||||
UIExtensions.ExecuteExtensions<ConfigDocumentTemplateBulkGenerate>(ControllerContext, m);
|
||||
return model;
|
||||
}
|
||||
|
||||
return View(MVC.Config.DocumentTemplate.Views.BulkGenerate, m);
|
||||
[DiscoAuthorizeAll(Claims.Config.DocumentTemplate.BulkGenerate, Claims.User.Actions.GenerateDocuments)]
|
||||
public virtual ActionResult BulkGenerate(string id)
|
||||
{
|
||||
var documentTemplate = Database.DocumentTemplates.FirstOrDefault(at => at.Id == id);
|
||||
|
||||
if (documentTemplate == null)
|
||||
throw new ArgumentException("Invalid Document Template Id", nameof(id));
|
||||
|
||||
if (documentTemplate.Scope != DocumentTemplate.DocumentTemplateScopes.User)
|
||||
throw new NotSupportedException("Only user-scoped document templates can be bulk generated using this method");
|
||||
|
||||
|
||||
var model = BuildBulkGenerateModel(documentTemplate, Database, Authorization);
|
||||
|
||||
// UI Extensions
|
||||
UIExtensions.ExecuteExtensions(ControllerContext, model);
|
||||
|
||||
return View(MVC.Config.DocumentTemplate.Views.BulkGenerate, model);
|
||||
}
|
||||
|
||||
[DiscoAuthorize(Claims.Config.Show)]
|
||||
|
||||
@@ -12,11 +12,5 @@ namespace Disco.Web.Areas.Config.Models.DocumentTemplate
|
||||
public List<ItemWithCount<Disco.Models.Repository.DeviceBatch>> DeviceBatches { get; set; }
|
||||
public List<ItemWithCount<Disco.Models.Repository.DocumentTemplate>> DocumentTemplates { get; set; }
|
||||
public List<ItemWithCount<string>> UserDetails { get; set; }
|
||||
|
||||
public class ItemWithCount<T>
|
||||
{
|
||||
public T Item { get; set; }
|
||||
public int Count { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user