-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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-12490] Extract OrganizationService.EnableAsync into commands #5321
Open
r-tome
wants to merge
12
commits into
main
Choose a base branch
from
ac/pm-12490/create-organization-enable-command
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+213
โ31
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f6a71b
Add organization enable command implementation
r-tome 85c408a
Add unit tests for OrganizationEnableCommand
r-tome f1f80a6
Add organization enable command registration for dependency injection
r-tome 95d0030
Refactor payment and subscription handlers to use IOrganizationEnableโฆ
r-tome 688f1d3
Remove EnableAsync methods from IOrganizationService and Organizationโฆ
r-tome 4c3c230
Merge branch 'main' into ac/pm-12490/create-organization-enable-command
r-tome 84ad415
Add xmldoc to IOrganizationEnableCommand
r-tome 56d8794
Merge branch 'main' into ac/pm-12490/create-organization-enable-command
r-tome 9a35fca
Merge branch 'main' into ac/pm-12490/create-organization-enable-command
r-tome f5faa78
Merge branch 'main' into ac/pm-12490/create-organization-enable-command
r-tome bf7871c
Refactor OrganizationEnableCommand to consolidate enable logic and adโฆ
r-tome ed483e9
Merge branch 'main' into ac/pm-12490/create-organization-enable-command
r-tome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
.../AdminConsole/OrganizationFeatures/Organizations/Interfaces/IOrganizationEnableCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
๏ปฟnamespace Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces; | ||
|
||
public interface IOrganizationEnableCommand | ||
{ | ||
/// <summary> | ||
/// Enables an organization if it is currently disabled. | ||
/// </summary> | ||
/// <param name="organizationId">The unique identifier of the organization to enable.</param> | ||
Task EnableAsync(Guid organizationId); | ||
|
||
/// <summary> | ||
/// Enables an organization with a specified expiration date if it is currently disabled and has a gateway configured. | ||
/// </summary> | ||
/// <param name="organizationId">The unique identifier of the organization to enable.</param> | ||
/// <param name="expirationDate">The optional expiration date when the organization's subscription will expire.</param> | ||
Task EnableAsync(Guid organizationId, DateTime? expirationDate); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Core/AdminConsole/OrganizationFeatures/Organizations/OrganizationEnableCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
๏ปฟusing Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces; | ||
using Bit.Core.Repositories; | ||
using Bit.Core.Services; | ||
|
||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Organizations; | ||
|
||
public class OrganizationEnableCommand : IOrganizationEnableCommand | ||
{ | ||
private readonly IApplicationCacheService _applicationCacheService; | ||
private readonly IOrganizationRepository _organizationRepository; | ||
|
||
public OrganizationEnableCommand( | ||
IApplicationCacheService applicationCacheService, | ||
IOrganizationRepository organizationRepository) | ||
{ | ||
_applicationCacheService = applicationCacheService; | ||
_organizationRepository = organizationRepository; | ||
} | ||
|
||
public async Task EnableAsync(Guid organizationId) | ||
{ | ||
var organization = await _organizationRepository.GetByIdAsync(organizationId); | ||
if (organization is { Enabled: false }) | ||
{ | ||
organization.Enabled = true; | ||
|
||
await _organizationRepository.ReplaceAsync(organization); | ||
await _applicationCacheService.UpsertOrganizationAbilityAsync(organization); | ||
} | ||
} | ||
|
||
public async Task EnableAsync(Guid organizationId, DateTime? expirationDate) | ||
{ | ||
var organization = await _organizationRepository.GetByIdAsync(organizationId); | ||
if (organization is { Enabled: false, Gateway: not null }) | ||
{ | ||
organization.Enabled = true; | ||
organization.ExpirationDate = expirationDate; | ||
organization.RevisionDate = DateTime.UtcNow; | ||
|
||
await _organizationRepository.ReplaceAsync(organization); | ||
await _applicationCacheService.UpsertOrganizationAbilityAsync(organization); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
...re.Test/AdminConsole/OrganizationFeatures/Organizations/OrganizationEnableCommandTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
๏ปฟusing Bit.Core.AdminConsole.Entities; | ||
using Bit.Core.AdminConsole.OrganizationFeatures.Organizations; | ||
using Bit.Core.Enums; | ||
using Bit.Core.Repositories; | ||
using Bit.Core.Services; | ||
using Bit.Test.Common.AutoFixture; | ||
using Bit.Test.Common.AutoFixture.Attributes; | ||
using NSubstitute; | ||
using Xunit; | ||
|
||
namespace Bit.Core.Test.AdminConsole.OrganizationFeatures.Organizations; | ||
|
||
[SutProviderCustomize] | ||
public class OrganizationEnableCommandTests | ||
{ | ||
[Theory, BitAutoData] | ||
public async Task EnableAsync_WhenOrganizationDoesNotExist_DoesNothing( | ||
Guid organizationId, | ||
SutProvider<OrganizationEnableCommand> sutProvider) | ||
{ | ||
sutProvider.GetDependency<IOrganizationRepository>() | ||
.GetByIdAsync(organizationId) | ||
.Returns((Organization)null); | ||
|
||
await sutProvider.Sut.EnableAsync(organizationId); | ||
|
||
await sutProvider.GetDependency<IOrganizationRepository>() | ||
.DidNotReceive() | ||
.ReplaceAsync(Arg.Any<Organization>()); | ||
await sutProvider.GetDependency<IApplicationCacheService>() | ||
.DidNotReceive() | ||
.UpsertOrganizationAbilityAsync(Arg.Any<Organization>()); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task EnableAsync_WhenOrganizationAlreadyEnabled_DoesNothing( | ||
Organization organization, | ||
SutProvider<OrganizationEnableCommand> sutProvider) | ||
{ | ||
organization.Enabled = true; | ||
sutProvider.GetDependency<IOrganizationRepository>() | ||
.GetByIdAsync(organization.Id) | ||
.Returns(organization); | ||
|
||
await sutProvider.Sut.EnableAsync(organization.Id); | ||
|
||
await sutProvider.GetDependency<IOrganizationRepository>() | ||
.DidNotReceive() | ||
.ReplaceAsync(Arg.Any<Organization>()); | ||
await sutProvider.GetDependency<IApplicationCacheService>() | ||
.DidNotReceive() | ||
.UpsertOrganizationAbilityAsync(Arg.Any<Organization>()); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task EnableAsync_WhenOrganizationDisabled_EnablesAndSaves( | ||
Organization organization, | ||
SutProvider<OrganizationEnableCommand> sutProvider) | ||
{ | ||
organization.Enabled = false; | ||
sutProvider.GetDependency<IOrganizationRepository>() | ||
.GetByIdAsync(organization.Id) | ||
.Returns(organization); | ||
|
||
await sutProvider.Sut.EnableAsync(organization.Id); | ||
|
||
Assert.True(organization.Enabled); | ||
await sutProvider.GetDependency<IOrganizationRepository>() | ||
.Received(1) | ||
.ReplaceAsync(organization); | ||
await sutProvider.GetDependency<IApplicationCacheService>() | ||
.Received(1) | ||
.UpsertOrganizationAbilityAsync(organization); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task EnableAsync_WithExpiration_WhenOrganizationHasNoGateway_DoesNothing( | ||
Organization organization, | ||
DateTime expirationDate, | ||
SutProvider<OrganizationEnableCommand> sutProvider) | ||
{ | ||
organization.Enabled = false; | ||
organization.Gateway = null; | ||
sutProvider.GetDependency<IOrganizationRepository>() | ||
.GetByIdAsync(organization.Id) | ||
.Returns(organization); | ||
|
||
await sutProvider.Sut.EnableAsync(organization.Id, expirationDate); | ||
|
||
await sutProvider.GetDependency<IOrganizationRepository>() | ||
.DidNotReceive() | ||
.ReplaceAsync(Arg.Any<Organization>()); | ||
JimmyVo16 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await sutProvider.GetDependency<IApplicationCacheService>() | ||
.DidNotReceive() | ||
.UpsertOrganizationAbilityAsync(Arg.Any<Organization>()); | ||
} | ||
|
||
[Theory, BitAutoData] | ||
public async Task EnableAsync_WithExpiration_WhenValid_EnablesAndSetsExpiration( | ||
Organization organization, | ||
DateTime expirationDate, | ||
SutProvider<OrganizationEnableCommand> sutProvider) | ||
{ | ||
organization.Enabled = false; | ||
organization.Gateway = GatewayType.Stripe; | ||
organization.RevisionDate = DateTime.UtcNow.AddDays(-1); | ||
var originalRevisionDate = organization.RevisionDate; | ||
sutProvider.GetDependency<IOrganizationRepository>() | ||
.GetByIdAsync(organization.Id) | ||
.Returns(organization); | ||
|
||
await sutProvider.Sut.EnableAsync(organization.Id, expirationDate); | ||
|
||
Assert.True(organization.Enabled); | ||
Assert.Equal(expirationDate, organization.ExpirationDate); | ||
Assert.True(organization.RevisionDate > originalRevisionDate); | ||
await sutProvider.GetDependency<IOrganizationRepository>() | ||
.Received(1) | ||
.ReplaceAsync(organization); | ||
await sutProvider.GetDependency<IApplicationCacheService>() | ||
.Received(1) | ||
.UpsertOrganizationAbilityAsync(organization); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just leaving a non-blocking drive-by-nit โ๏ธ before approving! Looking at these two methods, it feels like they can be combined into a single method. I don't have all the context around this, so there's probably a reason they're not! Something like this is what I was imagining:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good thinking! I've pushed a commit with your suggestion. I also added an extra unit test just to make sure we're not breaking anything.