Backend/e-suite.Modules.WorkflowTemplatesManager/WorkflowTemplateManager.cs

127 lines
5.0 KiB
C#

using e_suite.API.Common;
using e_suite.API.Common.exceptions;
using e_suite.API.Common.repository;
using e_suite.Database.Audit;
using e_suite.Database.Core.Extensions;
using e_suite.Database.Core.Tables.Workflow;
using e_suite.Utilities.Pagination;
using eSuite.Core.Miscellaneous;
using System.Linq.Expressions;
namespace e_suite.Modules.WorkflowTemplatesManager;
public class WorkflowTemplateManager : IWorkflowTemplateManager
{
private readonly IWorkflowTemplateRepository _workflowTemplateRepository;
private readonly IDomainRepository _domainRepository;
private readonly IPatchFactory _patchFactory;
public WorkflowTemplateManager(IWorkflowTemplateRepository workflowTemplateRepository, IPatchFactory patchFactory, IDomainRepository domainRepository)
{
_workflowTemplateRepository = workflowTemplateRepository;
_patchFactory = patchFactory;
_domainRepository = domainRepository;
}
public async Task<PaginatedData<GetWorkflowTemplate>> GetWorkflowTemplatesAsync(Paging paging, CancellationToken cancellationToken)
{
var ssoProviders = _workflowTemplateRepository.GetWorkflowVersions().Where(x => x.Deleted == false);
var paginatedData = await PaginatedData.Paginate<WorkflowVersion, GetWorkflowTemplate>(ssoProviders, paging,
KeySelector, cancellationToken);
return paginatedData;
}
private Expression<Func<WorkflowVersion, object>> KeySelector(string sortKey)
{
return sortKey?.ToLowerInvariant() switch
{
"id" => x => x.Id,
"activitynametemplate" => x => x.ActivityNameTemplate,
"description" => x => x.Description,
"domain.name" => x => x.Domain.Name,
"workflow.name" => x => x.Workflow.Name,
_ => x => x.Id
};
}
public async Task<GetWorkflowTemplate?> GetWorkflowTemplateAsync(GeneralIdRef generalIdRef, CancellationToken cancellationToken)
{
var workflowTemplate = await _workflowTemplateRepository.GetWorkflowVersions().FindByGeneralIdRefAsync(generalIdRef, cancellationToken)
?? throw new NotFoundException("Unable to find Workflow Version");
return new GetWorkflowTemplate(workflowTemplate);
}
public async Task EditTemplate(
AuditUserDetails auditUserDetails,
EditWorkflowTemplate template,
CancellationToken cancellationToken
)
{
await AlterWorkflowTemplateVersionAsync(auditUserDetails, template.ToGeneralIdRef()!, async version =>
{
var domain = await _domainRepository.GetDomainById(template.DomainId, cancellationToken);
if (domain is null)
throw new NotFoundException("Unable to find Domain with provided id");
version.Domain = domain;
version.DomainId = domain.Id;
version.Description = template.Description;
version.ActivityNameTemplate = template.ActivityNameTemplate;
version.Deleted = false;
version.Version = template.Version;
//version.WorkflowId
}, cancellationToken);
}
public async Task PatchTemplate(
AuditUserDetails auditUserDetails,
IGeneralIdRef templateId,
PatchWorkflowTemplate patchTemplate,
CancellationToken cancellationToken
)
{
var patch = _patchFactory.Create(patchTemplate);
await AlterWorkflowTemplateVersionAsync(auditUserDetails, templateId, async version =>
{
patch.ApplyTo(version);
}, cancellationToken);
}
public async Task PostTemplate(
AuditUserDetails auditUserDetails,
CreateWorkflowTemplate template,
CancellationToken cancellationToken
)
{
throw new NotImplementedException();
}
public async Task DeleteTemplate(AuditUserDetails auditUserDetails, IGeneralIdRef templateId, CancellationToken cancellationToken)
{
await AlterWorkflowTemplateVersionAsync(auditUserDetails, templateId, async version =>
{
version.Deleted = true;
}, cancellationToken);
}
private async Task AlterWorkflowTemplateVersionAsync(
AuditUserDetails auditUserDetails,
IGeneralIdRef generalIdRef,
Func<WorkflowVersion, Task> applyChanges,
CancellationToken cancellationToken
)
{
await _workflowTemplateRepository.TransactionAsync(async () =>
{
var workflowVersion = await _workflowTemplateRepository.GetWorkflowVersions().FindByGeneralIdRefAsync(generalIdRef, cancellationToken);
if (workflowVersion is null)
throw new NotFoundException("SsoProvider with this id does not exists");
await applyChanges(workflowVersion);
await _workflowTemplateRepository.EditWorkflowVersionAsync(auditUserDetails, workflowVersion, cancellationToken);
});
}
}