2021-11-24 15:49:25 +01:00
|
|
|
//using Microsoft.Extensions.Logging;
|
|
|
|
|
//using Raven.Client.Documents;
|
|
|
|
|
//using Raven.Client.Documents.Linq;
|
2021-01-15 13:56:50 +01:00
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
//namespace zero.Core.Configuration;
|
|
|
|
|
|
|
|
|
|
//public class IntegrationsCollection : EntityStore<Integration>, IIntegrationsCollection
|
2021-11-24 15:49:25 +01:00
|
|
|
//{
|
2021-12-21 15:51:26 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public IReadOnlyCollection<IntegrationType> RegisteredTypes { get; private set; }
|
|
|
|
|
|
|
|
|
|
// protected ILogger<IntegrationsCollection> Logger { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// public IntegrationsCollection(IStoreContext<Integration> context, ILogger<IntegrationsCollection> logger) : base(context)
|
2021-11-24 15:49:25 +01:00
|
|
|
// {
|
2021-12-21 15:51:26 +01:00
|
|
|
// Options = new(true);
|
|
|
|
|
// RegisteredTypes = Context.Options.Integrations.GetAllItems();
|
|
|
|
|
// Logger = logger;
|
|
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
|
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public Integration GetEmpty(string alias)
|
|
|
|
|
// {
|
|
|
|
|
// IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
|
|
// if (type == null)
|
2021-11-24 15:49:25 +01:00
|
|
|
// {
|
|
|
|
|
// return null;
|
|
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// try
|
2021-11-24 15:49:25 +01:00
|
|
|
// {
|
2021-12-21 15:51:26 +01:00
|
|
|
// Integration model = Activator.CreateInstance(type.ContentType) as Integration;
|
|
|
|
|
// model.TypeAlias = type.Alias;
|
|
|
|
|
// return model;
|
|
|
|
|
// }
|
|
|
|
|
// catch
|
|
|
|
|
// {
|
|
|
|
|
// Logger.LogWarning("Could not create integration with type {alias}", alias);
|
2021-11-24 15:49:25 +01:00
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// return null;
|
2021-11-24 15:49:25 +01:00
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
|
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<Integration> GetByAlias(string alias)
|
2021-11-24 15:49:25 +01:00
|
|
|
// {
|
2021-12-21 15:51:26 +01:00
|
|
|
// IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
// return await Load<Integration>(type);
|
|
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
|
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<T> Get<T>(string alias = null) where T : Integration, new()
|
|
|
|
|
// {
|
|
|
|
|
// IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.ContentType == typeof(T) && (alias.IsNullOrEmpty() || x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase)));
|
|
|
|
|
// return await Load<T>(type);
|
|
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
|
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<IList<Integration>> GetByTag(string tag)
|
|
|
|
|
// {
|
|
|
|
|
// IEnumerable<IntegrationType> types = RegisteredTypes.Where(x => x.Tags.Contains(tag, StringComparer.InvariantCultureIgnoreCase));
|
2021-01-15 13:56:50 +01:00
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// if (!types.Any())
|
|
|
|
|
// {
|
|
|
|
|
// return new List<Integration>();
|
|
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// string[] aliases = types.Select(x => x.Alias).ToArray();
|
|
|
|
|
// return await Session.Query<Integration>().Where(x => x.TypeAlias.In(aliases)).ToListAsync();
|
|
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
|
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<bool> Any(string tag)
|
|
|
|
|
// {
|
|
|
|
|
// return (await GetByTag(tag)).Any(x => x.IsActive);
|
|
|
|
|
// }
|
2021-01-15 13:56:50 +01:00
|
|
|
|
|
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public override async Task<Paged<Integration>> Load(ListQuery<Integration> query)
|
|
|
|
|
// {
|
|
|
|
|
// List<Integration> result = new();
|
|
|
|
|
// List<Integration> models = await Session.Query<Integration>().ToListAsync();
|
2021-01-15 13:56:50 +01:00
|
|
|
|
2021-12-21 15:51:26 +01:00
|
|
|
// foreach (IntegrationType type in RegisteredTypes)
|
|
|
|
|
// {
|
|
|
|
|
// Integration model = models.FirstOrDefault(x => x.TypeAlias == type.Alias);
|
|
|
|
|
|
|
|
|
|
// if (model != null)
|
|
|
|
|
// {
|
|
|
|
|
// model.Name = type.Name;
|
|
|
|
|
// result.Add(model);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return result.ToQueriedList(query);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<IList<IntegrationTypeWithStatus>> GetTypesWithStatus()
|
|
|
|
|
// {
|
|
|
|
|
// List<IntegrationTypeWithStatus> result = new();
|
|
|
|
|
// List<Integration> models = await Session.Query<Integration>().ToListAsync();
|
|
|
|
|
|
|
|
|
|
// foreach (IntegrationType type in RegisteredTypes)
|
|
|
|
|
// {
|
|
|
|
|
// Integration model = models.FirstOrDefault(x => x.TypeAlias == type.Alias);
|
|
|
|
|
|
|
|
|
|
// result.Add(new()
|
|
|
|
|
// {
|
|
|
|
|
// Type = type,
|
|
|
|
|
// Id = model?.Id,
|
|
|
|
|
// IsActive = model?.IsActive ?? false,
|
|
|
|
|
// IsConfigured = model != null
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return result.OrderByDescending(x => x.IsActive).ThenByDescending(x => x.IsConfigured).ThenByDescending(x => x.Type.Name).ToList();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public override async Task<Result<Integration>> Save(Integration model)
|
|
|
|
|
// {
|
|
|
|
|
// if (model == null)
|
|
|
|
|
// {
|
|
|
|
|
// return Result<Integration>.Fail("@integration.errors.notfound");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.Alias.Equals(model.TypeAlias, StringComparison.InvariantCultureIgnoreCase));
|
|
|
|
|
|
|
|
|
|
// if (type == null)
|
|
|
|
|
// {
|
|
|
|
|
// return Result<Integration>.Fail("@integration.errors.typenotfound");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// string existingId = await Session.Query<Integration>().Where(x => x.TypeAlias == type.Alias).Select(x => x.Id).FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
// if (!existingId.IsNullOrEmpty() && existingId != model.Id)
|
|
|
|
|
// {
|
|
|
|
|
// return Result<Integration>.Fail("@integration.errors.multiplenotallowed");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// model.Alias = type.Alias;
|
|
|
|
|
// model.Name = null;
|
|
|
|
|
|
|
|
|
|
// Result<Integration> result = await base.Save(model);
|
|
|
|
|
|
|
|
|
|
// if (result.IsSuccess)
|
|
|
|
|
// {
|
|
|
|
|
// result.Model.Name = type.Name;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return result;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<Result<Integration>> Activate(string alias)
|
|
|
|
|
// {
|
|
|
|
|
// Integration model = await GetByAlias(alias);
|
|
|
|
|
// if (model != null)
|
|
|
|
|
// {
|
|
|
|
|
// model.IsActive = true;
|
|
|
|
|
// }
|
|
|
|
|
// return await Save(model);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<Result<Integration>> Deactivate(string alias)
|
|
|
|
|
// {
|
|
|
|
|
// Integration model = await GetByAlias(alias);
|
|
|
|
|
// if (model != null)
|
|
|
|
|
// {
|
|
|
|
|
// model.IsActive = false;
|
|
|
|
|
// }
|
|
|
|
|
// return await Save(model);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// <inheritdoc />
|
|
|
|
|
// public async Task<Result<Integration>> DeleteByAlias(string alias)
|
|
|
|
|
// {
|
|
|
|
|
// return await base.Delete(await GetByAlias(alias));
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get integration data from database
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// protected async Task<T> Load<T>(IntegrationType type) where T : Integration, new()
|
|
|
|
|
// {
|
|
|
|
|
// if (type == null)
|
|
|
|
|
// {
|
|
|
|
|
// return default;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// T entity = await Session.Query<T>().FirstOrDefaultAsync(x => x.TypeAlias == type.Alias);
|
|
|
|
|
|
|
|
|
|
// if (entity != null)
|
|
|
|
|
// {
|
|
|
|
|
// entity.Name = type.Name;
|
|
|
|
|
// entity.TypeAlias = type.Alias;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// return WhenActive(entity) as T;
|
2021-11-24 15:49:25 +01:00
|
|
|
// }
|
|
|
|
|
//}
|
2021-12-21 15:51:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//public interface IIntegrationsCollection
|
|
|
|
|
//{
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get all registered integration types
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// IReadOnlyCollection<IntegrationType> RegisteredTypes { get; }
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get new integration model for the specified integration type alias
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Integration GetEmpty(string alias);
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get integration by an alias
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<Integration> GetByAlias(string alias);
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get an integration by type and optional alias
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<T> Get<T>(string alias = null) where T : Integration, new();
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get all integrations by a certain tag
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<IList<Integration>> GetByTag(string tag);
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Check if any integrations of certain tag are activated
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<bool> Any(string tag);
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get all integrations with the specified query
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<Paged<Integration>> Load(ListQuery<Integration> query);
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Get all integration types with their configuration status
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<IList<IntegrationTypeWithStatus>> GetTypesWithStatus();
|
|
|
|
|
|
|
|
|
|
// /// <summary>
|
|
|
|
|
// /// Saves an integration
|
|
|
|
|
// /// </summary>
|
|
|
|
|
// Task<Result<Integration>> Save(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>> DeleteByAlias(string alias);
|
|
|
|
|
//}
|