1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-21 12:05:42 +01:00

[PM-13834] Skip providers that have no clients during migration (#4913)

* Skip providers that have no clients during migration

* Remove enabled requirement from migrator
This commit is contained in:
Alex Morask 2024-10-21 08:54:15 -04:00 committed by GitHub
parent 5d15750b80
commit f61a017c17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 21 deletions

View File

@ -3,17 +3,14 @@
public enum ProviderMigrationProgress public enum ProviderMigrationProgress
{ {
Started = 1, Started = 1,
ClientsMigrated = 2, NoClients = 2,
TeamsPlanConfigured = 3, ClientsMigrated = 3,
EnterprisePlanConfigured = 4, TeamsPlanConfigured = 4,
CustomerSetup = 5, EnterprisePlanConfigured = 5,
SubscriptionSetup = 6, CustomerSetup = 6,
CreditApplied = 7, SubscriptionSetup = 7,
Completed = 8, CreditApplied = 8,
Completed = 9,
Reversing = 9,
ReversedClientMigrations = 10,
RemovedProviderPlans = 11
} }
public class ProviderMigrationTracker public class ProviderMigrationTracker

View File

@ -41,7 +41,18 @@ public class ProviderMigrator(
await migrationTrackerCache.StartTracker(provider); await migrationTrackerCache.StartTracker(provider);
await MigrateClientsAsync(providerId); var organizations = await GetClientsAsync(provider.Id);
if (organizations.Count == 0)
{
logger.LogInformation("CB: Skipping migration for provider ({ProviderID}) with no clients", providerId);
await migrationTrackerCache.UpdateTrackingStatus(providerId, ProviderMigrationProgress.NoClients);
return;
}
await MigrateClientsAsync(providerId, organizations);
await ConfigureTeamsPlanAsync(providerId); await ConfigureTeamsPlanAsync(providerId);
@ -65,6 +76,16 @@ public class ProviderMigrator(
return null; return null;
} }
if (providerTracker.Progress == ProviderMigrationProgress.NoClients)
{
return new ProviderMigrationResult
{
ProviderId = providerTracker.ProviderId,
ProviderName = providerTracker.ProviderName,
Result = providerTracker.Progress.ToString()
};
}
var clientTrackers = await Task.WhenAll(providerTracker.OrganizationIds.Select(organizationId => var clientTrackers = await Task.WhenAll(providerTracker.OrganizationIds.Select(organizationId =>
migrationTrackerCache.GetTracker(providerId, organizationId))); migrationTrackerCache.GetTracker(providerId, organizationId)));
@ -99,12 +120,10 @@ public class ProviderMigrator(
#region Steps #region Steps
private async Task MigrateClientsAsync(Guid providerId) private async Task MigrateClientsAsync(Guid providerId, List<Organization> organizations)
{ {
logger.LogInformation("CB: Migrating clients for provider ({ProviderID})", providerId); logger.LogInformation("CB: Migrating clients for provider ({ProviderID})", providerId);
var organizations = await GetEnabledClientsAsync(providerId);
var organizationIds = organizations.Select(organization => organization.Id); var organizationIds = organizations.Select(organization => organization.Id);
await migrationTrackerCache.SetOrganizationIds(providerId, organizationIds); await migrationTrackerCache.SetOrganizationIds(providerId, organizationIds);
@ -129,7 +148,7 @@ public class ProviderMigrator(
{ {
logger.LogInformation("CB: Configuring Teams plan for provider ({ProviderID})", providerId); logger.LogInformation("CB: Configuring Teams plan for provider ({ProviderID})", providerId);
var organizations = await GetEnabledClientsAsync(providerId); var organizations = await GetClientsAsync(providerId);
var teamsSeats = organizations var teamsSeats = organizations
.Where(IsTeams) .Where(IsTeams)
@ -172,7 +191,7 @@ public class ProviderMigrator(
{ {
logger.LogInformation("CB: Configuring Enterprise plan for provider ({ProviderID})", providerId); logger.LogInformation("CB: Configuring Enterprise plan for provider ({ProviderID})", providerId);
var organizations = await GetEnabledClientsAsync(providerId); var organizations = await GetClientsAsync(providerId);
var enterpriseSeats = organizations var enterpriseSeats = organizations
.Where(IsEnterprise) .Where(IsEnterprise)
@ -215,7 +234,7 @@ public class ProviderMigrator(
{ {
if (string.IsNullOrEmpty(provider.GatewayCustomerId)) if (string.IsNullOrEmpty(provider.GatewayCustomerId))
{ {
var organizations = await GetEnabledClientsAsync(provider.Id); var organizations = await GetClientsAsync(provider.Id);
var sampleOrganization = organizations.FirstOrDefault(organization => !string.IsNullOrEmpty(organization.GatewayCustomerId)); var sampleOrganization = organizations.FirstOrDefault(organization => !string.IsNullOrEmpty(organization.GatewayCustomerId));
@ -299,7 +318,7 @@ public class ProviderMigrator(
private async Task ApplyCreditAsync(Provider provider) private async Task ApplyCreditAsync(Provider provider)
{ {
var organizations = await GetEnabledClientsAsync(provider.Id); var organizations = await GetClientsAsync(provider.Id);
var organizationCustomers = var organizationCustomers =
await Task.WhenAll(organizations.Select(organization => stripeAdapter.CustomerGetAsync(organization.GatewayCustomerId))); await Task.WhenAll(organizations.Select(organization => stripeAdapter.CustomerGetAsync(organization.GatewayCustomerId)));
@ -355,13 +374,12 @@ public class ProviderMigrator(
#region Utilities #region Utilities
private async Task<List<Organization>> GetEnabledClientsAsync(Guid providerId) private async Task<List<Organization>> GetClientsAsync(Guid providerId)
{ {
var providerOrganizations = await providerOrganizationRepository.GetManyDetailsByProviderAsync(providerId); var providerOrganizations = await providerOrganizationRepository.GetManyDetailsByProviderAsync(providerId);
return (await Task.WhenAll(providerOrganizations.Select(providerOrganization => return (await Task.WhenAll(providerOrganizations.Select(providerOrganization =>
organizationRepository.GetByIdAsync(providerOrganization.OrganizationId)))) organizationRepository.GetByIdAsync(providerOrganization.OrganizationId))))
.Where(organization => organization.Enabled)
.ToList(); .ToList();
} }