Enhance admin maintenance functionality with new endpoints and response types
- Added new DELETE endpoints for managing audit logs and disabled apps in the admin maintenance section. - Updated existing endpoint for closed tickets to use a unified response type, `MaintenanceCleanupResponseDto`. - Enhanced API documentation to reflect the new operations and their expected request/response formats. - Improved frontend integration with new functions for deleting old audit logs and disabled apps, including user confirmation prompts. - Added localization support for new maintenance actions in both Russian and English.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using PnvPanel.Api.Common;
|
||||
using PnvPanel.Application.Admin.Maintenance;
|
||||
using PnvPanel.Application.Common.Messaging;
|
||||
using PnvPanel.Application.Common.Models;
|
||||
using PnvPanel.Infrastructure.Identity;
|
||||
|
||||
namespace PnvPanel.Api.Endpoints;
|
||||
@@ -15,7 +16,13 @@ public static class AdminMaintenanceEndpoints
|
||||
|
||||
admin
|
||||
.MapDelete("/tickets/closed", DeleteClosedTickets)
|
||||
.Produces<DeleteClosedTicketsResponseDto>();
|
||||
.Produces<MaintenanceCleanupResponseDto>();
|
||||
admin
|
||||
.MapDelete("/audit-logs", DeleteOldAuditLogs)
|
||||
.Produces<MaintenanceCleanupResponseDto>();
|
||||
admin
|
||||
.MapDelete("/apps/disabled", DeleteDisabledApps)
|
||||
.Produces<MaintenanceCleanupResponseDto>();
|
||||
|
||||
return app;
|
||||
}
|
||||
@@ -26,11 +33,35 @@ public static class AdminMaintenanceEndpoints
|
||||
)
|
||||
{
|
||||
var result = await sender.Send(new DeleteClosedTicketsCommand(), cancellationToken);
|
||||
if (!result.IsSuccess)
|
||||
return result.ToHttpResult();
|
||||
|
||||
return Results.Ok(new DeleteClosedTicketsResponseDto(result.Value));
|
||||
return ToResponse(result);
|
||||
}
|
||||
|
||||
private static async Task<IResult> DeleteOldAuditLogs(
|
||||
int olderThanDays,
|
||||
ISender sender,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var result = await sender.Send(
|
||||
new DeleteOldAuditLogsCommand(olderThanDays),
|
||||
cancellationToken
|
||||
);
|
||||
return ToResponse(result);
|
||||
}
|
||||
|
||||
private static async Task<IResult> DeleteDisabledApps(
|
||||
ISender sender,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var result = await sender.Send(new DeleteDisabledAppsCommand(), cancellationToken);
|
||||
return ToResponse(result);
|
||||
}
|
||||
|
||||
private static IResult ToResponse(Result<int> result) =>
|
||||
result.IsSuccess
|
||||
? Results.Ok(new MaintenanceCleanupResponseDto(result.Value))
|
||||
: result.ToHttpResult();
|
||||
}
|
||||
|
||||
public sealed record DeleteClosedTicketsResponseDto(int DeletedCount);
|
||||
public sealed record MaintenanceCleanupResponseDto(int DeletedCount);
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ public sealed class DeleteClosedTicketsCommandHandler(
|
||||
"ClosedTicketsCleanedUp",
|
||||
"SupportTicket",
|
||||
"bulk",
|
||||
metadata: $"count={ticketIds.Count}",
|
||||
metadata: $"{{\"count\":{ticketIds.Count}}}",
|
||||
AuditSource.Web
|
||||
)
|
||||
);
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
using PnvPanel.Application.Common.Messaging;
|
||||
using PnvPanel.Application.Common.Models;
|
||||
|
||||
namespace PnvPanel.Application.Admin.Maintenance;
|
||||
|
||||
/// <summary>Удаляет все ClientApp с IsEnabled = false. Value — число удалённых записей.</summary>
|
||||
public sealed record DeleteDisabledAppsCommand : ICommand<Result<int>>;
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PnvPanel.Application.Auth;
|
||||
using PnvPanel.Application.Common.Interfaces;
|
||||
using PnvPanel.Application.Common.Messaging;
|
||||
using PnvPanel.Application.Common.Models;
|
||||
using PnvPanel.Domain.Audit;
|
||||
|
||||
namespace PnvPanel.Application.Admin.Maintenance;
|
||||
|
||||
public sealed class DeleteDisabledAppsCommandHandler(
|
||||
IAppDbContext dbContext,
|
||||
ICurrentUser currentUser
|
||||
) : ICommandHandler<DeleteDisabledAppsCommand, Result<int>>
|
||||
{
|
||||
public async Task<Result<int>> Handle(
|
||||
DeleteDisabledAppsCommand command,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
if (currentUser.UserId is not { } adminId)
|
||||
return Result.Failure<int>(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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using PnvPanel.Application.Common.Messaging;
|
||||
using PnvPanel.Application.Common.Models;
|
||||
|
||||
namespace PnvPanel.Application.Admin.Maintenance;
|
||||
|
||||
/// <summary>Удаляет записи AuditLog старше OlderThanDays дней. Value — число удалённых записей.</summary>
|
||||
public sealed record DeleteOldAuditLogsCommand(int OlderThanDays) : ICommand<Result<int>>;
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PnvPanel.Application.Auth;
|
||||
using PnvPanel.Application.Common.Interfaces;
|
||||
using PnvPanel.Application.Common.Messaging;
|
||||
using PnvPanel.Application.Common.Models;
|
||||
using PnvPanel.Domain.Audit;
|
||||
|
||||
namespace PnvPanel.Application.Admin.Maintenance;
|
||||
|
||||
public sealed class DeleteOldAuditLogsCommandHandler(
|
||||
IAppDbContext dbContext,
|
||||
ICurrentUser currentUser
|
||||
) : ICommandHandler<DeleteOldAuditLogsCommand, Result<int>>
|
||||
{
|
||||
public async Task<Result<int>> Handle(
|
||||
DeleteOldAuditLogsCommand command,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
if (currentUser.UserId is not { } adminId)
|
||||
return Result.Failure<int>(AuthErrors.Unauthorized);
|
||||
|
||||
var threshold = DateTimeOffset.UtcNow.AddDays(-command.OlderThanDays);
|
||||
|
||||
var logs = await dbContext
|
||||
.AuditLogs.Where(l => l.CreatedAt < threshold)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (logs.Count == 0)
|
||||
return Result.Success(0);
|
||||
|
||||
dbContext.AuditLogs.RemoveRange(logs);
|
||||
|
||||
// Новая запись создаётся уже после выборки старых — её CreatedAt = UtcNow не попадает
|
||||
// под тот же порог и не удаляется этой же операцией.
|
||||
dbContext.AuditLogs.Add(
|
||||
AuditLog.Create(
|
||||
adminId,
|
||||
"AuditLogsCleanedUp",
|
||||
"AuditLog",
|
||||
"bulk",
|
||||
metadata: $"{{\"count\":{logs.Count},\"olderThanDays\":{command.OlderThanDays}}}",
|
||||
AuditSource.Web
|
||||
)
|
||||
);
|
||||
|
||||
return Result.Success(logs.Count);
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace PnvPanel.Application.Admin.Maintenance;
|
||||
|
||||
public sealed class DeleteOldAuditLogsCommandValidator
|
||||
: AbstractValidator<DeleteOldAuditLogsCommand>
|
||||
{
|
||||
public DeleteOldAuditLogsCommandValidator()
|
||||
{
|
||||
RuleFor(x => x.OlderThanDays).GreaterThanOrEqualTo(1).LessThanOrEqualTo(3650);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user