public async Task <IActionResult> PostWorkflow(string app, [FromBody] AddWorkflowDto request)
        {
            var command = request.ToCommand();

            var response = await InvokeCommandAsync(command);

            return(Ok(response));
        }
Beispiel #2
0
        public async Task Should_manage_workflows()
        {
            var workflowName = Guid.NewGuid().ToString();

            // STEP 0: Create workflow.
            var createRequest = new AddWorkflowDto
            {
                Name = workflowName
            };

            var workflows_1 = await _.Apps.PostWorkflowAsync(_.AppName, createRequest);

            var workflow_1 = workflows_1.Items.FirstOrDefault(x => x.Name == workflowName);

            Assert.NotNull(workflow_1);
            Assert.NotNull(workflow_1.Name);
            Assert.Equal(3, workflow_1.Steps.Count);


            // STEP 1: Update workflow.
            var updateRequest = new UpdateWorkflowDto
            {
                Initial = "Draft",
                Steps   = new Dictionary <string, WorkflowStepDto>
                {
                    ["Draft"] = new WorkflowStepDto
                    {
                        Transitions = new Dictionary <string, WorkflowTransitionDto>
                        {
                            ["Published"] = new WorkflowTransitionDto()
                        }
                    },
                    ["Published"] = new WorkflowStepDto(),
                },
                Name = workflowName
            };

            var workflows_2 = await _.Apps.PutWorkflowAsync(_.AppName, workflow_1.Id, updateRequest);

            var workflow_2 = workflows_2.Items.FirstOrDefault(x => x.Name == workflowName);

            Assert.NotNull(workflow_2);
            Assert.NotNull(workflow_2.Name);
            Assert.Equal(2, workflow_2.Steps.Count);


            // STEP 2: Delete workflow.
            var workflows_3 = await _.Apps.DeleteWorkflowAsync(_.AppName, workflow_1.Id);

            var workflow_3 = workflows_3.Items.FirstOrDefault(x => x.Name == workflowName);

            // Should not return deleted workflow.
            Assert.Null(workflow_3);
        }
        private async Task <WorkflowDto> CreateAsync()
        {
            var createRequest = new AddWorkflowDto
            {
                Name = name
            };

            var workflows = await _.Apps.PostWorkflowAsync(appName, createRequest);

            var workflow = workflows.Items.Find(x => x.Name == name);

            return(workflow);
        }
        public async Task ImportAsync(DirectoryInfo directoryInfo, JsonHelper jsonHelper, SyncOptions options, ISession session)
        {
            var newWorkflows = GetWorkflowModels(directoryInfo, jsonHelper).ToList();

            if (!newWorkflows.HasDistinctNames(x => x.Name))
            {
                log.WriteLine("ERROR: Can only sync workflows when all target workflows have distinct names.");
                return;
            }

            var current = await session.Apps.GetWorkflowsAsync(session.App);

            if (!current.Items.HasDistinctNames(x => x.Name))
            {
                log.WriteLine("ERROR: Can only sync workflows when all current workflows have distinct names.");
                return;
            }

            var workflowsByName = current.Items.ToDictionary(x => x.Name);

            if (options.NoDeletion)
            {
                foreach (var(name, workflow) in workflowsByName.ToList())
                {
                    if (!newWorkflows.Any(x => x.Name != name))
                    {
                        await log.DoSafeAsync($"Workflow '{name}' deleting", async() =>
                        {
                            await session.Apps.DeleteWorkflowAsync(session.App, workflow.Id.ToString());

                            workflowsByName.Remove(name);
                        });
                    }
                }
            }

            foreach (var newWorkflow in newWorkflows)
            {
                if (workflowsByName.ContainsKey(newWorkflow.Name))
                {
                    continue;
                }

                await log.DoSafeAsync($"Workflow '{newWorkflow.Name}' creating", async() =>
                {
                    if (workflowsByName.ContainsKey(newWorkflow.Name))
                    {
                        throw new CLIException("Name already used.");
                    }

                    var request = new AddWorkflowDto
                    {
                        Name = newWorkflow.Name
                    };

                    var created = await session.Apps.PostWorkflowAsync(session.App, request);

                    workflowsByName[newWorkflow.Name] = created.Items.FirstOrDefault(x => x.Name == newWorkflow.Name);
                });
            }

            foreach (var newWorkflow in newWorkflows)
            {
                var workflow = workflowsByName.GetValueOrDefault(newWorkflow.Name);

                if (workflow == null)
                {
                    return;
                }

                await log.DoSafeAsync($"Workflow '{newWorkflow.Name}' updating", async() =>
                {
                    await session.Apps.PutWorkflowAsync(session.App, workflow.Id.ToString(), newWorkflow);
                });
            }
        }
        public async Task ImportAsync(ISyncService sync, SyncOptions options, ISession session)
        {
            var models =
                GetFiles(sync.FileSystem)
                .Select(x => sync.Read <UpdateWorkflowDto>(x, log))
                .ToList();

            if (!models.HasDistinctNames(x => x.Name))
            {
                log.WriteLine("ERROR: Can only sync workflows when all target workflows have distinct names.");
                return;
            }

            var current = await session.Apps.GetWorkflowsAsync(session.App);

            if (!current.Items.HasDistinctNames(x => x.Name))
            {
                log.WriteLine("ERROR: Can only sync workflows when all current workflows have distinct names.");
                return;
            }

            var workflowsByName = current.Items.ToDictionary(x => x.Name);

            if (options.Delete)
            {
                foreach (var(name, workflow) in workflowsByName.ToList())
                {
                    if (models.All(x => x.Name == name))
                    {
                        await log.DoSafeAsync($"Workflow '{name}' deleting", async() =>
                        {
                            await session.Apps.DeleteWorkflowAsync(session.App, workflow.Id);

                            workflowsByName.Remove(name);
                        });
                    }
                }
            }

            foreach (var workflow in models)
            {
                if (workflowsByName.ContainsKey(workflow.Name))
                {
                    continue;
                }

                await log.DoSafeAsync($"Workflow '{workflow.Name}' creating", async() =>
                {
                    if (workflowsByName.ContainsKey(workflow.Name))
                    {
                        throw new CLIException("Name already used.");
                    }

                    var request = new AddWorkflowDto
                    {
                        Name = workflow.Name
                    };

                    var created = await session.Apps.PostWorkflowAsync(session.App, request);

                    workflowsByName[workflow.Name] = created.Items.FirstOrDefault(x => x.Name == workflow.Name);
                });
            }

            var schemas = await session.Schemas.GetSchemasAsync(session.App);

            var schemaMap = schemas.Items.ToDictionary(x => x.Id, x => x.Name);

            foreach (var workflow in models)
            {
                var existing = workflowsByName.GetValueOrDefault(workflow.Name);

                if (existing == null)
                {
                    return;
                }

                MapSchemas(workflow, schemaMap);

                await log.DoSafeAsync($"Workflow '{workflow.Name}' updating", async() =>
                {
                    await session.Apps.PutWorkflowAsync(session.App, existing.Id, workflow);
                });
            }
        }