| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | |
| | | 7 | | namespace Services.OzonWebhookServices; |
| | | 8 | | |
| | | 9 | | public interface IOzonPostingCancellationService |
| | | 10 | | { |
| | | 11 | | Task<OzonPostingCancellation> RecordAsync(int ozonClientId, string postingNumber, long? reasonId, |
| | | 12 | | string? reasonTitle, string? reasonMessage, string? reasonTypeId, DateTime occurredAt, string source, |
| | | 13 | | IReadOnlyCollection<int>? orderIds = null, CancellationToken cancellationToken = default); |
| | | 14 | | Task LinkOrdersAsync(IReadOnlyCollection<Order> orders, CancellationToken cancellationToken = default); |
| | | 15 | | Task LinkImportedOrdersAsync(IReadOnlyCollection<Order> orders, CancellationToken cancellationToken = default); |
| | | 16 | | Task PopulateLatestForOrdersAsync(IReadOnlyCollection<Order> orders, CancellationToken cancellationToken = default); |
| | | 17 | | } |
| | | 18 | | |
| | 1 | 19 | | public sealed class OzonPostingCancellationService(ApplicationDbContext db) : IOzonPostingCancellationService |
| | | 20 | | { |
| | | 21 | | public async Task<OzonPostingCancellation> RecordAsync(int ozonClientId, string postingNumber, long? reasonId, |
| | | 22 | | string? reasonTitle, string? reasonMessage, string? reasonTypeId, DateTime occurredAt, string source, |
| | | 23 | | IReadOnlyCollection<int>? orderIds = null, CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 2 | 25 | | var normalizedPosting = postingNumber.Trim(); |
| | 2 | 26 | | var fingerprint = CreateFingerprint(ozonClientId, normalizedPosting, reasonId, reasonMessage, occurredAt, source |
| | 2 | 27 | | var cancellation = await db.OzonPostingCancellations |
| | 2 | 28 | | .Include(x => x.OrderLinks) |
| | 2 | 29 | | .SingleOrDefaultAsync(x => x.EventFingerprint == fingerprint, cancellationToken); |
| | 2 | 30 | | if (cancellation == null) |
| | | 31 | | { |
| | 1 | 32 | | cancellation = new OzonPostingCancellation |
| | 1 | 33 | | { |
| | 1 | 34 | | OzonClientId = ozonClientId, PostingNumber = normalizedPosting, ReasonId = reasonId, |
| | 1 | 35 | | ReasonTitle = Trim(reasonTitle, 500), ReasonMessage = reasonMessage?.Trim(), |
| | 1 | 36 | | ReasonTypeId = Trim(reasonTypeId, 32), OccurredAt = occurredAt, Source = Trim(source, 20) ?? "Webhook", |
| | 1 | 37 | | EventFingerprint = fingerprint, CreatedAt = DateTime.UtcNow |
| | 1 | 38 | | }; |
| | 1 | 39 | | db.OzonPostingCancellations.Add(cancellation); |
| | 1 | 40 | | await db.SaveChangesAsync(cancellationToken); |
| | | 41 | | } |
| | | 42 | | |
| | 6 | 43 | | var ids = orderIds?.Where(x => x > 0).Distinct().ToArray() ?? []; |
| | 2 | 44 | | if (ids.Length > 0) |
| | 2 | 45 | | await AddLinksAsync(cancellation, ids, cancellationToken); |
| | 2 | 46 | | return cancellation; |
| | 2 | 47 | | } |
| | | 48 | | |
| | | 49 | | public async Task LinkOrdersAsync(IReadOnlyCollection<Order> orders, CancellationToken cancellationToken = default) |
| | | 50 | | { |
| | 0 | 51 | | var candidates = orders.Where(x => x.Id > 0 && x.OzonClientId.HasValue && !string.IsNullOrWhiteSpace(x.ShipmentN |
| | 0 | 52 | | if (candidates.Count == 0) return; |
| | 0 | 53 | | var clientIds = candidates.Select(x => x.OzonClientId!.Value).Distinct().ToArray(); |
| | 0 | 54 | | var postings = candidates.Select(x => x.ShipmentNumber!.Trim()).Distinct(StringComparer.OrdinalIgnoreCase).ToArr |
| | 0 | 55 | | var cancellations = await db.OzonPostingCancellations |
| | 0 | 56 | | .Where(x => clientIds.Contains(x.OzonClientId) && postings.Contains(x.PostingNumber)) |
| | 0 | 57 | | .ToListAsync(cancellationToken); |
| | 0 | 58 | | foreach (var cancellation in cancellations) |
| | | 59 | | { |
| | 0 | 60 | | var ids = candidates.Where(x => x.OzonClientId == cancellation.OzonClientId && |
| | 0 | 61 | | string.Equals(x.ShipmentNumber!.Trim(), cancellation.PostingNumber, StringC |
| | 0 | 62 | | .Select(x => x.Id).ToArray(); |
| | 0 | 63 | | await AddLinksAsync(cancellation, ids, cancellationToken); |
| | | 64 | | } |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | public async Task LinkImportedOrdersAsync(IReadOnlyCollection<Order> orders, CancellationToken cancellationToken = d |
| | | 68 | | { |
| | 0 | 69 | | var candidates = orders.Where(x => (x.OzonClientId.HasValue || x.OzonClient != null) && !string.IsNullOrWhiteSpa |
| | 0 | 70 | | .Select(x => new { ClientId = x.OzonClientId ?? x.OzonClient!.Id, Posting = x.ShipmentNumber!.Trim() }).Dist |
| | 0 | 71 | | if (candidates.Count == 0) return; |
| | 0 | 72 | | var clientIds = candidates.Select(x => x.ClientId).Distinct().ToArray(); |
| | 0 | 73 | | var postings = candidates.Select(x => x.Posting).Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); |
| | 0 | 74 | | var persisted = await db.Orders.Where(x => x.OzonClientId.HasValue && clientIds.Contains(x.OzonClientId.Value) & |
| | 0 | 75 | | x.ShipmentNumber != null && postings.Contains(x.ShipmentNumber)) |
| | 0 | 76 | | .ToListAsync(cancellationToken); |
| | 0 | 77 | | await LinkOrdersAsync(persisted, cancellationToken); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | public async Task PopulateLatestForOrdersAsync(IReadOnlyCollection<Order> orders, CancellationToken cancellationToke |
| | | 81 | | { |
| | 0 | 82 | | var ids = orders.Select(x => x.Id).Where(x => x > 0).Distinct().ToArray(); |
| | 0 | 83 | | if (ids.Length == 0) return; |
| | 0 | 84 | | var latest = await db.OzonPostingCancellationOrders.AsNoTracking() |
| | 0 | 85 | | .Where(x => ids.Contains(x.OrderId)) |
| | 0 | 86 | | .Select(x => new { x.OrderId, x.Cancellation }) |
| | 0 | 87 | | .GroupBy(x => x.OrderId) |
| | 0 | 88 | | .Select(g => g.OrderByDescending(x => x.Cancellation.OccurredAt).ThenByDescending(x => x.Cancellation.Id).Fi |
| | 0 | 89 | | .ToListAsync(cancellationToken); |
| | 0 | 90 | | var byOrder = latest.ToDictionary(x => x.OrderId, x => x.Cancellation); |
| | 0 | 91 | | foreach (var order in orders) |
| | 0 | 92 | | order.LatestOzonPostingCancellation = byOrder.GetValueOrDefault(order.Id); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | private async Task AddLinksAsync(OzonPostingCancellation cancellation, IReadOnlyCollection<int> orderIds, Cancellati |
| | | 96 | | { |
| | 2 | 97 | | var existing = await db.OzonPostingCancellationOrders |
| | 2 | 98 | | .Where(x => x.CancellationId == cancellation.Id && orderIds.Contains(x.OrderId)) |
| | 2 | 99 | | .Select(x => x.OrderId).ToListAsync(ct); |
| | 4 | 100 | | var links = orderIds.Except(existing).Select(id => new OzonPostingCancellationOrder { CancellationId = cancellat |
| | 3 | 101 | | if (links.Count == 0) return; |
| | 1 | 102 | | db.OzonPostingCancellationOrders.AddRange(links); |
| | 1 | 103 | | await db.SaveChangesAsync(ct); |
| | 2 | 104 | | } |
| | | 105 | | |
| | | 106 | | private static string CreateFingerprint(int clientId, string posting, long? reasonId, string? message, DateTime occu |
| | | 107 | | { |
| | 2 | 108 | | var input = $"{clientId}|{posting}|{reasonId}|{message?.Trim()}|{occurredAt:O}|{source}"; |
| | 2 | 109 | | return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(input))); |
| | | 110 | | } |
| | 3 | 111 | | private static string? Trim(string? value, int max) => string.IsNullOrWhiteSpace(value) ? null : value.Trim()[..Math |
| | | 112 | | } |