using Hutopy.Web.Features.Contents.Data; using Hutopy.Web.Features.Memberships.Events; namespace Hutopy.Web.Features.Contents.EventHandlers; [UsedImplicitly] public class StripeAccountConfiguredHandler( ILogger logger, IServiceScopeFactory scopeFactory) : IEventHandler { public async Task HandleAsync( StripeAccountConfigured eventModel, CancellationToken ct) { using var scope = scopeFactory.CreateScope(); await using var dbContext = scope.ServiceProvider.GetRequiredService(); var creator = await dbContext.FindAsync( [eventModel.CreatorId], cancellationToken: ct); if (creator is null) { logger.LogError( "Creator with id {CreatorId} was not found.", eventModel.CreatorId); return; } creator.AcceptDonation = !string.IsNullOrWhiteSpace(eventModel.StripeAccountId); var rows = await dbContext.SaveChangesAsync(ct); if (rows is 0 or > 1) { logger.LogError( "An error occured while updating Creator with id {CreatorId}: rows:{Rows}", eventModel.CreatorId, rows); } } }