using Microsoft.EntityFrameworkCore; using PnvPanel.Application.Auth; using PnvPanel.Application.Common.Interfaces; using LiteCqrs; using PnvPanel.Application.Common.Models; using PnvPanel.Domain.Audit; namespace PnvPanel.Application.Admin.Maintenance; public sealed class DeleteDisabledAppsCommandHandler( IAppDbContext dbContext, ICurrentUser currentUser ) : ICommandHandler> { public async Task> Handle( DeleteDisabledAppsCommand command, CancellationToken cancellationToken ) { if (currentUser.UserId is not { } adminId) return Result.Failure(AuthErrors.Unauthorized); var apps = await dbContext .ClientApps.Where(a => !a.IsEnabled) .ToListAsync(cancellationToken); if (apps.Count == 0) return Result.Success(0); dbContext.ClientApps.RemoveRange(apps); dbContext.AuditLogs.Add( AuditLog.Create( adminId, "DisabledAppsCleanedUp", "ClientApp", "bulk", metadata: $"{{\"count\":{apps.Count}}}", AuditSource.Web ) ); return Result.Success(apps.Count); } }