using System.Dynamic; using System.Net; using System.Net.Mime; using Elsa.Expressions.Models; using Elsa.Extensions; using Elsa.Http; using Elsa.Http.Models; using Elsa.Workflows; using Elsa.Workflows.Activities; using Elsa.Workflows.Contracts; using Elsa.Workflows.Runtime.Activities; namespace Elsa.Samples.AspNet.DocumentApproval { /// /// A workflow that gives Jack and Lucy a chance to review a submitted document. /// public class DocumentApprovalWorkflow : WorkflowBase { protected override void Build(IWorkflowBuilder builder) { var documentVariable = builder.WithVariable(); var approvedVariable = builder.WithVariable(); builder.Root = new Sequence { Activities = { new HttpEndpoint { Path = new("/documents"), SupportedMethods = new(new[] { HttpMethods.Post }), ParsedContent = new(documentVariable), CanStartWorkflow = true }, new WriteLine(context => $"Document received from {documentVariable.Get(context)!.Author.Name}."), new WriteHttpResponse { Content = new("

Request for Approval Sent

Your document has been received and will be reviewed shortly.

"), ContentType = new(MediaTypeNames.Text.Html), StatusCode = new(HttpStatusCode.OK), ResponseHeaders = new(new HttpHeaders { ["X-Powered-By"] = new[] { "Elsa 3.0" } }) }, new Fork { JoinMode = ForkJoinMode.WaitAll, Branches = { // Jack new Sequence { Activities = { new WriteLine(context => $"Jack approve url: \n {GenerateSignalUrl(context, "Approve:Jack")}"), new WriteLine(context => $"Jack reject url: \n {GenerateSignalUrl(context, "Reject:Jack")}"), new Fork { JoinMode = ForkJoinMode.WaitAny, Branches = { // Approve new Sequence { Activities = { new Event("Approve:Jack"), new SetVariable { Variable = approvedVariable, Value = new(true) }, new WriteHttpResponse { Content = new("Thanks for the approval, Jack!"), } } }, // Reject new Sequence { Activities = { new Event("Reject:Jack"), new SetVariable { Variable = approvedVariable, Value = new(false) }, new WriteHttpResponse { Content = new("Sorry to hear that, Jack!"), } } } } } } }, // Lucy new Sequence { Activities = { new WriteLine(context => $"Lucy approve url: \n {GenerateSignalUrl(context, "Approve:Lucy")}"), new WriteLine(context => $"Lucy reject url: \n {GenerateSignalUrl(context, "Reject:Lucy")}"), new Fork { JoinMode = ForkJoinMode.WaitAny, Branches = { // Approve new Sequence { Activities = { new Event("Approve:Lucy"), new SetVariable { Variable = approvedVariable, Value = new(true) }, new WriteHttpResponse { Content = new("Thanks for the approval, Lucy!"), } } }, // Reject new Sequence { Activities = { new Event("Reject:Lucy"), new SetVariable { Variable = approvedVariable, Value = new(false) }, new WriteHttpResponse { Content = new("Sorry to hear that, Lucy!"), } } } } } } } } }, new WriteLine(context => $"Approved: {approvedVariable.Get(context)}"), new If(context => approvedVariable.Get(context)) { Then = new WriteLine(context => $"Document ${documentVariable.Get(context)!.Id} approved!"), Else = new WriteLine(context => $"Document ${documentVariable.Get(context)!.Id} rejected!") } } }; } private string GenerateSignalUrl(ExpressionExecutionContext context, string signalName) { return context.GenerateEventTriggerUrl(signalName); } } }