| | | 1 | | using OzonOrdersWeb.Areas.Studio2.Models.Transactions; |
| | | 2 | | using Services.ApiServices.OzonApi; |
| | | 3 | | |
| | | 4 | | namespace OzonOrdersWeb.WebServices.Transactions.Grid.Commit.Steps; |
| | | 5 | | |
| | | 6 | | /// <summary>Отмена отправлений в Ozon до смены статусов; ошибка API останавливает проведение (как на классических стран |
| | | 7 | | public sealed class OzonCancelStep : ITransactionCommitStep |
| | | 8 | | { |
| | | 9 | | private readonly OzonOrderCancellationService? _cancellation; |
| | | 10 | | |
| | 0 | 11 | | public OzonCancelStep(OzonOrderCancellationService? cancellation = null) |
| | | 12 | | { |
| | 0 | 13 | | _cancellation = cancellation; |
| | 0 | 14 | | } |
| | | 15 | | |
| | 0 | 16 | | public string Key => TransactionCommitStepKeys.OzonCancel; |
| | | 17 | | |
| | | 18 | | public async Task ExecuteAsync(TransactionCommitContext ctx) |
| | | 19 | | { |
| | 0 | 20 | | if (_cancellation == null || !ctx.Definition.OzonCancelPanel) |
| | | 21 | | { |
| | 0 | 22 | | return; |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | var requests = ctx.Edits.Values |
| | 0 | 26 | | .Where(e => e.CancelInOzon && e.OzonCancelReasonId is > 0) |
| | 0 | 27 | | .Select(e => new OzonCancelFormRequest(e.Id, e.OzonCancelReasonId!.Value, e.OzonCancelReasonMessage, e.OzonC |
| | 0 | 28 | | .ToList(); |
| | 0 | 29 | | if (requests.Count == 0) |
| | | 30 | | { |
| | 0 | 31 | | return; |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | await ctx.StageAsync("ozon-cancel", "Отменяем отправления в Ozon", requests.Count == 1 ? "1 отправление" : $"отп |
| | 0 | 35 | | var result = await _cancellation.CancelOrdersAsync(requests, ctx.Orders.ToDictionary(o => o.Id)); |
| | 0 | 36 | | if (result.Errors.Count > 0) |
| | | 37 | | { |
| | 0 | 38 | | ctx.Fail(TransactionErrorCode.Unknown, string.Join("<br/>", result.Errors)); |
| | 0 | 39 | | return; |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | var html = OzonOrderCancellationService.BuildSuccessHtml(result.SuccessEntries); |
| | 0 | 43 | | if (!string.IsNullOrWhiteSpace(html)) |
| | | 44 | | { |
| | 0 | 45 | | ctx.SummaryBlocks.Add(html); |
| | | 46 | | } |
| | 0 | 47 | | } |
| | | 48 | | } |