using Elsa.Common.Models;
using Elsa.Workflows;
using Elsa.Workflows.Management;
using Elsa.Workflows.Management.Entities;
using Elsa.Workflows.Management.Models;
using Elsa.Workflows.Runtime;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Testing.Shared;
///
/// Provides extension methods for .
///
[PublicAPI]
public static class ServiceProviderExtensions
{
/// The services.
extension(IServiceProvider services)
{
///
/// Updates the registries.
///
public Task PopulateRegistriesAsync()
{
var registriesPopulator = services.GetRequiredService();
return registriesPopulator.PopulateAsync();
}
///
/// Imports a workflow definition from a file.
///
/// The file name.
/// The workflow definition.
public async Task ImportWorkflowDefinitionAsync(string fileName)
{
var json = await File.ReadAllTextAsync(fileName);
var serializer = services.GetRequiredService();
var model = serializer.Deserialize(json);
var workflowDefinitionRequest = new SaveWorkflowDefinitionRequest
{
Model = model,
Publish = true
};
var workflowDefinitionImporter = services.GetRequiredService();
var result = await workflowDefinitionImporter.ImportAsync(workflowDefinitionRequest);
return result.WorkflowDefinition;
}
///
/// Retrieves a workflow definition by its ID.
///
/// The definition ID of the workflow definition.
/// Options to specify the version of the workflow definition to retrieve.
/// A cancellation token to cancel the operation.
/// The retrieved workflow definition.
public async Task GetWorkflowDefinitionAsync(string workflowDefinitionId, VersionOptions versionOptions, CancellationToken cancellationToken = default)
{
var workflowDefinitionService = services.GetRequiredService();
var workflowDefinition = await workflowDefinitionService.FindWorkflowDefinitionAsync(workflowDefinitionId, versionOptions, cancellationToken);
return workflowDefinition!;
}
}
}