using Raven.Client.Documents; using Raven.Client.Documents.Linq; using Raven.Client.Documents.Session; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using zero.Core.Database; using zero.Core.Options; namespace zero.Core.Integrations { public class IntegrationService : IIntegrationService { /// public IReadOnlyCollection RegisteredTypes { get; private set; } protected IZeroOptions Options { get; private set; } protected IZeroStore Store { get; private set; } public IntegrationService(IZeroOptions options, IZeroStore store) { Options = options; Store = store; RegisteredTypes = options.Integrations.GetAllItems(); } /// public async Task Get() where T : IIntegration, new() { IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.ContentType == typeof(T)); return await GetIntegration(type); } /// public async Task Get(string alias) where T : IIntegration, new() { IntegrationType type = RegisteredTypes.FirstOrDefault(x => x.ContentType == typeof(T) && x.Alias.Equals(alias, StringComparison.InvariantCultureIgnoreCase)); return await GetIntegration(type); } /// public async Task Any(string tag) { IEnumerable types = RegisteredTypes.Where(x => x.Tags.Contains(tag, StringComparer.InvariantCultureIgnoreCase)); if (!types.Any()) { return false; } //if (types.Any(x => x.IsAutoActivated)) //{ // return true; //} string[] aliases = types.Select(x => x.Alias).ToArray(); using IAsyncDocumentSession session = Store.OpenAsyncSession(); return await session.Query().AnyAsync(x => x.TypeAlias.In(aliases) && x.IsActive); } /// /// Get integration data from database /// async Task GetIntegration(IntegrationType type) where T : IIntegration, new() { if (type == null) { return default; } using IAsyncDocumentSession session = Store.OpenAsyncSession(); T integration = await session.Query().FirstOrDefaultAsync(x => x.TypeAlias == type.Alias && x.IsActive); if (integration == null)// && type.IsAutoActivated) { return new T(); } return integration; } } public interface IIntegrationService { /// /// Get an integration by type /// Task Get() where T : IIntegration, new(); /// /// Get an integration by type and alias /// Task Get(string alias) where T : IIntegration, new(); /// /// Checks if any integrations for a certain tag are activated /// Task Any(string tag); } }