/// <summary>
        /// Run the workflow associated with notification reply
        /// </summary>
        void RunReplyWorkflow(IWorkflowRunner runner, Workflow workflow, ReplyRecord reply)
        {
            if (workflow.WorkflowRunAsOwner != true)
            {
                throw new ArgumentException($"The provided workflow is not marked as run as owner. This should never occur. Workflow: ${workflow.Name}");
            }

            var input = new Dictionary <string, object>();

            var inputArg = workflow?.InputArgumentForAction?.As <ResourceArgument>();

            if (inputArg != null)
            {
                if (inputArg.ConformsToType.Id == ReplyRecord.ReplyRecord_Type.Id)
                {
                    input.Add(inputArg.Name, reply);
                }
            }

            // Set the context to the owner and start the workflow.
            var wfOwner     = workflow.SecurityOwner;
            var contextData = new RequestContextData(RequestContext.GetContext());

            contextData.Identity = new ReadiNow.Security.IdentityInfo(wfOwner.Id, wfOwner.Name);

            using (CustomContext.SetContext(contextData))
            {
                runner.RunWorkflowAsync(new WorkflowStartEvent(workflow)
                {
                    Arguments = input
                });
            }
        }
Ejemplo n.º 2
0
        public void Workflow_GoldenPath()
        {
            using (var scope = Factory.Current.BeginLifetimeScope(builder =>
            {
                builder.RegisterType <DummyNotifier>().As <INotifier>();
            }))
                using (Factory.SetCurrentScope(scope))
                {
                    var wf = CreateNotifyWorkflow(true);

                    var person = new Person {
                        Name = "bob",
                    };
                    person.SetField("shared:businessEmail", "*****@*****.**");
                    person.Save();

                    var inputs = new Dictionary <string, object>
                    {
                        { "People", person.ToEnumerable() },
                        { "Message", "Test" }
                    };

                    WorkflowRun run1;

                    using (new TestWfRunContext())
                    {
                        run1 = (RunWorkflow(wf, inputs));
                    }

                    Assert.That(run1.WorkflowRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunPaused));

                    var notifications = person.GetRelationships(new EntityRef("core", "srToPerson"), Direction.Reverse);

                    Assert.That(notifications.Count(), Is.EqualTo(1));

                    var send = notifications.First().As <SendRecord>();

                    Assert.That(send.SendToNotification, Is.Not.Null);
                    Assert.That(send.SendToNotification.NMessage, Is.EqualTo("Test"));

                    using (new TestWfRunContext())
                    {
                        // reply
                        var reply = new ReplyRecord {
                            RrToSend = send, RrReply = "replying", RrReplyDate = DateTime.UtcNow
                        };
                        reply.Save();
                    }

                    var run2 = Entity.Get <WorkflowRun>(run1.Id);

                    Assert.That(run2.WorkflowRunStatus_Enum, Is.EqualTo(WorkflowRunState_Enumeration.WorkflowRunCompleted));

                    IDictionary <string, object> outputs = run1.GetOutput();

                    Assert.That(outputs.Keys, Has.Member("Notification Record"));
                    var notification = (IEntity)outputs["Notification Record"];
                    Assert.That(notification, Is.Not.Null);
                }
        }
        public void ThatTheReplyWorflowRunsAndHasCorrectInput(bool workflowHasInput)
        {
            var dummyrunner = new DummyWorkflowRunner();


            using (var scope = Factory.Current.BeginLifetimeScope(builder =>
            {
                builder.Register(ctx => dummyrunner).As <IWorkflowRunner>();
            }))
                using (Factory.SetCurrentScope(scope))
                {
                    var workflow = new Workflow {
                        WorkflowRunAsOwner = true
                    };

                    if (workflowHasInput)
                    {
                        var inputArg = new ResourceArgument {
                            Name = "myInput", ConformsToType = ReplyRecord.ReplyRecord_Type
                        };
                        workflow.InputArguments.Add(inputArg.As <ActivityArgument>());
                    }

                    var notification = new Notification();
                    notification.NReplyMapCopy.Add(new ReplyMapEntry {
                        Name = "Reply", RmeWorkflow = workflow
                    });

                    var send = new SendRecord();
                    notification.SendRecords.Add(send);

                    notification.Save();

                    int runs = 0;

                    var reply = new ReplyRecord {
                        RrToSend = send, RrReply = "Reply to anything", RrReplyDate = DateTime.UtcNow
                    };

                    dummyrunner.StartWorkflowAsyncFn = (startEvent) => {
                        runs++;
                        if (workflowHasInput)
                        {
                            Assert.That(startEvent.Arguments.Keys, Has.Member("myInput"));
                            Assert.That(startEvent.Arguments["myInput"] is IEntity, Is.True);
                            Assert.That(((IEntity)startEvent.Arguments["myInput"]).Id, Is.EqualTo(reply.Id));
                        }

                        return("1");
                    };

                    reply.Save();

                    Assert.That(runs, Is.EqualTo(1));
                }
        }
        /// <summary>
        /// Match the replies against the replyMap and trigger a workflow if there is a match
        /// </summary>
        void TestAndRunReplyMap(IWorkflowRunner runner, IEnumerable <ReplyMapEntry> replyMap, ReplyRecord reply)
        {
            var message = reply?.RrReply?.ToLower();

            if (message == null)
            {
                return;
            }

            var entry = ReplyRecordEventTarget.SelectReplyMapEntry(replyMap, message);

            if (entry != null)
            {
                RunReplyWorkflow(runner, entry.RmeWorkflow, reply);
            }
        }