mirror of
https://github.com/bitwarden/server
synced 2025-12-11 05:43:35 +00:00
[PM-20452] - Offloading Stripe Update (#6034)
* Adding job to update stripe subscriptions and increment seat count when inviting a user. * Updating name * Added ef migrations * Fixing script * Fixing procedures. Added repo tests. * Fixed set stored procedure. Fixed parameter name. * Added tests for database calls and updated stored procedures * Fixed build for sql file. * fixing sproc * File is nullsafe * Adding view to select from instead of table. * Updating UpdateSubscriptionStatus to use a CTE and do all the updates in 1 statement. * Setting revision date when incrementing seat count * Added feature flag check for the background job. * Fixing nullable property. * Removing new table and just adding the column to org. Updating to query and command. Updated tests. * Adding migration script rename * Add SyncSeats to Org.sql def * Adding contraint name * Removing old table files. * Added tests * Upped the frequency to be at the top of every 3rd hour. * Updating error message. * Removing extension method * Changed to GuidIdArray * Added xml doc and switched class to record
This commit is contained in:
@@ -31,13 +31,15 @@ public static class OrganizationTestHelpers
|
||||
/// Creates an Enterprise organization.
|
||||
/// </summary>
|
||||
public static Task<Organization> CreateTestOrganizationAsync(this IOrganizationRepository organizationRepository,
|
||||
int? seatCount = null,
|
||||
string identifier = "test")
|
||||
=> organizationRepository.CreateAsync(new Organization
|
||||
{
|
||||
Name = $"{identifier}-{Guid.NewGuid()}",
|
||||
BillingEmail = "billing@example.com", // TODO: EF does not enforce this being NOT NULL
|
||||
Plan = "Enterprise (Annually)", // TODO: EF does not enforce this being NOT NULl
|
||||
PlanType = PlanType.EnterpriseAnnually
|
||||
PlanType = PlanType.EnterpriseAnnually,
|
||||
Seats = seatCount
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -423,4 +423,111 @@ public class OrganizationRepositoryTests
|
||||
Assert.Equal(0, result.Sponsored);
|
||||
Assert.Equal(0, result.Total);
|
||||
}
|
||||
|
||||
[DatabaseTheory, DatabaseData]
|
||||
public async Task IncrementSeatCountAsync_IncrementsSeatCount(IOrganizationRepository organizationRepository)
|
||||
{
|
||||
var organization = await organizationRepository.CreateTestOrganizationAsync();
|
||||
organization.Seats = 5;
|
||||
await organizationRepository.ReplaceAsync(organization);
|
||||
|
||||
await organizationRepository.IncrementSeatCountAsync(organization.Id, 3, DateTime.UtcNow);
|
||||
|
||||
var result = await organizationRepository.GetByIdAsync(organization.Id);
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(8, result.Seats);
|
||||
}
|
||||
|
||||
[DatabaseData, DatabaseTheory]
|
||||
public async Task IncrementSeatCountAsync_GivenOrganizationHasNotChangedSeatCountBefore_WhenUpdatingOrgSeats_ThenSubscriptionUpdateIsSaved(
|
||||
IOrganizationRepository sutRepository)
|
||||
{
|
||||
// Arrange
|
||||
var organization = await sutRepository.CreateTestOrganizationAsync(seatCount: 2);
|
||||
var requestDate = DateTime.UtcNow;
|
||||
|
||||
// Act
|
||||
await sutRepository.IncrementSeatCountAsync(organization.Id, 1, requestDate);
|
||||
|
||||
// Assert
|
||||
var result = (await sutRepository.GetOrganizationsForSubscriptionSyncAsync()).ToArray();
|
||||
|
||||
var updateResult = result.FirstOrDefault(x => x.Id == organization.Id);
|
||||
Assert.NotNull(updateResult);
|
||||
Assert.Equal(organization.Id, updateResult.Id);
|
||||
Assert.True(updateResult.SyncSeats);
|
||||
Assert.Equal(requestDate.ToString("yyyy-MM-dd HH:mm:ss"), updateResult.RevisionDate.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
// Annul
|
||||
await sutRepository.DeleteAsync(organization);
|
||||
}
|
||||
|
||||
[DatabaseData, DatabaseTheory]
|
||||
public async Task IncrementSeatCountAsync_GivenOrganizationHasChangedSeatCountBeforeAndRecordExists_WhenUpdatingOrgSeats_ThenSubscriptionUpdateIsSaved(
|
||||
IOrganizationRepository sutRepository)
|
||||
{
|
||||
// Arrange
|
||||
var organization = await sutRepository.CreateTestOrganizationAsync(seatCount: 2);
|
||||
await sutRepository.IncrementSeatCountAsync(organization.Id, 1, DateTime.UtcNow);
|
||||
|
||||
var requestDate = DateTime.UtcNow;
|
||||
|
||||
// Act
|
||||
await sutRepository.IncrementSeatCountAsync(organization.Id, 1, DateTime.UtcNow);
|
||||
|
||||
// Assert
|
||||
var result = (await sutRepository.GetOrganizationsForSubscriptionSyncAsync()).ToArray();
|
||||
var updateResult = result.FirstOrDefault(x => x.Id == organization.Id);
|
||||
Assert.NotNull(updateResult);
|
||||
Assert.Equal(organization.Id, updateResult.Id);
|
||||
Assert.True(updateResult.SyncSeats);
|
||||
Assert.Equal(requestDate.ToString("yyyy-MM-dd HH:mm:ss"), updateResult.RevisionDate.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
// Annul
|
||||
await sutRepository.DeleteAsync(organization);
|
||||
}
|
||||
|
||||
[DatabaseData, DatabaseTheory]
|
||||
public async Task GetOrganizationsForSubscriptionSyncAsync_GivenOrganizationHasChangedSeatCount_WhenGettingOrgsToUpdate_ThenReturnsOrgSubscriptionUpdate(
|
||||
IOrganizationRepository sutRepository)
|
||||
{
|
||||
// Arrange
|
||||
var organization = await sutRepository.CreateTestOrganizationAsync(seatCount: 2);
|
||||
var requestDate = DateTime.UtcNow;
|
||||
await sutRepository.IncrementSeatCountAsync(organization.Id, 1, requestDate);
|
||||
|
||||
// Act
|
||||
var result = (await sutRepository.GetOrganizationsForSubscriptionSyncAsync()).ToArray();
|
||||
|
||||
// Assert
|
||||
var updateResult = result.FirstOrDefault(x => x.Id == organization.Id);
|
||||
Assert.NotNull(updateResult);
|
||||
Assert.Equal(organization.Id, updateResult.Id);
|
||||
Assert.True(updateResult.SyncSeats);
|
||||
Assert.Equal(requestDate.ToString("yyyy-MM-dd HH:mm:ss"), updateResult.RevisionDate.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
// Annul
|
||||
await sutRepository.DeleteAsync(organization);
|
||||
}
|
||||
|
||||
[DatabaseData, DatabaseTheory]
|
||||
public async Task UpdateSuccessfulOrganizationSyncStatusAsync_GivenOrganizationHasChangedSeatCount_WhenUpdatingStatus_ThenSuccessfullyUpdatesOrgSoItDoesntSync(
|
||||
IOrganizationRepository sutRepository)
|
||||
{
|
||||
// Arrange
|
||||
var organization = await sutRepository.CreateTestOrganizationAsync(seatCount: 2);
|
||||
var requestDate = DateTime.UtcNow;
|
||||
var syncDate = DateTime.UtcNow.AddMinutes(1);
|
||||
await sutRepository.IncrementSeatCountAsync(organization.Id, 1, requestDate);
|
||||
|
||||
// Act
|
||||
await sutRepository.UpdateSuccessfulOrganizationSyncStatusAsync([organization.Id], syncDate);
|
||||
|
||||
// Assert
|
||||
var result = (await sutRepository.GetOrganizationsForSubscriptionSyncAsync()).ToArray();
|
||||
Assert.Null(result.FirstOrDefault(x => x.Id == organization.Id));
|
||||
|
||||
// Annul
|
||||
await sutRepository.DeleteAsync(organization);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user