2020-02-15 19:32:16 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
using Elsa.Models;
|
2020-10-10 15:13:52 +00:00
|
|
|
using Elsa.Queries;
|
2020-02-15 19:32:16 +00:00
|
|
|
using Elsa.Serialization;
|
|
|
|
|
using Elsa.Server.GraphQL.Models;
|
|
|
|
|
using Elsa.Server.GraphQL.Types;
|
|
|
|
|
using Elsa.Services;
|
|
|
|
|
using HotChocolate;
|
2020-10-10 15:13:52 +00:00
|
|
|
using Open.Linq.AsyncExtensions;
|
|
|
|
|
using YesSql;
|
|
|
|
|
using IIdGenerator = Elsa.Services.IIdGenerator;
|
2020-02-15 19:32:16 +00:00
|
|
|
|
|
|
|
|
namespace Elsa.Server.GraphQL
|
|
|
|
|
{
|
|
|
|
|
public class Mutation
|
|
|
|
|
{
|
2020-10-09 17:52:54 +00:00
|
|
|
private readonly IMapper _mapper;
|
2020-02-15 19:32:16 +00:00
|
|
|
|
|
|
|
|
public Mutation(IMapper mapper)
|
|
|
|
|
{
|
2020-10-10 15:13:52 +00:00
|
|
|
_mapper = mapper;
|
2020-02-15 19:32:16 +00:00
|
|
|
}
|
2020-10-10 15:13:52 +00:00
|
|
|
|
2020-10-09 20:26:19 +00:00
|
|
|
public async Task<WorkflowDefinition> SaveWorkflowDefinition(
|
2020-02-15 19:32:16 +00:00
|
|
|
string? id,
|
|
|
|
|
WorkflowSaveAction saveAction,
|
|
|
|
|
WorkflowInput workflowInput,
|
2020-10-10 15:13:52 +00:00
|
|
|
[Service] ISession session,
|
2020-02-15 19:32:16 +00:00
|
|
|
[Service] IIdGenerator idGenerator,
|
|
|
|
|
[Service] ITokenSerializer serializer,
|
|
|
|
|
[Service] IWorkflowPublisher publisher,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2020-10-10 15:13:52 +00:00
|
|
|
var workflowDefinition = id != null
|
|
|
|
|
? await session.GetWorkflowDefinitionAsync(id, VersionOptions.Latest, cancellationToken)
|
|
|
|
|
: default;
|
2020-02-15 19:32:16 +00:00
|
|
|
|
|
|
|
|
if (workflowDefinition == null)
|
|
|
|
|
{
|
2020-10-09 20:26:19 +00:00
|
|
|
workflowDefinition = new WorkflowDefinition
|
2020-02-15 19:32:16 +00:00
|
|
|
{
|
2020-10-09 20:26:19 +00:00
|
|
|
WorkflowDefinitionId = idGenerator.Generate(),
|
|
|
|
|
WorkflowDefinitionVersionId = idGenerator.Generate(),
|
2020-02-15 19:32:16 +00:00
|
|
|
Version = 1,
|
|
|
|
|
IsLatest = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (workflowInput.Activities != null)
|
|
|
|
|
workflowDefinition.Activities = workflowInput.Activities.Select(ToActivityDefinition).ToList();
|
|
|
|
|
|
|
|
|
|
if (workflowInput.Connections != null)
|
|
|
|
|
workflowDefinition.Connections = workflowInput.Connections;
|
|
|
|
|
|
|
|
|
|
if (workflowInput.Description != null)
|
|
|
|
|
workflowDefinition.Description = workflowInput.Description.Trim();
|
|
|
|
|
|
|
|
|
|
if (workflowInput.Name != null)
|
|
|
|
|
workflowDefinition.Name = workflowInput.Name.Trim();
|
|
|
|
|
|
2020-10-10 15:13:52 +00:00
|
|
|
if (workflowInput.IsEnabled != null)
|
|
|
|
|
workflowDefinition.IsEnabled = workflowInput.IsEnabled.Value;
|
2020-02-15 19:32:16 +00:00
|
|
|
|
|
|
|
|
if (workflowInput.IsSingleton != null)
|
|
|
|
|
workflowDefinition.IsSingleton = workflowInput.IsSingleton.Value;
|
|
|
|
|
|
|
|
|
|
if (workflowInput.DeleteCompletedInstances != null)
|
|
|
|
|
workflowDefinition.DeleteCompletedInstances = workflowInput.DeleteCompletedInstances.Value;
|
|
|
|
|
|
|
|
|
|
if (workflowInput.Variables != null)
|
|
|
|
|
workflowDefinition.Variables = serializer.Deserialize<Variables>(workflowInput.Variables);
|
|
|
|
|
|
|
|
|
|
if (workflowInput.PersistenceBehavior != null)
|
|
|
|
|
workflowDefinition.PersistenceBehavior = workflowInput.PersistenceBehavior.Value;
|
|
|
|
|
|
|
|
|
|
if (saveAction == WorkflowSaveAction.Publish)
|
|
|
|
|
workflowDefinition = await publisher.PublishAsync(workflowDefinition, cancellationToken);
|
|
|
|
|
else
|
|
|
|
|
workflowDefinition = await publisher.SaveDraftAsync(workflowDefinition, cancellationToken);
|
|
|
|
|
|
|
|
|
|
return workflowDefinition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> DeleteWorkflowDefinition(
|
|
|
|
|
string id,
|
2020-10-10 15:13:52 +00:00
|
|
|
[Service] ISession session,
|
2020-02-15 19:32:16 +00:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2020-10-10 15:13:52 +00:00
|
|
|
var workflowDefinitions = await session.QueryWorkflowDefinitionByIdAndVersion(id, VersionOptions.All)
|
|
|
|
|
.ListAsync()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var workflowDefinition in workflowDefinitions)
|
|
|
|
|
session.Delete(workflowDefinition);
|
|
|
|
|
|
|
|
|
|
return workflowDefinitions.Count;
|
2020-02-15 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
2020-10-10 15:13:52 +00:00
|
|
|
private ActivityDefinition ToActivityDefinition(ActivityDefinitionInput source) =>
|
|
|
|
|
_mapper.Map<ActivityDefinition>(source);
|
2020-02-15 19:32:16 +00:00
|
|
|
}
|
|
|
|
|
}
|