From 6dcbc319ee58666be2bd7a07678e3826848a5b4c Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 14 Nov 2022 12:31:36 +0100 Subject: [PATCH] Query correct plan when checking for contributors. --- .../Apps/DomainObject/AppDomainObject.cs | 14 ++++++++------ .../Commands/InMemoryCommandBus.cs | 2 +- .../Apps/DomainObject/AppDomainObjectTests.cs | 18 ++++++++++-------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Entities/Apps/DomainObject/AppDomainObject.cs b/backend/src/Squidex.Domain.Apps.Entities/Apps/DomainObject/AppDomainObject.cs index 568aec528..3f8aee9e6 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Apps/DomainObject/AppDomainObject.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Apps/DomainObject/AppDomainObject.cs @@ -128,7 +128,9 @@ public partial class AppDomainObject : DomainObject case AssignContributor assignContributor: return UpdateReturnAsync(assignContributor, async (c, ct) => { - await GuardAppContributors.CanAssign(c, Snapshot, Users, Plan); + var (plan, _, _) = await UsageGate.GetPlanForAppAsync(Snapshot, false, ct); + + await GuardAppContributors.CanAssign(c, Snapshot, Users, plan); AssignContributor(c, !Snapshot.Contributors.ContainsKey(assignContributor.ContributorId)); @@ -480,6 +482,11 @@ public partial class AppDomainObject : DomainObject get => serviceProvider.GetRequiredService(); } + private IUsageGate UsageGate + { + get => serviceProvider.GetRequiredService(); + } + private IUserResolver Users { get => serviceProvider.GetRequiredService(); @@ -489,9 +496,4 @@ public partial class AppDomainObject : DomainObject { get => BillingPlans.GetFreePlan(); } - - private Plan Plan - { - get => BillingPlans.GetActualPlan(Snapshot.Plan?.PlanId).Plan; - } } diff --git a/backend/src/Squidex.Infrastructure/Commands/InMemoryCommandBus.cs b/backend/src/Squidex.Infrastructure/Commands/InMemoryCommandBus.cs index 1963f7565..80f8efbe3 100644 --- a/backend/src/Squidex.Infrastructure/Commands/InMemoryCommandBus.cs +++ b/backend/src/Squidex.Infrastructure/Commands/InMemoryCommandBus.cs @@ -17,7 +17,7 @@ public sealed class InMemoryCommandBus : ICommandBus NextDelegate next = (c, ct) => Task.CompletedTask; - foreach (var middleware in middlewares.Reverse()) + foreach (var middleware in reverseMiddlewares) { next = Create(next, middleware); } diff --git a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/DomainObject/AppDomainObjectTests.cs b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/DomainObject/AppDomainObjectTests.cs index bf4249bbf..da9f1325b 100644 --- a/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/DomainObject/AppDomainObjectTests.cs +++ b/backend/tests/Squidex.Domain.Apps.Entities.Tests/Apps/DomainObject/AppDomainObjectTests.cs @@ -30,6 +30,7 @@ public class AppDomainObjectTests : HandlerTestBase private readonly IBillingManager billingManager = A.Fake(); private readonly IUser user; private readonly IUserResolver userResolver = A.Fake(); + private readonly IUsageGate usageGate = A.Fake(); private readonly string contributorId = DomainId.NewGuid().ToString(); private readonly string clientId = "client"; private readonly string clientNewName = "My Client"; @@ -53,14 +54,14 @@ public class AppDomainObjectTests : HandlerTestBase A.CallTo(() => userResolver.FindByIdOrEmailAsync(contributorId, default)) .Returns(user); - A.CallTo(() => billingPlans.GetFreePlan()) - .Returns(new Plan { Id = planIdFree, MaxContributors = 10 }); + A.CallTo(() => usageGate.GetPlanForAppAsync(A.That.Matches(x => x.Plan != null && x.Plan.PlanId == planIdFree), false, default)) + .Returns((new Plan { Id = planIdFree, MaxContributors = 10 }, planIdFree, null)); - A.CallTo(() => billingPlans.GetPlan(planIdFree)) - .Returns(new Plan { Id = planIdFree, MaxContributors = 10 }); + A.CallTo(() => usageGate.GetPlanForAppAsync(A.That.Matches(x => x.Plan != null && x.Plan.PlanId == planIdPaid), false, default)) + .Returns((new Plan { Id = planIdPaid, MaxContributors = 30 }, planIdPaid, null)); - A.CallTo(() => billingPlans.GetPlan(planIdPaid)) - .Returns(new Plan { Id = planIdPaid, MaxContributors = 30 }); + A.CallTo(() => billingPlans.GetFreePlan()) + .Returns(new Plan { Id = planIdFree, MaxContributors = 10 }); A.CallTo(() => billingManager.MustRedirectToPortalAsync(Actor.Identifier, A._, A._, default)) .Returns(Task.FromResult(null)); @@ -80,9 +81,10 @@ public class AppDomainObjectTests : HandlerTestBase var serviceProvider = new ServiceCollection() .AddSingleton(appProvider) - .AddSingleton(initialSettings) - .AddSingleton(billingPlans) .AddSingleton(billingManager) + .AddSingleton(billingPlans) + .AddSingleton(initialSettings) + .AddSingleton(usageGate) .AddSingleton(userResolver) .BuildServiceProvider();