using System; using System.Net; using System.Net.Http; using Elsa; using Elsa.Activities.Email.Activities; using Elsa.Activities.Http.Activities; using Elsa.Activities.ControlFlow; using Elsa.Activities.Primitives; using Elsa.Expressions; using Elsa.Services; using Elsa.Services.Models; namespace Sample07 { public class DocumentApprovalWorkflow : IWorkflow { public void Build(IWorkflowBuilder builder) { builder .StartWith( activity => { activity.Method = HttpMethod.Post.Method; activity.Path = new Uri("/documents", UriKind.RelativeOrAbsolute); activity.ReadContent = true; } ) .Then( activity => { activity.VariableName = "document"; activity.Expression = new JavaScriptExpression("lastResult().ParsedContent"); } ) .Then( activity => { activity.From = new PlainTextExpression("approval@acme.com"); activity.To = new JavaScriptExpression("document.author.email"); activity.Subject = new JavaScriptExpression("`Document received from ${document.author.name}`"); activity.Body = new JavaScriptExpression( "`Document from ${document.author.name} received for review. " + "Approve or Reject`" ); } ) .Then( activity => { activity.Content = new PlainTextExpression("

Request for Approval Sent

Your document has been received and will be reviewed shortly.

"); activity.ContentType = new PlainTextExpression("text/html"); activity.StatusCode = HttpStatusCode.OK; activity.ResponseHeaders = new PlainTextExpression("X-Powered-By=Elsa Workflows"); } ) .Then( activity => { activity.Branches = new[] { "Approve", "Reject" }; }, fork => { fork .When("Approve") .Then(activity => activity.Signal = new PlainTextExpression("approve")) .Then("join-signals"); fork .When("Reject") .Then(activity => activity.Signal = new PlainTextExpression("reject")) .Then("join-signals"); } ) .Add(activity => activity.Mode = Join.JoinMode.WaitAny, "join-signals") .Then(activity => activity.Expression = new JavaScriptExpression("input('signal') === 'approve'"), ifElse => { ifElse .When(OutcomeNames.True) .Then( activity => { activity.From = new PlainTextExpression("approval@acme.com"); activity.To = new JavaScriptExpression("document.author.email"); activity.Subject = new JavaScriptExpression("`Document ${document.id} approved!`"); activity.Body = new JavaScriptExpression("`Great job ${document.author.name}, that document is perfect! Keep it up.`"); }); ifElse .When(OutcomeNames.False) .Then( activity => { activity.From = new PlainTextExpression("approval@acme.com"); activity.To = new JavaScriptExpression("document.author.email"); activity.Subject = new JavaScriptExpression("`Document ${document.id} rejected`"); activity.Body = new JavaScriptExpression("`Sorry ${document.author.name}, that document isn't good enough. Please try again.`"); }); }); } } }