From 84f7cd262c976596bdc9c09ef85ebc9de3614b58 Mon Sep 17 00:00:00 2001 From: Jonas Hendrickx Date: Tue, 1 Oct 2024 11:08:10 +0200 Subject: [PATCH] [PM-12526] Can Reduce Org's PM seats to be lower than SM seats (#4796) --- .../Implementations/OrganizationService.cs | 17 +++++++++++------ .../Services/OrganizationServiceTests.cs | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Core/AdminConsole/Services/Implementations/OrganizationService.cs b/src/Core/AdminConsole/Services/Implementations/OrganizationService.cs index 6a0855c4ef..5c3f81cee2 100644 --- a/src/Core/AdminConsole/Services/Implementations/OrganizationService.cs +++ b/src/Core/AdminConsole/Services/Implementations/OrganizationService.cs @@ -358,6 +358,11 @@ public class OrganizationService : IOrganizationService } } + if (organization.UseSecretsManager && organization.Seats + seatAdjustment < organization.SmSeats) + { + throw new BadRequestException("You cannot have more Secrets Manager seats than Password Manager seats."); + } + var paymentIntentClientSecret = await _paymentService.AdjustSeatsAsync(organization, plan, additionalSeats); await _referenceEventService.RaiseEventAsync( new ReferenceEvent(ReferenceEventType.AdjustSeats, organization, _currentContext) @@ -1186,12 +1191,7 @@ public class OrganizationService : IOrganizationService var currentOrganization = await _organizationRepository.GetByIdAsync(organization.Id); // Revert autoscaling - if (initialSeatCount.HasValue && currentOrganization.Seats.HasValue && currentOrganization.Seats.Value != initialSeatCount.Value) - { - await AdjustSeatsAsync(organization, initialSeatCount.Value - currentOrganization.Seats.Value); - } - - // Revert SmSeat autoscaling + // Do this first so that SmSeats never exceed PM seats (due to current billing requirements) if (initialSmSeatCount.HasValue && currentOrganization.SmSeats.HasValue && currentOrganization.SmSeats.Value != initialSmSeatCount.Value) { @@ -1202,6 +1202,11 @@ public class OrganizationService : IOrganizationService await _updateSecretsManagerSubscriptionCommand.UpdateSubscriptionAsync(smSubscriptionUpdateRevert); } + if (initialSeatCount.HasValue && currentOrganization.Seats.HasValue && currentOrganization.Seats.Value != initialSeatCount.Value) + { + await AdjustSeatsAsync(organization, initialSeatCount.Value - currentOrganization.Seats.Value); + } + exceptions.Add(e); } diff --git a/test/Core.Test/AdminConsole/Services/OrganizationServiceTests.cs b/test/Core.Test/AdminConsole/Services/OrganizationServiceTests.cs index 4bef18f555..1193b2de8e 100644 --- a/test/Core.Test/AdminConsole/Services/OrganizationServiceTests.cs +++ b/test/Core.Test/AdminConsole/Services/OrganizationServiceTests.cs @@ -1866,6 +1866,20 @@ OrganizationUserInvite invite, SutProvider sutProvider) await Assert.ThrowsAsync(() => sutProvider.Sut.UpdateSubscription(organizationId, 0, null)); } + [Theory, SecretsManagerOrganizationCustomize] + [BitAutoData("You cannot have more Secrets Manager seats than Password Manager seats.", -1)] + public async Task UpdateSubscription_PmSeatAdjustmentLessThanSmSeats_Throws(string expectedMessage, + int seatAdjustment, Organization organization, SutProvider sutProvider) + { + organization.Seats = 100; + organization.SmSeats = 100; + + sutProvider.GetDependency().GetByIdAsync(organization.Id).Returns(organization); + + var actual = await Assert.ThrowsAsync(() => sutProvider.Sut.UpdateSubscription(organization.Id, seatAdjustment, null)); + Assert.Contains(expectedMessage, actual.Message); + } + [Theory, PaidOrganizationCustomize] [BitAutoData(0, 100, null, true, "")] [BitAutoData(0, 100, 100, true, "")]