| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Newtonsoft.Json.Linq; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonDomains.Models.SupplierStatuses; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | using Services.ApiServices.ShateMApi; |
| | | 7 | | |
| | | 8 | | namespace Services.SupplierStatusServices.Providers; |
| | | 9 | | |
| | | 10 | | /// <summary>Сверяет внешнюю корзину и заказы Шате‑М, не меняя статус при ошибке API.</summary> |
| | | 11 | | public sealed class ShateMSupplierStatusProvider : ISupplierStatusProvider |
| | | 12 | | { |
| | | 13 | | private readonly ApplicationDbContext _db; |
| | | 14 | | private readonly ShateMDataManager _api; |
| | 50 | 15 | | public ShateMSupplierStatusProvider(ApplicationDbContext db, ShateMDataManager api) => (_db, _api) = (db, api); |
| | 2 | 16 | | public string ProviderKey => ApiSupplierSettings.ShateMProviderKey; |
| | | 17 | | |
| | | 18 | | public async Task<IReadOnlyList<SupplierStatusSnapshot>> FetchCartAsync(IReadOnlyList<OrderSupplierStateRef> states, |
| | | 19 | | { |
| | | 20 | | try |
| | | 21 | | { |
| | 12 | 22 | | var response = await _api.GetAsync("/api/v1/cartitems", null, ct); |
| | 33 | 23 | | var external = (response as JArray ?? new JArray()).OfType<JObject>().Select(x => x.Value<string>("id")).Whe |
| | 9 | 24 | | var local = await _db.ShateMCartItems.Where(x => states.Select(s => s.OrderId).Contains(x.StudioOrderId) && |
| | 9 | 25 | | var result = new List<SupplierStatusSnapshot>(); |
| | 38 | 26 | | foreach (var state in states) |
| | | 27 | | { |
| | 20 | 28 | | var row = local.FirstOrDefault(x => x.StudioOrderId == state.OrderId); |
| | 10 | 29 | | var exists = row is not null && !string.IsNullOrWhiteSpace(row.ExternalBasketItemId) && external.Contain |
| | 22 | 30 | | if (!exists && row is not null) { row.Status = ShateMCartItemStatus.Removed; row.ActiveStudioOrderId = n |
| | 10 | 31 | | result.Add(new SupplierStatusSnapshot { OrderId = state.OrderId, Kind = SupplierStatusKind.Cart, Externa |
| | | 32 | | } |
| | 18 | 33 | | await _db.SaveChangesAsync(ct); return result; |
| | | 34 | | } |
| | 3 | 35 | | catch (Exception ex) when (ex is ShateMApiException or HttpRequestException or TaskCanceledException) |
| | | 36 | | { |
| | 6 | 37 | | return states.Select(x => new SupplierStatusSnapshot { OrderId = x.OrderId, Kind = SupplierStatusKind.Cart, |
| | | 38 | | } |
| | 12 | 39 | | } |
| | | 40 | | |
| | | 41 | | public async Task<IReadOnlyList<SupplierStatusSnapshot>> FetchOrderAsync(IReadOnlyList<OrderSupplierStateRef> states |
| | | 42 | | { |
| | | 43 | | try |
| | | 44 | | { |
| | 12 | 45 | | var response = await _api.GetAsync("/api/v1/orders", new Dictionary<string, string?> { ["page"] = "1", ["pag |
| | 17 | 46 | | var orders = (response as JArray ?? new JArray()).OfType<JObject>().ToDictionary(x => x.Value<string>("id") |
| | 9 | 47 | | return states.Select(state => |
| | 9 | 48 | | { |
| | 12 | 49 | | var id = state.ExternalOrderId ?? state.ExternalOrderNumber; |
| | 18 | 50 | | if (string.IsNullOrWhiteSpace(id) || !orders.TryGetValue(id, out var order)) return new SupplierStatusSn |
| | 6 | 51 | | var line = (order["orderItems"] as JArray)?.OfType<JObject>().FirstOrDefault(); |
| | 6 | 52 | | var code = line?.Value<string>("statusCode"); |
| | 6 | 53 | | return new SupplierStatusSnapshot { OrderId = state.OrderId, Kind = SupplierStatusKind.Order, ExternalCo |
| | 9 | 54 | | }).ToList(); |
| | | 55 | | } |
| | 3 | 56 | | catch (Exception ex) when (ex is ShateMApiException or HttpRequestException or TaskCanceledException) |
| | | 57 | | { |
| | 6 | 58 | | return states.Select(x => new SupplierStatusSnapshot { OrderId = x.OrderId, Kind = SupplierStatusKind.Order, |
| | | 59 | | } |
| | 12 | 60 | | } |
| | | 61 | | } |