1
0
mirror of https://github.com/bitwarden/server synced 2026-01-09 03:53:42 +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:
Jared McCannon
2025-07-31 07:54:51 -05:00
committed by GitHub
parent 88dd977848
commit 86ce3a86e9
38 changed files with 10968 additions and 43 deletions

View File

@@ -123,6 +123,11 @@ public class Organization : ITableObject<Guid>, IStorableSubscriber, IRevisable
/// </summary>
public bool UseAdminSponsoredFamilies { get; set; }
/// <summary>
/// If set to true, organization needs their seat count synced with their subscription
/// </summary>
public bool SyncSeats { get; set; }
public void SetNewId()
{
if (Id == default(Guid))

View File

@@ -0,0 +1,11 @@
using Bit.Core.AdminConsole.Entities;
using Bit.Core.Models.StaticStore;
namespace Bit.Core.AdminConsole.Models.Data.Organizations;
public record OrganizationSubscriptionUpdate
{
public required Organization Organization { get; set; }
public int Seats => Organization.Seats ?? 0;
public Plan? Plan { get; set; }
}

View File

@@ -25,7 +25,6 @@ namespace Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUse
public class InviteOrganizationUsersCommand(IEventService eventService,
IOrganizationUserRepository organizationUserRepository,
IInviteUsersValidator inviteUsersValidator,
IPaymentService paymentService,
IOrganizationRepository organizationRepository,
IApplicationCacheService applicationCacheService,
IMailService mailService,
@@ -190,12 +189,6 @@ public class InviteOrganizationUsersCommand(IEventService eventService,
{
if (validatedResult.Value.PasswordManagerSubscriptionUpdate is { Seats: > 0, SeatsRequiredToAdd: > 0 })
{
await paymentService.AdjustSeatsAsync(organization,
validatedResult.Value.InviteOrganization.Plan,
validatedResult.Value.PasswordManagerSubscriptionUpdate.Seats.Value);
organization.Seats = (short?)validatedResult.Value.PasswordManagerSubscriptionUpdate.Seats;
await organizationRepository.ReplaceAsync(organization);
@@ -297,13 +290,14 @@ public class InviteOrganizationUsersCommand(IEventService eventService,
{
if (validatedResult.Value.PasswordManagerSubscriptionUpdate is { SeatsRequiredToAdd: > 0, UpdatedSeatTotal: > 0 })
{
await paymentService.AdjustSeatsAsync(organization,
validatedResult.Value.InviteOrganization.Plan,
validatedResult.Value.PasswordManagerSubscriptionUpdate.UpdatedSeatTotal.Value);
await organizationRepository.IncrementSeatCountAsync(
organization.Id,
validatedResult.Value.PasswordManagerSubscriptionUpdate.SeatsRequiredToAdd,
validatedResult.Value.PerformedAt.UtcDateTime);
organization.Seats = (short?)validatedResult.Value.PasswordManagerSubscriptionUpdate.UpdatedSeatTotal;
organization.Seats = validatedResult.Value.PasswordManagerSubscriptionUpdate.UpdatedSeatTotal;
organization.SyncSeats = true;
await organizationRepository.ReplaceAsync(organization); // could optimize this with only a property update
await applicationCacheService.UpsertOrganizationAbilityAsync(organization);
}
}

View File

@@ -1,7 +1,4 @@
// FIXME: Update this file to be null safe and then delete the line below
#nullable disable
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Models;
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Models;
using Bit.Core.AdminConsole.OrganizationFeatures.OrganizationUsers.InviteUsers.Validation.PasswordManager;
using Bit.Core.AdminConsole.Utilities.Errors;
using Bit.Core.AdminConsole.Utilities.Validation;

View File

@@ -0,0 +1,24 @@
using Bit.Core.AdminConsole.Models.Data.Organizations;
using Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces;
using Bit.Core.Billing.Pricing;
using Bit.Core.Repositories;
namespace Bit.Core.AdminConsole.OrganizationFeatures.Organizations;
public class GetOrganizationSubscriptionsToUpdateQuery(IOrganizationRepository organizationRepository,
IPricingClient pricingClient) : IGetOrganizationSubscriptionsToUpdateQuery
{
public async Task<IEnumerable<OrganizationSubscriptionUpdate>> GetOrganizationSubscriptionsToUpdateAsync()
{
var organizationsToUpdateTask = organizationRepository.GetOrganizationsForSubscriptionSyncAsync();
var plansTask = pricingClient.ListPlans();
await Task.WhenAll(organizationsToUpdateTask, plansTask);
return organizationsToUpdateTask.Result.Select(o => new OrganizationSubscriptionUpdate
{
Organization = o,
Plan = plansTask.Result.FirstOrDefault(plan => plan.Type == o.PlanType)
});
}
}

View File

@@ -0,0 +1,16 @@
using Bit.Core.AdminConsole.Models.Data.Organizations;
namespace Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces;
public interface IGetOrganizationSubscriptionsToUpdateQuery
{
/// <summary>
/// Retrieves a collection of organization subscriptions that need to be updated. This is based on if the
/// Organization.SyncSeats flag is true and Organization.Seats has a value.
/// </summary>
/// <returns>
/// A collection of <see cref="OrganizationSubscriptionUpdate"/> instances, each representing an organization
/// subscription to be updated with their associated plan.
/// </returns>
Task<IEnumerable<OrganizationSubscriptionUpdate>> GetOrganizationSubscriptionsToUpdateAsync();
}

View File

@@ -0,0 +1,16 @@
using Bit.Core.AdminConsole.Models.Data.Organizations;
namespace Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces;
public interface IUpdateOrganizationSubscriptionCommand
{
/// <summary>
/// Attempts to update the subscription of all organizations that have had a subscription update.
///
/// If successful, the Organization.SyncSeats flag will be set to false and Organization.RevisionDate will be set.
///
/// In the event of a failure, it will log the failure and maybe be picked up in later runs.
/// </summary>
/// <param name="subscriptionsToUpdate">The collection of organization subscriptions to update.</param>
Task UpdateOrganizationSubscriptionAsync(IEnumerable<OrganizationSubscriptionUpdate> subscriptionsToUpdate);
}

View File

@@ -0,0 +1,43 @@
using Bit.Core.AdminConsole.Models.Data.Organizations;
using Bit.Core.AdminConsole.OrganizationFeatures.Organizations.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Microsoft.Extensions.Logging;
namespace Bit.Core.AdminConsole.OrganizationFeatures.Organizations;
public class UpdateOrganizationSubscriptionCommand(IPaymentService paymentService,
IOrganizationRepository repository,
TimeProvider timeProvider,
ILogger<UpdateOrganizationSubscriptionCommand> logger) : IUpdateOrganizationSubscriptionCommand
{
public async Task UpdateOrganizationSubscriptionAsync(IEnumerable<OrganizationSubscriptionUpdate> subscriptionsToUpdate)
{
var successfulSyncs = new List<Guid>();
foreach (var subscriptionUpdate in subscriptionsToUpdate)
{
try
{
await paymentService.AdjustSeatsAsync(subscriptionUpdate.Organization,
subscriptionUpdate.Plan,
subscriptionUpdate.Seats);
successfulSyncs.Add(subscriptionUpdate.Organization.Id);
}
catch (Exception ex)
{
logger.LogError(ex,
"Failed to update organization {organizationId} subscription.",
subscriptionUpdate.Organization.Id);
}
}
if (successfulSyncs.Count == 0)
{
return;
}
await repository.UpdateSuccessfulOrganizationSyncStatusAsync(successfulSyncs, timeProvider.GetUtcNow().UtcDateTime);
}
}

View File

@@ -24,6 +24,7 @@ public interface IOrganizationRepository : IRepository<Organization, Guid>
/// Gets the organizations that have a verified domain matching the user's email domain.
/// </summary>
Task<ICollection<Organization>> GetByVerifiedUserEmailDomainAsync(Guid userId);
Task<ICollection<Organization>> GetAddableToProviderByUserIdAsync(Guid userId, ProviderType providerType);
Task<ICollection<Organization>> GetManyByIdsAsync(IEnumerable<Guid> ids);
@@ -36,4 +37,29 @@ public interface IOrganizationRepository : IRepository<Organization, Guid>
/// <param name="organizationId">The ID of the organization to get the occupied seat count for.</param>
/// <returns>The number of occupied seats for the organization.</returns>
Task<OrganizationSeatCounts> GetOccupiedSeatCountByOrganizationIdAsync(Guid organizationId);
/// <summary>
/// Get all organizations that need to have their seat count updated to their Stripe subscription.
/// </summary>
/// <returns>Organizations to sync to Stripe</returns>
Task<IEnumerable<Organization>> GetOrganizationsForSubscriptionSyncAsync();
/// <summary>
/// Updates the organization SeatSync property to signify the organization's subscription has been updated in stripe
/// to match the password manager seats for the organization.
/// </summary>
/// <param name="successfulOrganizations"></param>
/// <param name="syncDate"></param>
/// <returns></returns>
Task UpdateSuccessfulOrganizationSyncStatusAsync(IEnumerable<Guid> successfulOrganizations, DateTime syncDate);
/// <summary>
/// This increments the password manager seat count on the organization by the provided amount and sets SyncSeats to true.
/// It also sets the revision date using the request date.
/// </summary>
/// <param name="organizationId">Organization to update</param>
/// <param name="increaseAmount">Amount to increase password manager seats by</param>
/// <param name="requestDate">When the action was performed</param>
/// <returns></returns>
Task IncrementSeatCountAsync(Guid organizationId, int increaseAmount, DateTime requestDate);
}