Backend/e-suite.Workflow.Core/Extensions/ActivityTaskOutcomeExtensions.cs

50 lines
1.7 KiB
C#

using e_suite.Database.Core.Tables.Activity;
using eSuite.Core.Enums;
namespace e_suite.Workflow.Core.Extensions;
public static class ActivityTaskOutcomeExtensions
{
extension(ActivityTask task)
{
public void SetState(ActivityState newState)
{
if (newState == ActivityState.Active)
{
if (task.StartDateTime == null)
throw new InvalidOperationException(
$"Cannot set state to Active because StartDateTime is not set for task {task.Id}."
);
}
if (newState == ActivityState.ReadyToComplete)
{
if (task.Outcomes == null || task.Outcomes.Count == 0)
throw new InvalidOperationException(
$"Cannot set state to ReadyToComplete because no outcomes have been recorded for task {task.Id}."
);
}
if (newState == ActivityState.Completed)
{
if (task.FinishDateTime == null)
throw new InvalidOperationException(
$"Cannot set state to Completed because FinishDateTime is not set for task {task.Id}."
);
}
task.ActivityState = newState;
}
public void AddOutcome<T>(T outcome)
{
if (outcome == null) throw new ArgumentNullException(nameof(outcome));
// Explicit, predictable conversion
var newOutcome = outcome.ToString()!.Trim();
if (!task.Outcomes.Contains(newOutcome))
task.Outcomes.Add(newOutcome);
}
}
}