Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashendrickx committed Nov 5, 2024
1 parent 2b57bc7 commit 9616f8c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ public async Task AddOrganization(Guid providerId, Guid organizationId, string k

var organization = await _organizationRepository.GetByIdAsync(organizationId);

ThrowOnInvalidPlanType(organization.PlanType);
var provider = await _providerRepository.GetByIdAsync(providerId);

ThrowOnInvalidPlanType(provider.Type, organization.PlanType);

if (organization.UseSecretsManager)
{
Expand All @@ -407,8 +409,6 @@ public async Task AddOrganization(Guid providerId, Guid organizationId, string k
Key = key,
};

var provider = await _providerRepository.GetByIdAsync(providerId);

await ApplyProviderPriceRateAsync(organization, provider);
await _providerOrganizationRepository.CreateAsync(providerOrganization);

Expand Down Expand Up @@ -547,7 +547,7 @@ public async Task<ProviderOrganization> CreateOrganizationAsync(Guid providerId,

var consolidatedBillingEnabled = _featureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling) && provider.IsBillable();

ThrowOnInvalidPlanType(organizationSignup.Plan, consolidatedBillingEnabled);
ThrowOnInvalidPlanType(provider.Type, organizationSignup.Plan, consolidatedBillingEnabled);

var (organization, _, defaultCollection) = consolidatedBillingEnabled
? await _organizationService.SignupClientAsync(organizationSignup)
Expand Down Expand Up @@ -687,11 +687,27 @@ private async Task<bool> HasConfirmedProviderAdminExceptAsync(Guid providerId, I
return confirmedOwnersIds.Except(providerUserIds).Any();
}

private void ThrowOnInvalidPlanType(PlanType requestedType, bool consolidatedBillingEnabled = false)
private void ThrowOnInvalidPlanType(ProviderType providerType, PlanType requestedType, bool consolidatedBillingEnabled = false)
{
if (consolidatedBillingEnabled && requestedType is not (PlanType.TeamsMonthly or PlanType.EnterpriseMonthly))
if (consolidatedBillingEnabled)
{
throw new BadRequestException($"Providers cannot manage organizations with the plan type {requestedType}. Only Teams (Monthly) and Enterprise (Monthly) are allowed.");
switch (providerType)
{
case ProviderType.Msp:
if (requestedType is not (PlanType.TeamsMonthly or PlanType.EnterpriseMonthly))
{
throw new BadRequestException($"Managed Service Providers cannot manage organizations with the plan type {requestedType}. Only Teams (Monthly) and Enterprise (Monthly) are allowed.");
}
break;
case ProviderType.MultiOrganizationEnterprise:
if (requestedType is not (PlanType.EnterpriseMonthly or PlanType.EnterpriseAnnually))
{
throw new BadRequestException($"Multi-organization Enterprise Providers cannot manage organizations with the plan type {requestedType}. Only Enterprise (Monthly) and Enterprise (Annually) are allowed.");

Check warning on line 705 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L704-L705

Added lines #L704 - L705 were not covered by tests
}
break;

Check warning on line 707 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L707

Added line #L707 was not covered by tests
default:
throw new BadRequestException($"Unsupported provider type {providerType}.");

Check warning on line 709 in bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs

View check run for this annotation

Codecov / codecov/patch

bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L709

Added line #L709 was not covered by tests
}
}

if (ProviderDisallowedOrganizationTypes.Contains(requestedType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CreateClientOrganizationRequestBody
[Required(ErrorMessage = "'ownerEmail' must be provided")]
public string OwnerEmail { get; set; }

[EnumMatches<PlanType>(PlanType.TeamsMonthly, PlanType.EnterpriseMonthly, ErrorMessage = "'planType' must be Teams (Monthly) or Enterprise (Monthly)")]
[EnumMatches<PlanType>(PlanType.TeamsMonthly, PlanType.EnterpriseMonthly, PlanType.EnterpriseAnnually, ErrorMessage = "'planType' must be Teams (Monthly), Enterprise (Monthly) or Enterprise (Annually)")]
public PlanType PlanType { get; set; }

[Range(1, int.MaxValue, ErrorMessage = "'seats' must be greater than 0")]
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Billing/Extensions/BillingExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ setupIntent is
};

public static bool SupportsConsolidatedBilling(this PlanType planType)
=> planType is PlanType.TeamsMonthly or PlanType.EnterpriseMonthly;
=> planType is PlanType.TeamsMonthly or PlanType.EnterpriseMonthly or PlanType.EnterpriseAnnually;
}

0 comments on commit 9616f8c

Please sign in to comment.