| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using OzonDomains.Models.Partners; |
| | | 3 | | using OzonRepositories.Context; |
| | | 4 | | |
| | | 5 | | namespace Services.PartnerServices; |
| | | 6 | | |
| | | 7 | | public interface IPartnerAuditService |
| | | 8 | | { |
| | | 9 | | Task WriteAsync( |
| | | 10 | | string action, |
| | | 11 | | PartnerActorKind actorKind, |
| | | 12 | | string actor, |
| | | 13 | | long? partnerId, |
| | | 14 | | bool success = true, |
| | | 15 | | string? details = null, |
| | | 16 | | string? orderNumber = null, |
| | | 17 | | string? ipAddress = null, |
| | | 18 | | CancellationToken ct = default); |
| | | 19 | | |
| | | 20 | | Task<IReadOnlyList<PartnerAuditLog>> QueryAsync(PartnerAuditQuery query, CancellationToken ct = default); |
| | | 21 | | } |
| | | 22 | | |
| | 50 | 23 | | public sealed class PartnerAuditService(ApplicationDbContext db) : IPartnerAuditService |
| | | 24 | | { |
| | | 25 | | private const int MaxTake = 500; |
| | | 26 | | |
| | | 27 | | public async Task WriteAsync( |
| | | 28 | | string action, |
| | | 29 | | PartnerActorKind actorKind, |
| | | 30 | | string actor, |
| | | 31 | | long? partnerId, |
| | | 32 | | bool success = true, |
| | | 33 | | string? details = null, |
| | | 34 | | string? orderNumber = null, |
| | | 35 | | string? ipAddress = null, |
| | | 36 | | CancellationToken ct = default) |
| | | 37 | | { |
| | 92 | 38 | | db.PartnerAuditLogs.Add(new PartnerAuditLog |
| | 92 | 39 | | { |
| | 92 | 40 | | Action = action, |
| | 92 | 41 | | ActorKind = actorKind, |
| | 92 | 42 | | Actor = Truncate(actor, 200) ?? "system", |
| | 92 | 43 | | PartnerId = partnerId, |
| | 92 | 44 | | Success = success, |
| | 92 | 45 | | Details = Truncate(details, 2000), |
| | 92 | 46 | | OrderNumber = Truncate(orderNumber, 100), |
| | 92 | 47 | | IpAddress = Truncate(ipAddress, 64), |
| | 92 | 48 | | OccurredAtUtc = DateTime.UtcNow |
| | 92 | 49 | | }); |
| | 92 | 50 | | await db.SaveChangesAsync(ct); |
| | 92 | 51 | | } |
| | | 52 | | |
| | | 53 | | public async Task<IReadOnlyList<PartnerAuditLog>> QueryAsync(PartnerAuditQuery query, CancellationToken ct = default |
| | | 54 | | { |
| | 5 | 55 | | IQueryable<PartnerAuditLog> q = db.PartnerAuditLogs.AsNoTracking(); |
| | 9 | 56 | | if (query.PartnerId is { } pid) q = q.Where(x => x.PartnerId == pid); |
| | 10 | 57 | | if (!string.IsNullOrWhiteSpace(query.Action)) q = q.Where(x => x.Action == query.Action); |
| | 5 | 58 | | if (query.FromUtc is { } from) q = q.Where(x => x.OccurredAtUtc >= from); |
| | 5 | 59 | | if (query.ToUtc is { } to) q = q.Where(x => x.OccurredAtUtc <= to); |
| | | 60 | | |
| | 5 | 61 | | return await q |
| | 5 | 62 | | .OrderByDescending(x => x.OccurredAtUtc) |
| | 5 | 63 | | .ThenByDescending(x => x.Id) |
| | 5 | 64 | | .Skip(Math.Max(0, query.Skip)) |
| | 5 | 65 | | .Take(Math.Clamp(query.Take, 1, MaxTake)) |
| | 5 | 66 | | .ToListAsync(ct); |
| | 5 | 67 | | } |
| | | 68 | | |
| | | 69 | | private static string? Truncate(string? value, int max) => |
| | 368 | 70 | | value is null || value.Length <= max ? value : value[..max]; |
| | | 71 | | } |