From 9a360feb7f17fac5fca7886e4f98c3a35bff5e34 Mon Sep 17 00:00:00 2001 From: Colin Dawson Date: Mon, 2 Mar 2026 23:57:35 +0000 Subject: [PATCH] Editing a template version is now immutable. --- .../Tables/Workflow/WorkflowVersion.cs | 4 +- .../WorkflowTemplateManager.cs | 46 ++++++++++++------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/e-suite.Database.Core/e-suite.Database.Core/Tables/Workflow/WorkflowVersion.cs b/e-suite.Database.Core/e-suite.Database.Core/Tables/Workflow/WorkflowVersion.cs index 2e8f333..f774f42 100644 --- a/e-suite.Database.Core/e-suite.Database.Core/Tables/Workflow/WorkflowVersion.cs +++ b/e-suite.Database.Core/e-suite.Database.Core/Tables/Workflow/WorkflowVersion.cs @@ -19,12 +19,12 @@ public class WorkflowVersion : IGeneralId, ISoftDeletable public long Id { get; set; } [Required] - public Guid Guid { get; set; } + public Guid Guid { get; set; } = Guid.NewGuid(); public long WorkflowId { get; set; } [Required] - public long Version { get; set; } = 0; + public long Version { get; set; } = 1; [Required] public long DomainId { get; set; } diff --git a/e-suite.Modules.WorkflowTemplatesManager/WorkflowTemplateManager.cs b/e-suite.Modules.WorkflowTemplatesManager/WorkflowTemplateManager.cs index a470675..af1895a 100644 --- a/e-suite.Modules.WorkflowTemplatesManager/WorkflowTemplateManager.cs +++ b/e-suite.Modules.WorkflowTemplatesManager/WorkflowTemplateManager.cs @@ -100,14 +100,21 @@ public class WorkflowTemplateManager : IWorkflowTemplateManager { 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+1; - version.Tasks = template.Tasks; - version.Workflow.Name = template.WorkflowName; + var workflow = version.Workflow; + workflow.Name = template.WorkflowName; + + var newVersion = new e_suite.Database.Core.Tables.Workflow.WorkflowVersion + { + Domain = domain, + DomainId = domain.Id, + Description = template.Description, + ActivityNameTemplate = template.ActivityNameTemplate, + Deleted = false, + Version = template.Version + 1, + Tasks = template.Tasks, + Workflow = workflow + }; + return newVersion; }, cancellationToken); } @@ -122,6 +129,7 @@ public class WorkflowTemplateManager : IWorkflowTemplateManager await AlterWorkflowTemplateVersionAsync(auditUserDetails, templateId, async version => { await patch.ApplyTo(version); + return version; }, cancellationToken); } @@ -151,13 +159,11 @@ public class WorkflowTemplateManager : IWorkflowTemplateManager var dbWorkflowTemplate = new WorkflowVersion { - Guid = Guid.NewGuid(), ActivityNameTemplate = template.ActivityNameTemplate, Description = template.Description, Domain = workflowDomain, Tasks = template.Tasks, Workflow = workflow, - Version = 1 }; //This will attempt to parse the data onto the internal workflow structure. @@ -179,23 +185,31 @@ public class WorkflowTemplateManager : IWorkflowTemplateManager await AlterWorkflowTemplateVersionAsync(auditUserDetails, templateId, async version => { version.Deleted = true; + return version; }, cancellationToken); } - + private async Task AlterWorkflowTemplateVersionAsync( AuditUserDetails auditUserDetails, IGeneralIdRef generalIdRef, - Func applyChanges, + 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"); + 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); + var newVersion = await applyChanges(workflowVersion); + if (newVersion != workflowVersion) + await _workflowTemplateRepository.AddWorkflowVersion(auditUserDetails, newVersion, cancellationToken); + else + await _workflowTemplateRepository.EditWorkflowVersionAsync(auditUserDetails, workflowVersion, + cancellationToken); }); }