2021-12-22 15:41:11 +01:00
|
|
|
using FluentValidation.Results;
|
2022-01-06 17:03:05 +01:00
|
|
|
using Raven.Client.Documents;
|
|
|
|
|
using Raven.Client.Documents.Linq;
|
2021-12-22 15:41:11 +01:00
|
|
|
|
|
|
|
|
namespace zero.Configuration;
|
|
|
|
|
|
|
|
|
|
public class IntegrationStore : IIntegrationStore
|
|
|
|
|
{
|
|
|
|
|
protected IStoreOperations Operations { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected IIntegrationTypeService IntegrationTypes { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
2021-12-28 11:33:41 +01:00
|
|
|
public IntegrationStore(IStoreOperationsWithInactive operations, IIntegrationTypeService integrationTypes)
|
2021-12-22 15:41:11 +01:00
|
|
|
{
|
|
|
|
|
IntegrationTypes = integrationTypes;
|
2021-12-28 11:33:41 +01:00
|
|
|
Operations = operations;
|
2021-12-22 15:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-12-23 09:07:34 +01:00
|
|
|
public virtual async Task<Integration> Empty(string alias)
|
2021-12-22 15:41:11 +01:00
|
|
|
{
|
2021-12-23 09:07:34 +01:00
|
|
|
return await Operations.Empty<Integration>(alias);
|
2021-12-22 15:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual async Task<Integration> Load(string alias)
|
|
|
|
|
{
|
2021-12-23 09:07:34 +01:00
|
|
|
Paged<Integration> result = await Operations.Load<Integration>(1, 1, q => q.Where(x => x.Flavor == alias));
|
2021-12-22 15:41:11 +01:00
|
|
|
return result.Items.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 17:03:05 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task<IList<Integration>> LoadByTag(string tag)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<IntegrationType> types = IntegrationTypes.GetByTag(tag);
|
|
|
|
|
|
|
|
|
|
if (!types.Any())
|
|
|
|
|
{
|
|
|
|
|
return new List<Integration>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] aliases = types.Select(x => x.Alias).ToArray();
|
|
|
|
|
return await Operations.Session.Query<Integration>().Where(x => x.Flavor.In(aliases)).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task<bool> Any(string tag)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<IntegrationType> types = IntegrationTypes.GetByTag(tag);
|
|
|
|
|
|
|
|
|
|
if (!types.Any())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] aliases = types.Select(x => x.Alias).ToArray();
|
|
|
|
|
return await Operations.Session.Query<Integration>().AnyAsync(x => x.Flavor.In(aliases) && x.IsActive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-22 15:41:11 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual string GetChangeToken(Integration model) => Operations.GetChangeToken(model);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual async Task<Result<Integration>> Create(Integration model) => await Save(model, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual async Task<Result<Integration>> Update(Integration model) => await Save(model, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual async Task<Result<Integration>> Activate(string alias)
|
|
|
|
|
{
|
|
|
|
|
Integration model = await Load(alias);
|
|
|
|
|
if (model != null)
|
|
|
|
|
{
|
|
|
|
|
model.IsActive = true;
|
|
|
|
|
}
|
|
|
|
|
return await Update(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public virtual async Task<Result<Integration>> Deactivate(string alias)
|
|
|
|
|
{
|
|
|
|
|
Integration model = await Load(alias);
|
|
|
|
|
if (model != null)
|
|
|
|
|
{
|
|
|
|
|
model.IsActive = false;
|
|
|
|
|
}
|
|
|
|
|
return await Update(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async Task<Result<Integration>> Delete(string alias)
|
|
|
|
|
{
|
|
|
|
|
Integration integration = await Load(alias);
|
|
|
|
|
return await Operations.Delete(integration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// <inheritdoc />
|
|
|
|
|
protected virtual async Task<ValidationResult> Validate(Integration model)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(0);
|
|
|
|
|
return new ValidationResult();
|
|
|
|
|
//ZeroValidator<T> validator = new();
|
|
|
|
|
//ValidationRules(validator);
|
|
|
|
|
//return await validator.ValidateAsync(model);
|
|
|
|
|
//return base.Validate(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected virtual async Task<Result<Integration>> Save(Integration model, bool create = false)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return Result<Integration>.Fail("@integration.errors.notfound");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 09:07:34 +01:00
|
|
|
IntegrationType type = IntegrationTypes.GetByAlias(model.Flavor);
|
2021-12-22 15:41:11 +01:00
|
|
|
|
|
|
|
|
if (type == null)
|
|
|
|
|
{
|
|
|
|
|
return Result<Integration>.Fail("@integration.errors.typenotfound");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 09:07:34 +01:00
|
|
|
if (create && await Operations.Any<Integration>(q => q.Where(x => x.Flavor == model.Flavor)))
|
2021-12-22 15:41:11 +01:00
|
|
|
{
|
|
|
|
|
return Result<Integration>.Fail("@integration.errors.multiplenotallowed");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-23 09:07:34 +01:00
|
|
|
if (!create && await Operations.Any<Integration>(q => q.Where(x => x.Flavor == model.Flavor && x.Id != model.Id)))
|
2021-12-22 15:41:11 +01:00
|
|
|
{
|
|
|
|
|
return Result<Integration>.Fail("@integration.errors.alreadycreated");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await Operations.Create(model, async x => await Validate(x));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface IIntegrationStore
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get new instance of an integration by integration alias
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Integration> Empty(string alias);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get an integration by integration alias
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Integration> Load(string alias);
|
|
|
|
|
|
2022-01-06 17:03:05 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get all integrations by a certain tag
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<IList<Integration>> LoadByTag(string tag);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check if there are any activated integrations for a certain tag
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<bool> Any(string tag);
|
|
|
|
|
|
2021-12-22 15:41:11 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get the change vector for a model (Proxy to IAsyncDocumentSession.GetChangeVectorFor<>)
|
|
|
|
|
/// </summary>
|
|
|
|
|
string GetChangeToken(Integration model);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new integration
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Result<Integration>> Create(Integration model);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates an integration
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Result<Integration>> Update(Integration model);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Activates a configured integration
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Result<Integration>> Activate(string alias);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disables a configured integration
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Result<Integration>> Deactivate(string alias);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes configuration of an integration and disables it
|
|
|
|
|
/// </summary>
|
|
|
|
|
Task<Result<Integration>> Delete(string alias);
|
|
|
|
|
}
|