Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-14275] Add IsManaged to OrganizationMetadata #4957

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,5 @@
๏ปฟusing Bit.Core.AdminConsole.Entities;
using Bit.Core.AdminConsole.Repositories;

Check warning on line 2 in src/Core/Billing/Services/Implementations/OrganizationBillingService.cs

View workflow job for this annotation

GitHub Actions / Lint

Using directive is unnecessary.
using Bit.Core.Billing.Caches;
using Bit.Core.Billing.Constants;
using Bit.Core.Billing.Models;
Expand Down Expand Up @@ -27,7 +27,6 @@
IGlobalSettings globalSettings,
ILogger<OrganizationBillingService> logger,
IOrganizationRepository organizationRepository,
IProviderRepository providerRepository,
ISetupIntentCache setupIntentCache,
IStripeAdapter stripeAdapter,
ISubscriberService subscriberService) : IOrganizationBillingService
Expand Down Expand Up @@ -71,11 +70,11 @@

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 +338,12 @@
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
Loading