/// <inheritdoc/>
        public virtual async Task <IOperationResult <Integration.Models.V1WorkflowInstance> > HandleAsync(V1CorrelateWorkflowInstanceCommand command, CancellationToken cancellationToken = default)
        {
            var workflowInstance = await this.WorkflowInstances.FindAsync(command.Id, cancellationToken);

            if (workflowInstance == null)
            {
                throw DomainException.NullReference(typeof(V1WorkflowInstance), command.Id);
            }
            workflowInstance.SetCorrelationContext(command.CorrelationContext);
            workflowInstance = await this.WorkflowInstances.UpdateAsync(workflowInstance, cancellationToken);

            await this.WorkflowInstances.SaveChangesAsync(cancellationToken);

            switch (workflowInstance.Status)
            {
            case V1WorkflowInstanceStatus.Suspended:
                await this.Mediator.ExecuteAndUnwrapAsync(new V1StartWorkflowInstanceCommand(workflowInstance.Id), cancellationToken);

                break;

            case V1WorkflowInstanceStatus.Running:
                var runtimeProxy = this.RuntimeProxyManager.GetProxy(workflowInstance.Id);
                await runtimeProxy.CorrelateAsync(command.CorrelationContext, cancellationToken);

                break;

            default:
                throw DomainException.UnexpectedState(typeof(V1WorkflowInstance), workflowInstance.Id, workflowInstance.Status);
            }
            return(this.Ok(this.Mapper.Map <Integration.Models.V1WorkflowInstance>(workflowInstance)));
        }
 /// <summary>
 /// Completes and sets the <see cref="V1WorkflowActivity"/>'s output
 /// </summary>
 public virtual void SetOutput(object?output)
 {
     if (this.Status >= V1WorkflowActivityStatus.Faulted)
     {
         throw DomainException.UnexpectedState(typeof(V1WorkflowActivity), this.Id, this.Status);
     }
     this.On(this.RegisterEvent(new V1WorkflowActivityCompletedDomainEvent(this.Id, output)));
 }
 /// <summary>
 /// Skips the <see cref="V1WorkflowActivity"/>
 /// </summary>
 public virtual void Skip()
 {
     if (this.Status >= V1WorkflowActivityStatus.Faulted)
     {
         throw DomainException.UnexpectedState(typeof(V1WorkflowActivity), this.Id, this.Status);
     }
     this.On(this.RegisterEvent(new V1WorkflowActivitySkippedDomainEvent(this.Id)));
 }
 /// <summary>
 /// Supsend the <see cref="V1WorkflowActivity"/>
 /// </summary>
 public virtual void Suspend()
 {
     if (this.Status != V1WorkflowActivityStatus.Running)
     {
         throw DomainException.UnexpectedState(typeof(V1WorkflowActivity), this.Id, this.Status);
     }
     this.On(this.RegisterEvent(new V1WorkflowActivitySuspendedDomainEvent(this.Id)));
 }
 /// <summary>
 /// Faults the <see cref="V1WorkflowActivity"/>
 /// </summary>
 /// <param name="error">The unhandled <see cref="Error"/> that caused the <see cref="V1WorkflowActivity"/> to end prematurily</param>
 public virtual void Fault(Neuroglia.Error error)
 {
     if (error == null)
     {
         throw DomainException.ArgumentNull(nameof(error));
     }
     if (this.Status >= V1WorkflowActivityStatus.Faulted)
     {
         throw DomainException.UnexpectedState(typeof(V1WorkflowActivity), this.Id, this.Status);
     }
     this.On(this.RegisterEvent(new V1WorkflowActivityFaultedDomainEvent(this.Id, error)));
 }
 /// <summary>
 /// Faults the <see cref="V1WorkflowActivity"/>
 /// </summary>
 /// <param name="ex">The unhandled <see cref="Exception"/> that caused the <see cref="V1WorkflowActivity"/> to end prematurily</param>
 public virtual void Fault(Exception ex)
 {
     if (ex == null)
     {
         throw DomainException.ArgumentNull(nameof(ex));
     }
     if (this.Status >= V1WorkflowActivityStatus.Faulted)
     {
         throw DomainException.UnexpectedState(typeof(V1WorkflowActivity), this.Id, this.Status);
     }
     this.Fault(new Neuroglia.Error(ex.GetType().Name.Replace("exception", string.Empty, StringComparison.OrdinalIgnoreCase), ex.Message));
 }