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.Activities.Timers.Activities; using Elsa.Activities.Workflows; using Elsa.Expressions; using Elsa.Services; using Elsa.Services.Models; namespace Sample07 { public class DocumentApprovalWorkflow : IWorkflow { public void Build(IWorkflowBuilder builder) { builder .StartWith( x => { x.Method = HttpMethod.Post.Method; x.Path = new Uri("/documents", UriKind.Relative); x.ReadContent = true; }, "WaitForDocument" ) .Then( x => { x.VariableName = "Document"; x.ValueExpression = new JavaScriptExpression("lastResult().ParsedContent"); } ) .Then( x => { x.From = new LiteralExpression("approval@acme.com"); x.To = new JavaScriptExpression("Document.Author.Email"); x.Subject = new JavaScriptExpression("`Document received from ${Document.Author.Name}`"); x.Body = new JavaScriptExpression( "`Document from ${Document.Author.Name} received for review. " + "Approve or Reject`" ); } ) .Then( x => { x.Content = new LiteralExpression( "

Request for Approval Sent

Your document has been received and will be reviewed shortly.

" ); x.ContentType = "text/html"; x.StatusCode = HttpStatusCode.OK; x.ResponseHeaders = new LiteralExpression("X-Powered-By=Elsa Workflows"); } ) .Then( x => { x.VariableName = "Approved"; x.ValueExpression = new LiteralExpression("false"); } ) .Then( x => { x.Branches = new[] { "Approve", "Reject", "Remind" }; }, fork => { fork .When("Approve") .Then(x => x.Signal = new LiteralExpression("Approve")) .Then("Join"); fork .When("Reject") .Then(x => x.Signal = new LiteralExpression("Reject")) .Then("Join"); fork .When("Remind") .Then( x => x.TimeoutExpression = new LiteralExpression("00:00:10"), id: "RemindTimer" ) .Then( x => x.ConditionExpression = new JavaScriptExpression("!!Approved"), ifElse => { ifElse .When(OutcomeNames.False) .Then( x => { x.From = new LiteralExpression("reminder@acme.com"); x.To = new LiteralExpression("approval@acme.com"); x.Subject = new JavaScriptExpression( "`${Document.Author.Name} is awaiting for your review!`" ); x.Body = new JavaScriptExpression( "`Don't forget to review document ${Document.Id}.
" + "Approve or Reject`" ); } ) .Then("RemindTimer"); } ); } ) .Then(x => x.Mode = Join.JoinMode.WaitAny, id: "Join") .Then( x => { x.VariableName = "Approved"; x.ValueExpression = new JavaScriptExpression("input('Signal') === 'Approve'"); } ) .Then( x => x.ConditionExpression = new JavaScriptExpression("!!Approved"), ifElse => { ifElse .When(OutcomeNames.True) .Then( x => { x.From = new LiteralExpression("approval@acme.com"); x.To = new JavaScriptExpression("Document.Author.Email"); x.Subject = new JavaScriptExpression("`Document ${Document.Id} approved!`"); x.Body = new JavaScriptExpression( "`Great job ${Document.Author.Name}, that document is perfect! Keep it up.`" ); } ); ifElse .When(OutcomeNames.False) .Then( x => { x.From = new LiteralExpression("approval@acme.com"); x.To = new JavaScriptExpression("Document.Author.Email"); x.Subject = new JavaScriptExpression("`Document ${Document.Id} rejected`"); x.Body = new JavaScriptExpression( "`Sorry ${Document.Author.Name}, that document isn't good enough. Please try again.`" ); } ); } ); } } }