diff --git a/zero.Api/Endpoints/Users/UsersController.cs b/zero.Api/Endpoints/Users/UsersController.cs index 039305f7..0282ca5a 100644 --- a/zero.Api/Endpoints/Users/UsersController.cs +++ b/zero.Api/Endpoints/Users/UsersController.cs @@ -14,7 +14,7 @@ public class UsersController : ZeroApiController [HttpGet("empty")] [ZeroAuthorize(UserPermissions.Create)] - public virtual async Task> Empty(string flavor = null) + public virtual ActionResult Empty(string flavor = null) { return new ZeroUser(); } diff --git a/zero.Backoffice.UI/package.json b/zero.Backoffice.UI/package.json index 65ddf2c0..abc26ca7 100644 --- a/zero.Backoffice.UI/package.json +++ b/zero.Backoffice.UI/package.json @@ -7,7 +7,7 @@ "build": "vite build" }, "workspaces": [ - "../plugins/zero.Commerce/Backoffice/Plugin" + "../plugins/zero.Commerce/Backoffice/Plugin" ], "dependencies": { "axios": "^0.24.0", diff --git a/zero.Core/Configuration/Integrations/IntegrationStore.cs b/zero.Core/Configuration/Integrations/IntegrationStore.cs index 783fd654..48bb952c 100644 --- a/zero.Core/Configuration/Integrations/IntegrationStore.cs +++ b/zero.Core/Configuration/Integrations/IntegrationStore.cs @@ -1,4 +1,6 @@ using FluentValidation.Results; +using Raven.Client.Documents; +using Raven.Client.Documents.Linq; namespace zero.Configuration; @@ -31,6 +33,36 @@ public class IntegrationStore : IIntegrationStore } + /// + public async Task> LoadByTag(string tag) + { + IEnumerable types = IntegrationTypes.GetByTag(tag); + + if (!types.Any()) + { + return new List(); + } + + string[] aliases = types.Select(x => x.Alias).ToArray(); + return await Operations.Session.Query().Where(x => x.Flavor.In(aliases)).ToListAsync(); + } + + + /// + public async Task Any(string tag) + { + IEnumerable types = IntegrationTypes.GetByTag(tag); + + if (!types.Any()) + { + return false; + } + + string[] aliases = types.Select(x => x.Alias).ToArray(); + return await Operations.Session.Query().AnyAsync(x => x.Flavor.In(aliases) && x.IsActive); + } + + /// public virtual string GetChangeToken(Integration model) => Operations.GetChangeToken(model); @@ -130,6 +162,16 @@ public interface IIntegrationStore /// Task Load(string alias); + /// + /// Get all integrations by a certain tag + /// + Task> LoadByTag(string tag); + + /// + /// Check if there are any activated integrations for a certain tag + /// + Task Any(string tag); + /// /// Get the change vector for a model (Proxy to IAsyncDocumentSession.GetChangeVectorFor<>) /// diff --git a/zero.Core/Configuration/Integrations/IntegrationTypeService.cs b/zero.Core/Configuration/Integrations/IntegrationTypeService.cs index 9627b393..bd073f47 100644 --- a/zero.Core/Configuration/Integrations/IntegrationTypeService.cs +++ b/zero.Core/Configuration/Integrations/IntegrationTypeService.cs @@ -36,6 +36,13 @@ public class IntegrationTypeService : IIntegrationTypeService { return Flavors.Get() as IntegrationType; } + + + /// + public IEnumerable GetByTag(string tag) + { + return GetAll().Where(x => x.Tags.Contains(tag, StringComparer.InvariantCultureIgnoreCase)); + } } @@ -55,4 +62,9 @@ public interface IIntegrationTypeService /// Get a specific integration type by model type. /// IntegrationType GetByType() where T : Integration; + + /// + /// Get all integrations with a certain tag. + /// + IEnumerable GetByTag(string tag); } diff --git a/zero.Core/Extensions/ServiceCollectionExtensions.cs b/zero.Core/Extensions/ServiceCollectionExtensions.cs index 4a4c31bc..2b6f3252 100644 --- a/zero.Core/Extensions/ServiceCollectionExtensions.cs +++ b/zero.Core/Extensions/ServiceCollectionExtensions.cs @@ -74,4 +74,21 @@ public static class ServiceCollectionExtensions services.RemoveAll(); services.Add(new ServiceDescriptor(typeof(TService), implementationFactory, lifetime)); } + + + /// + /// Adds or overrides an implementation + /// + public static void Replace(this IServiceCollection services) + where TService : class + where TOldImplementation : class, TService + where TNewImplementation : class, TService + { + ServiceDescriptor oldDescriptor = services.FirstOrDefault(x => x.ImplementationType == typeof(TOldImplementation)); + if (oldDescriptor != null) + { + services.Remove(oldDescriptor); + services.Add(new ServiceDescriptor(typeof(TService), typeof(TNewImplementation), oldDescriptor.Lifetime)); + } + } } diff --git a/zero.Core/Localization/LocalizationModule.cs b/zero.Core/Localization/LocalizationModule.cs index 75a61557..060eb94c 100644 --- a/zero.Core/Localization/LocalizationModule.cs +++ b/zero.Core/Localization/LocalizationModule.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.DependencyInjection; namespace zero.Localization; -public class LocalizationModule : ZeroModule +internal class LocalizationModule : ZeroModule { public override void ConfigureServices(IServiceCollection services, IConfiguration configuration) { diff --git a/zero.Core/Pages/PagesModule.cs b/zero.Core/Pages/PagesModule.cs index e4106057..f25510f8 100644 --- a/zero.Core/Pages/PagesModule.cs +++ b/zero.Core/Pages/PagesModule.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.DependencyInjection; namespace zero.Pages; -public class PagesModule : ZeroModule +internal class PagesModule : ZeroModule { public override void ConfigureServices(IServiceCollection services, IConfiguration configuration) { diff --git a/zero.Core/Persistence/IdGenerator.cs b/zero.Core/Persistence/IdGenerator.cs index 3addb588..c4612108 100644 --- a/zero.Core/Persistence/IdGenerator.cs +++ b/zero.Core/Persistence/IdGenerator.cs @@ -39,4 +39,27 @@ public class IdGenerator { return value.GetHashCode().ToString().Replace("-", String.Empty); } + + + /// + /// Autofill IDs on an object with [GenerateId] attributes + /// + public static T Autofill(T model) + { + // find all Raven Ids + List> ravenIds = ObjectTraverser.FindAttribute(model); + + // set unset Raven Ids + foreach (ObjectTraverser.Result item in ravenIds) + { + string id = item.Property.GetValue(item.Parent, null) as string; + if (id.IsNullOrWhiteSpace()) + { + id = item.Item.Length.HasValue ? Create(item.Item.Length.Value) : Create(); + item.Property.SetValue(item.Parent, id); + } + } + + return model; + } } diff --git a/zero.Core/Stores/StoreOperations.cs b/zero.Core/Stores/StoreOperations.cs index 5cafc445..892cfeaf 100644 --- a/zero.Core/Stores/StoreOperations.cs +++ b/zero.Core/Stores/StoreOperations.cs @@ -48,21 +48,7 @@ public partial class StoreOperations : /// public T AutoSetIds(T model) { - // find all Raven Ids - List> ravenIds = ObjectTraverser.FindAttribute(model); - - // set unset Raven Ids - foreach (ObjectTraverser.Result item in ravenIds) - { - string id = item.Property.GetValue(item.Parent, null) as string; - if (id.IsNullOrWhiteSpace()) - { - id = item.Item.Length.HasValue ? IdGenerator.Create(item.Item.Length.Value) : IdGenerator.Create(); - item.Property.SetValue(item.Parent, id); - } - } - - return model; + return IdGenerator.Autofill(model); }