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.Utilities.Pagination; using eSuite.Core.Miscellaneous; using System.Linq.Expressions; using e_suite.Modules.WorkflowTemplatesManager.Repository; //using WorkflowVersion = e_suite.Workflow.Core.WorkflowVersion; namespace e_suite.Modules.WorkflowTemplatesManager; public class WorkflowTemplateManager : IWorkflowTemplateManager { private readonly IWorkflowTemplateRepository _workflowTemplateRepository; private readonly IWorkflowConverter _workflowConverter; 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> GetWorkflowTemplatesAsync(Paging paging, CancellationToken cancellationToken) { var ssoProviders = _workflowTemplateRepository.GetWorkflows().Where(x => x.Deleted == false); var paginatedData = await PaginatedData.Paginate(ssoProviders, paging, WorkflowKeySelector, cancellationToken); return paginatedData; } private Expression> WorkflowKeySelector(string sortKey) { return sortKey?.ToLowerInvariant() switch { "id" => x => x.Id, "guid" => x => x.Guid, "name" => x => x.Name, "lastupdated" => x => x.LastUpdated, _ => x => x.Id }; } public async Task> GetWorkflowTemplateVersionsAsync(Paging paging, CancellationToken cancellationToken) { var ssoProviders = _workflowTemplateRepository.GetWorkflowVersions().Where(x => x.Deleted == false); var paginatedData = await PaginatedData.Paginate(ssoProviders, paging, KeySelector, cancellationToken); return paginatedData; } private Expression> 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 GetWorkflowTemplateVersionAsync(GeneralIdRef generalIdRef, CancellationToken cancellationToken) { var dbWorkflowTemplate = await _workflowTemplateRepository.GetWorkflowVersions().FindByGeneralIdRefAsync(generalIdRef, cancellationToken) ?? throw new NotFoundException("Unable to find Workflow Version"); //var workflowTemplate = _workflowConverter.DeserialiseFromDatabase(dbWorkflowTemplate); return new GetWorkflowTemplateVersion(dbWorkflowTemplate); } public async Task EditTemplateVersion( AuditUserDetails auditUserDetails, EditWorkflowTemplateVersion template, CancellationToken cancellationToken ) { await AlterWorkflowTemplateVersionAsync(auditUserDetails, template.ToGeneralIdRef()!, async version => { var domain = await _domainRepository.GetDomainById(template.DomainId!, cancellationToken) ?? 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.Tasks = template.Tasks; }, cancellationToken); } public async Task PatchTemplateVersion( AuditUserDetails auditUserDetails, IGeneralIdRef templateId, PatchWorkflowTemplateVersion patchTemplate, CancellationToken cancellationToken ) { var patch = _patchFactory.Create(patchTemplate); await AlterWorkflowTemplateVersionAsync(auditUserDetails, templateId, async version => { await patch.ApplyTo(version); }, cancellationToken); } public async Task PostTemplateVersion( AuditUserDetails auditUserDetails, CreateWorkflowTemplateVersion template, CancellationToken cancellationToken ) { //var workflowTemplate = _workflowConverter.DeserialiseFromDatabase(dbWorkflowTemplate); throw new NotImplementedException(); } public async Task DeleteTemplateVersion(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 applyChanges, CancellationToken cancellationToken ) { await _workflowTemplateRepository.TransactionAsync(async () => { var workflowVersion = await _workflowTemplateRepository.GetWorkflowVersions().FindByGeneralIdRefAsync(generalIdRef, cancellationToken) ?? throw new NotFoundException("SsoProvider with this id does not exists"); await applyChanges(workflowVersion); await _workflowTemplateRepository.EditWorkflowVersionAsync(auditUserDetails, workflowVersion, cancellationToken); }); } }