From 4755279adf938f4f6307ba28453484d4172a8d38 Mon Sep 17 00:00:00 2001 From: Alex Morask <144709477+amorask-bitwarden@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:01:37 -0400 Subject: [PATCH] [PM-14275] Add IsManaged to OrganizationMetadata (#4957) * Add IsManaged to OrganizationMetadata * Remove subscription requirement from self-host eligibility check * Remove unused service * Run dotnet format --- .../Responses/OrganizationMetadataResponse.cs | 2 ++ .../Billing/Models/OrganizationMetadata.cs | 8 ++--- .../OrganizationBillingService.cs | 30 +++++-------------- .../OrganizationBillingControllerTests.cs | 9 +++--- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/Api/Billing/Models/Responses/OrganizationMetadataResponse.cs b/src/Api/Billing/Models/Responses/OrganizationMetadataResponse.cs index b5f9ab2f5910..ebaf7b548aa2 100644 --- a/src/Api/Billing/Models/Responses/OrganizationMetadataResponse.cs +++ b/src/Api/Billing/Models/Responses/OrganizationMetadataResponse.cs @@ -4,10 +4,12 @@ namespace Bit.Api.Billing.Models.Responses; public record OrganizationMetadataResponse( bool IsEligibleForSelfHost, + bool IsManaged, bool IsOnSecretsManagerStandalone) { public static OrganizationMetadataResponse From(OrganizationMetadata metadata) => new( metadata.IsEligibleForSelfHost, + metadata.IsManaged, metadata.IsOnSecretsManagerStandalone); } diff --git a/src/Core/Billing/Models/OrganizationMetadata.cs b/src/Core/Billing/Models/OrganizationMetadata.cs index 136964d7c10a..6b31b51d0219 100644 --- a/src/Core/Billing/Models/OrganizationMetadata.cs +++ b/src/Core/Billing/Models/OrganizationMetadata.cs @@ -2,9 +2,5 @@ public record OrganizationMetadata( bool IsEligibleForSelfHost, - bool IsOnSecretsManagerStandalone) -{ - public static OrganizationMetadata Default() => new( - IsEligibleForSelfHost: false, - IsOnSecretsManagerStandalone: false); -} + bool IsManaged, + bool IsOnSecretsManagerStandalone); diff --git a/src/Core/Billing/Services/Implementations/OrganizationBillingService.cs b/src/Core/Billing/Services/Implementations/OrganizationBillingService.cs index 7db886203296..5956e3d85cf0 100644 --- a/src/Core/Billing/Services/Implementations/OrganizationBillingService.cs +++ b/src/Core/Billing/Services/Implementations/OrganizationBillingService.cs @@ -1,5 +1,4 @@ using Bit.Core.AdminConsole.Entities; -using Bit.Core.AdminConsole.Repositories; using Bit.Core.Billing.Caches; using Bit.Core.Billing.Constants; using Bit.Core.Billing.Models; @@ -27,7 +26,6 @@ public class OrganizationBillingService( IGlobalSettings globalSettings, ILogger logger, IOrganizationRepository organizationRepository, - IProviderRepository providerRepository, ISetupIntentCache setupIntentCache, IStripeAdapter stripeAdapter, ISubscriberService subscriberService) : IOrganizationBillingService @@ -71,11 +69,11 @@ public async Task Finalize(OrganizationSale sale) var subscription = await subscriberService.GetSubscription(organization); - var isEligibleForSelfHost = await IsEligibleForSelfHost(organization, subscription); - + var isEligibleForSelfHost = IsEligibleForSelfHost(organization); + var isManaged = organization.Status == OrganizationStatusType.Managed; var isOnSecretsManagerStandalone = IsOnSecretsManagerStandalone(organization, customer, subscription); - return new OrganizationMetadata(isEligibleForSelfHost, isOnSecretsManagerStandalone); + return new OrganizationMetadata(isEligibleForSelfHost, isManaged, isOnSecretsManagerStandalone); } public async Task UpdatePaymentMethod( @@ -339,26 +337,12 @@ private async Task CreateSubscriptionAsync( return await stripeAdapter.SubscriptionCreateAsync(subscriptionCreateOptions); } - private async Task IsEligibleForSelfHost( - Organization organization, - Subscription? organizationSubscription) + private static bool IsEligibleForSelfHost( + Organization organization) { - if (organization.Status != OrganizationStatusType.Managed) - { - return organization.Plan.Contains("Families") || - organization.Plan.Contains("Enterprise") && IsActive(organizationSubscription); - } - - var provider = await providerRepository.GetByOrganizationIdAsync(organization.Id); - - var providerSubscription = await subscriberService.GetSubscriptionOrThrow(provider); - - return organization.Plan.Contains("Enterprise") && IsActive(providerSubscription); + var eligibleSelfHostPlans = StaticStore.Plans.Where(plan => plan.HasSelfHost).Select(plan => plan.Type); - bool IsActive(Subscription? subscription) => subscription?.Status is - StripeConstants.SubscriptionStatus.Active or - StripeConstants.SubscriptionStatus.Trialing or - StripeConstants.SubscriptionStatus.PastDue; + return eligibleSelfHostPlans.Contains(organization.PlanType); } private static bool IsOnSecretsManagerStandalone( diff --git a/test/Api.Test/Billing/Controllers/OrganizationBillingControllerTests.cs b/test/Api.Test/Billing/Controllers/OrganizationBillingControllerTests.cs index b46fd307e96b..eadf7e97b86e 100644 --- a/test/Api.Test/Billing/Controllers/OrganizationBillingControllerTests.cs +++ b/test/Api.Test/Billing/Controllers/OrganizationBillingControllerTests.cs @@ -52,16 +52,17 @@ public async Task GetMetadataAsync_OK( { sutProvider.GetDependency().AccessMembersTab(organizationId).Returns(true); sutProvider.GetDependency().GetMetadata(organizationId) - .Returns(new OrganizationMetadata(true, true)); + .Returns(new OrganizationMetadata(true, true, true)); var result = await sutProvider.Sut.GetMetadataAsync(organizationId); Assert.IsType>(result); - var organizationMetadataResponse = ((Ok)result).Value; + var response = ((Ok)result).Value; - Assert.True(organizationMetadataResponse.IsEligibleForSelfHost); - Assert.True(organizationMetadataResponse.IsOnSecretsManagerStandalone); + Assert.True(response.IsEligibleForSelfHost); + Assert.True(response.IsManaged); + Assert.True(response.IsOnSecretsManagerStandalone); } [Theory, BitAutoData]