Implemented audit logging
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using PostFundManagement.Domain;
|
||||
using PostFundManagement.Domain;
|
||||
using PostFundManagement.Domain.Abstractions;
|
||||
using PostFundManagement.Domain.Errors;
|
||||
using PostFundManagement.Domain.Extensions;
|
||||
@@ -10,27 +10,22 @@ namespace PostFundManagement.Application.Services.Shared;
|
||||
public sealed class AuditService(IDbContextFactory<ApplicationDbContext> contextFactory, ITenantContextService tenantContextService) : IService
|
||||
{
|
||||
public async Task<Result<AuditLog[]>> GetAuditLogsAsync(CancellationToken cancellationToken = default) =>
|
||||
await Result.Try((Func<Task<Result<AuditLog[]>>>)(async () =>
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var pagination = new Pagination { Page = 1, PageSize = 500, Index = 1 };
|
||||
|
||||
var logs = await context.AuditLogs
|
||||
.OrderByDescending(o => o.Id)
|
||||
.Skip((pagination.Page - 1) * pagination.PageSize)
|
||||
.Take(pagination.PageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return Result.Ok(logs.Select(p => p.Map()).ToArray());
|
||||
}));
|
||||
await GetAuditLogsAsync(new Pagination { Page = 1, PageSize = 500, Index = 1 }, cancellationToken);
|
||||
|
||||
public async Task<Result<AuditLog[]>> GetAuditLogsAsync(Pagination pagination, CancellationToken cancellationToken = default) =>
|
||||
await Result.Try((Func<Task<Result<AuditLog[]>>>)(async () =>
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var logs = await context.AuditLogs
|
||||
var query = context.AuditLogs.AsQueryable();
|
||||
|
||||
if (!tenantContextService.IsAdmin)
|
||||
{
|
||||
var currentTenantId = tenantContextService.TenantId > 0 ? tenantContextService.TenantId : 1L;
|
||||
query = query.Where(o => o.TenantId == currentTenantId);
|
||||
}
|
||||
|
||||
var logs = await query
|
||||
.OrderByDescending(o => o.Id)
|
||||
.Skip((pagination.Page - 1) * pagination.PageSize)
|
||||
.Take(pagination.PageSize)
|
||||
@@ -39,11 +34,23 @@ public sealed class AuditService(IDbContextFactory<ApplicationDbContext> context
|
||||
return Result.Ok(logs.Select(p => p.Map()).ToArray());
|
||||
}));
|
||||
|
||||
public async Task<Result> LogActionAsync(string entityId, string entityName, string action, string[] changes, Guid performedBy, CancellationToken cancellationToken = default) =>
|
||||
public async Task<Result> LogActionAsync(
|
||||
string entityId,
|
||||
string entityName,
|
||||
string action,
|
||||
string[] changes,
|
||||
Guid performedBy,
|
||||
string? error = null,
|
||||
long? tenantId = null,
|
||||
CancellationToken cancellationToken = default) =>
|
||||
await Result.Try((Func<Task<Result>>)(async () =>
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
|
||||
var resolvedTenantId = (tenantId.HasValue && tenantId.Value > 0)
|
||||
? tenantId.Value
|
||||
: (tenantContextService.TenantId > 0 ? tenantContextService.TenantId : 1L);
|
||||
|
||||
context.Add(new Domain.Entities.AuditLog
|
||||
{
|
||||
EntityId = entityId,
|
||||
@@ -52,7 +59,8 @@ public sealed class AuditService(IDbContextFactory<ApplicationDbContext> context
|
||||
EntityName = entityName,
|
||||
PerformedBy = performedBy,
|
||||
Timestamp = DateTime.UtcNow,
|
||||
TenantId = tenantContextService.TenantId > 0 ? tenantContextService.TenantId : null,
|
||||
TenantId = resolvedTenantId,
|
||||
Error = error,
|
||||
});
|
||||
|
||||
return Result.FailIf(await context.SaveChangesAsync(cancellationToken) <= 0, CreateError.New);
|
||||
|
||||
Reference in New Issue
Block a user