Enhance admin endpoints and queries for improved filtering and management
CI / Backend (build + test) (push) Successful in 1m22s
CI / Frontend (lint + typecheck + build) (push) Successful in 34s

- Updated `ListPaymentRequestsQuery` to include `Kind` and `Search` parameters for better filtering of payment requests.
- Enhanced `ListAuditLogsQuery` to support additional filters: `Source`, `TargetType`, and `Action`, improving audit log retrieval.
- Modified `ListUsersQuery` to accept new filters: `RoleId`, `IsActivated`, `IsBlocked`, and `BillingExpired`, allowing for more granular user management.
- Introduced `DeleteInbound` endpoint to allow deletion of inbounds that are not currently available, enhancing inbound management capabilities.
- Updated frontend API calls to reflect new query parameters and support for additional filtering options in the admin interface.
- Revised API documentation to include new parameters and endpoint functionalities for better clarity and usage guidance.
This commit is contained in:
Leonid Pershin
2026-07-20 10:35:48 +03:00
parent e19860ba46
commit 33ad98cf62
37 changed files with 1115 additions and 124 deletions
@@ -0,0 +1,75 @@
using PnvPanel.Application.Admin.Audit;
using PnvPanel.Application.Tests.TestSupport;
using PnvPanel.Domain.Audit;
using Xunit;
namespace PnvPanel.Application.Tests.Admin.Audit;
public class ListAuditLogsQueryHandlerTests
{
[Fact]
public async Task Handle_FiltersBySource()
{
using var dbContext = InMemoryDbContextFactory.Create();
dbContext.AuditLogs.AddRange(
AuditLog.Create(null, "BillingSuspended", "User", Guid.NewGuid().ToString(), null, AuditSource.System),
AuditLog.Create(Guid.NewGuid(), "InboundDeleted", "Inbound", Guid.NewGuid().ToString(), null, AuditSource.Web)
);
await dbContext.SaveChangesAsync(CancellationToken.None);
var handler = new ListAuditLogsQueryHandler(dbContext);
var result = await handler.Handle(
new ListAuditLogsQuery(1, 50, AuditSource.System, TargetType: null, Action: null),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
Assert.Equal("BillingSuspended", item.Action);
}
[Fact]
public async Task Handle_FiltersByTargetType()
{
using var dbContext = InMemoryDbContextFactory.Create();
dbContext.AuditLogs.AddRange(
AuditLog.Create(Guid.NewGuid(), "UserBlocked", "User", Guid.NewGuid().ToString(), null, AuditSource.Web),
AuditLog.Create(Guid.NewGuid(), "InboundDeleted", "Inbound", Guid.NewGuid().ToString(), null, AuditSource.Web)
);
await dbContext.SaveChangesAsync(CancellationToken.None);
var handler = new ListAuditLogsQueryHandler(dbContext);
var result = await handler.Handle(
new ListAuditLogsQuery(1, 50, Source: null, TargetType: "Inbound", Action: null),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
Assert.Equal("InboundDeleted", item.Action);
}
[Fact]
public async Task Handle_FiltersByActionSubstring()
{
using var dbContext = InMemoryDbContextFactory.Create();
dbContext.AuditLogs.AddRange(
AuditLog.Create(Guid.NewGuid(), "TicketResolved", "SupportTicket", Guid.NewGuid().ToString(), null, AuditSource.Web),
AuditLog.Create(Guid.NewGuid(), "TicketClosed", "SupportTicket", Guid.NewGuid().ToString(), null, AuditSource.Web)
);
await dbContext.SaveChangesAsync(CancellationToken.None);
var handler = new ListAuditLogsQueryHandler(dbContext);
var result = await handler.Handle(
new ListAuditLogsQuery(1, 50, Source: null, TargetType: null, Action: "Resolved"),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
Assert.Equal("TicketResolved", item.Action);
}
}
@@ -0,0 +1,81 @@
using NSubstitute;
using PnvPanel.Application.Admin.Billing;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Tests.TestSupport;
using PnvPanel.Domain.Billing;
using Xunit;
namespace PnvPanel.Application.Tests.Admin.Billing;
public class ListPaymentRequestsQueryHandlerTests
{
private readonly IIdentityService _identityService = Substitute.For<IIdentityService>();
[Fact]
public async Task Handle_FiltersByKind()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var subscription = PaymentRequest.Create(userId, PaymentPeriod.Quarter, 1500);
var topUp = PaymentRequest.CreateRoleChangeTopUp(userId, 500);
dbContext.PaymentRequests.AddRange(subscription, topUp);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(new Dictionary<Guid, string> { [userId] = "alice" });
var handler = new ListPaymentRequestsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(
new ListPaymentRequestsQuery(
StatusFilter: null,
KindFilter: PaymentRequestKind.RoleChangeTopUp,
Search: null,
Page: 1,
PageSize: 20
),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
Assert.Equal(topUp.Id, item.Id);
}
[Fact]
public async Task Handle_FiltersBySearch_ResolvesUserIdsBeforePagination()
{
using var dbContext = InMemoryDbContextFactory.Create();
var aliceId = Guid.NewGuid();
var bobId = Guid.NewGuid();
var aliceRequest = PaymentRequest.Create(aliceId, PaymentPeriod.Quarter, 1500);
var bobRequest = PaymentRequest.Create(bobId, PaymentPeriod.Quarter, 1500);
dbContext.PaymentRequests.AddRange(aliceRequest, bobRequest);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.FindUserIdsByUserNameAsync("alice", Arg.Any<CancellationToken>())
.Returns([aliceId]);
_identityService
.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(new Dictionary<Guid, string> { [aliceId] = "alice" });
var handler = new ListPaymentRequestsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(
new ListPaymentRequestsQuery(
StatusFilter: null,
KindFilter: null,
Search: "alice",
Page: 1,
PageSize: 20
),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
Assert.Equal(aliceRequest.Id, item.Id);
}
}
@@ -41,7 +41,7 @@ public class ListAllConfigsQueryHandlerTests
var handler = new ListAllConfigsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(
new ListAllConfigsQuery(1, 20, Search: null, Status: null),
new ListAllConfigsQuery(1, 20, Search: null, Status: null, Protocol: null, NodeId: null),
CancellationToken.None
);
@@ -83,7 +83,7 @@ public class ListAllConfigsQueryHandlerTests
var handler = new ListAllConfigsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(
new ListAllConfigsQuery(1, 20, Search: "phone", Status: null),
new ListAllConfigsQuery(1, 20, Search: "phone", Status: null, Protocol: null, NodeId: null),
CancellationToken.None
);
@@ -121,7 +121,7 @@ public class ListAllConfigsQueryHandlerTests
var handler = new ListAllConfigsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(
new ListAllConfigsQuery(1, 20, Search: null, Status: ConfigStatus.Revoked),
new ListAllConfigsQuery(1, 20, Search: null, Status: ConfigStatus.Revoked, Protocol: null, NodeId: null),
CancellationToken.None
);
@@ -129,4 +129,85 @@ public class ListAllConfigsQueryHandlerTests
var item = Assert.Single(result.Value.Items);
Assert.Equal(revoked.Id, item.Id);
}
[Fact]
public async Task Handle_FiltersByProtocol()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var node = Node.Register(
"node-1",
new Uri("https://node1.example.com"),
new NodeCredentials("admin", "protected"),
null
);
var inbound = Inbound.FromRemote(node.Id, "1", VpnProtocol.Vless, "remark", 443);
var vless = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, "vless-config");
var trojan = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Trojan, "trojan-config");
dbContext.Nodes.Add(node);
dbContext.Inbounds.Add(inbound);
dbContext.VpnConfigs.AddRange(vless, trojan);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(new Dictionary<Guid, string> { [userId] = "alice" });
var handler = new ListAllConfigsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(
new ListAllConfigsQuery(1, 20, Search: null, Status: null, Protocol: VpnProtocol.Trojan, NodeId: null),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
Assert.Equal(trojan.Id, item.Id);
}
[Fact]
public async Task Handle_FiltersByNodeId()
{
using var dbContext = InMemoryDbContextFactory.Create();
var userId = Guid.NewGuid();
var nodeA = Node.Register(
"node-a",
new Uri("https://node-a.example.com"),
new NodeCredentials("admin", "protected"),
null
);
var nodeB = Node.Register(
"node-b",
new Uri("https://node-b.example.com"),
new NodeCredentials("admin", "protected"),
null
);
var inboundA = Inbound.FromRemote(nodeA.Id, "1", VpnProtocol.Vless, "remark", 443);
var inboundB = Inbound.FromRemote(nodeB.Id, "1", VpnProtocol.Vless, "remark", 443);
var configA = VpnConfig.Create(userId, inboundA.Id, VpnProtocol.Vless, "config-a");
var configB = VpnConfig.Create(userId, inboundB.Id, VpnProtocol.Vless, "config-b");
dbContext.Nodes.AddRange(nodeA, nodeB);
dbContext.Inbounds.AddRange(inboundA, inboundB);
dbContext.VpnConfigs.AddRange(configA, configB);
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.GetUserNamesAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(new Dictionary<Guid, string> { [userId] = "alice" });
var handler = new ListAllConfigsQueryHandler(dbContext, _identityService);
var result = await handler.Handle(
new ListAllConfigsQuery(1, 20, Search: null, Status: null, Protocol: null, NodeId: nodeB.Id),
CancellationToken.None
);
Assert.True(result.IsSuccess);
var item = Assert.Single(result.Value.Items);
Assert.Equal(configB.Id, item.Id);
}
}
@@ -0,0 +1,88 @@
using NSubstitute;
using PnvPanel.Application.Admin.Inbounds;
using PnvPanel.Application.Common.Interfaces;
using PnvPanel.Application.Tests.TestSupport;
using PnvPanel.Domain.Configs;
using PnvPanel.Domain.Inbounds;
using Xunit;
namespace PnvPanel.Application.Tests.Admin.Inbounds;
public class DeleteInboundCommandHandlerTests
{
private readonly IRealtimeNotifier _notifier = Substitute.For<IRealtimeNotifier>();
private readonly ICurrentUser _currentUser = Substitute.For<ICurrentUser>();
private DeleteInboundCommandHandler CreateHandler(IAppDbContext dbContext) =>
new(dbContext, _notifier, _currentUser);
[Fact]
public async Task Handle_WhenInboundNotFound_ReturnsNotFound()
{
using var dbContext = InMemoryDbContextFactory.Create();
var result = await CreateHandler(dbContext)
.Handle(new DeleteInboundCommand(Guid.NewGuid()), CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(InboundErrors.NotFound, result.Error);
}
[Fact]
public async Task Handle_WhenInboundStillAvailable_ReturnsStillAvailable()
{
using var dbContext = InMemoryDbContextFactory.Create();
var inbound = Inbound.FromRemote(Guid.NewGuid(), "1", VpnProtocol.Vless, "remark", 443);
dbContext.Inbounds.Add(inbound);
await dbContext.SaveChangesAsync(CancellationToken.None);
var result = await CreateHandler(dbContext)
.Handle(new DeleteInboundCommand(inbound.Id), CancellationToken.None);
Assert.False(result.IsSuccess);
Assert.Equal(InboundErrors.StillAvailable, result.Error);
Assert.NotNull(await dbContext.Inbounds.FindAsync([inbound.Id], CancellationToken.None));
}
[Fact]
public async Task Handle_WhenUnavailable_RevokesActiveConfigsAndDeletesInbound()
{
using var dbContext = InMemoryDbContextFactory.Create();
var inbound = Inbound.FromRemote(Guid.NewGuid(), "1", VpnProtocol.Vless, "remark", 443);
inbound.MarkUnavailable();
var userId = Guid.NewGuid();
var activeConfig = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, null);
var alreadyRevoked = VpnConfig.Create(userId, inbound.Id, VpnProtocol.Vless, null);
alreadyRevoked.Revoke();
dbContext.Inbounds.Add(inbound);
dbContext.VpnConfigs.AddRange(activeConfig, alreadyRevoked);
await dbContext.SaveChangesAsync(CancellationToken.None);
var result = await CreateHandler(dbContext)
.Handle(new DeleteInboundCommand(inbound.Id), CancellationToken.None);
// UnitOfWorkBehavior делает это в реальном пайплайне — здесь хендлер вызывается напрямую.
await dbContext.SaveChangesAsync(CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.Equal(ConfigStatus.Revoked, activeConfig.Status);
Assert.Null(await dbContext.Inbounds.FindAsync([inbound.Id], CancellationToken.None));
await _notifier
.Received(1)
.NotifyConfigStatusChangedAsync(
userId,
activeConfig.Id,
ConfigStatus.Revoked,
Arg.Any<CancellationToken>()
);
await _notifier
.DidNotReceive()
.NotifyConfigStatusChangedAsync(
userId,
alreadyRevoked.Id,
Arg.Any<ConfigStatus>(),
Arg.Any<CancellationToken>()
);
}
}
@@ -26,12 +26,12 @@ public class ListUsersQueryHandlerTests
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.ListUsersAsync(1, 20, null, Arg.Any<CancellationToken>())
.ListUsersAsync(1, 20, null, null, null, null, null, Arg.Any<CancellationToken>())
.Returns(new PagedList<UserSummaryDto>([Summary(userId)], 1, 1, 20));
var handler = new ListUsersQueryHandler(_identityService, dbContext);
var result = await handler.Handle(new ListUsersQuery(1, 20, null), CancellationToken.None);
var result = await handler.Handle(new ListUsersQuery(1, 20, null, null, null, null, null), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.True(result.Value.Items.Single().BillingPendingReview);
@@ -44,12 +44,12 @@ public class ListUsersQueryHandlerTests
var userId = Guid.NewGuid();
_identityService
.ListUsersAsync(1, 20, null, Arg.Any<CancellationToken>())
.ListUsersAsync(1, 20, null, null, null, null, null, Arg.Any<CancellationToken>())
.Returns(new PagedList<UserSummaryDto>([Summary(userId)], 1, 1, 20));
var handler = new ListUsersQueryHandler(_identityService, dbContext);
var result = await handler.Handle(new ListUsersQuery(1, 20, null), CancellationToken.None);
var result = await handler.Handle(new ListUsersQuery(1, 20, null, null, null, null, null), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.False(result.Value.Items.Single().BillingPendingReview);
@@ -68,12 +68,12 @@ public class ListUsersQueryHandlerTests
await dbContext.SaveChangesAsync(CancellationToken.None);
_identityService
.ListUsersAsync(1, 20, null, Arg.Any<CancellationToken>())
.ListUsersAsync(1, 20, null, null, null, null, null, Arg.Any<CancellationToken>())
.Returns(new PagedList<UserSummaryDto>([Summary(userId)], 1, 1, 20));
var handler = new ListUsersQueryHandler(_identityService, dbContext);
var result = await handler.Handle(new ListUsersQuery(1, 20, null), CancellationToken.None);
var result = await handler.Handle(new ListUsersQuery(1, 20, null, null, null, null, null), CancellationToken.None);
Assert.True(result.IsSuccess);
Assert.False(result.Value.Items.Single().BillingPendingReview);