Skip to content

Commit

Permalink
[PM-14275] Add IsManaged to OrganizationMetadata (#4957)
Browse files Browse the repository at this point in the history
* Add IsManaged to OrganizationMetadata

* Remove subscription requirement from self-host eligibility check

* Remove unused service

* Run dotnet format
  • Loading branch information
amorask-bitwarden committed Oct 31, 2024
1 parent 75cc907 commit 4755279
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 2 additions & 6 deletions src/Core/Billing/Models/OrganizationMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@

public record OrganizationMetadata(
bool IsEligibleForSelfHost,
bool IsOnSecretsManagerStandalone)
{
public static OrganizationMetadata Default() => new(
IsEligibleForSelfHost: false,
IsOnSecretsManagerStandalone: false);
}
bool IsManaged,
bool IsOnSecretsManagerStandalone);
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -27,7 +26,6 @@ public class OrganizationBillingService(
IGlobalSettings globalSettings,
ILogger<OrganizationBillingService> logger,
IOrganizationRepository organizationRepository,
IProviderRepository providerRepository,
ISetupIntentCache setupIntentCache,
IStripeAdapter stripeAdapter,
ISubscriberService subscriberService) : IOrganizationBillingService
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -339,26 +337,12 @@ private async Task<Subscription> CreateSubscriptionAsync(
return await stripeAdapter.SubscriptionCreateAsync(subscriptionCreateOptions);
}

private async Task<bool> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,17 @@ public async Task GetMetadataAsync_OK(
{
sutProvider.GetDependency<ICurrentContext>().AccessMembersTab(organizationId).Returns(true);
sutProvider.GetDependency<IOrganizationBillingService>().GetMetadata(organizationId)
.Returns(new OrganizationMetadata(true, true));
.Returns(new OrganizationMetadata(true, true, true));

var result = await sutProvider.Sut.GetMetadataAsync(organizationId);

Assert.IsType<Ok<OrganizationMetadataResponse>>(result);

var organizationMetadataResponse = ((Ok<OrganizationMetadataResponse>)result).Value;
var response = ((Ok<OrganizationMetadataResponse>)result).Value;

Assert.True(organizationMetadataResponse.IsEligibleForSelfHost);
Assert.True(organizationMetadataResponse.IsOnSecretsManagerStandalone);
Assert.True(response.IsEligibleForSelfHost);
Assert.True(response.IsManaged);
Assert.True(response.IsOnSecretsManagerStandalone);
}

[Theory, BitAutoData]
Expand Down

0 comments on commit 4755279

Please sign in to comment.