-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApproveStepMutation.cs
More file actions
56 lines (49 loc) · 1.77 KB
/
Copy pathApproveStepMutation.cs
File metadata and controls
56 lines (49 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using ModularityKit.Mutator.Abstractions.Changes;
using ModularityKit.Mutator.Abstractions.Context;
using ModularityKit.Mutator.Abstractions.Engine;
using ModularityKit.Mutator.Abstractions.Intent;
using ModularityKit.Mutator.Abstractions.Results;
using WorkflowApprovals.State;
namespace WorkflowApprovals.Mutations;
/// <summary>
/// Mutation that approves specific step in an <see cref="ApprovalWorkflowState"/>.
/// </summary>
internal sealed class ApproveStepMutation(
int stepIndex,
string approver,
MutationContext context
) : MutationBase<ApprovalWorkflowState>(
CreateIntent(
operationName: "ApproveStep",
category: "Workflow",
description: "Approve a workflow step",
riskLevel: MutationRiskLevel.High),
context)
{
public int StepIndex { get; } = stepIndex;
public string Approver { get; } = approver;
public override ValidationResult Validate(ApprovalWorkflowState state)
{
var result = new ValidationResult();
if (StepIndex < 0 || StepIndex >= state.Steps.Count)
result.AddError("StepIndex", "Invalid step index");
if (string.IsNullOrEmpty(Approver))
result.AddError("Approver", "Approver cannot be empty");
return result;
}
public override MutationResult<ApprovalWorkflowState> Apply(ApprovalWorkflowState state)
{
var steps = state.Steps.ToList();
var oldStep = steps[StepIndex];
var newStep = oldStep with
{
Status = StepStatus.Approved,
ApprovedBy = Approver
};
steps[StepIndex] = newStep;
var newState = state with { Steps = steps };
return Success(
newState,
StateChange.Modified($"Steps[{StepIndex}]", oldStep.Status, newStep.Status));
}
}