< Summary

Information
Class: OzonOrdersWeb.WebServices.Orders.OrderReturnInfoService
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/Orders/OrderReturnInfoService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 82
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
PopulateForOrdersAsync()0%4260%
GetWarehouseOptionsAsync()100%210%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/WebServices/Orders/OrderReturnInfoService.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace OzonOrdersWeb.WebServices.Orders;
 6
 7public interface IOrderReturnInfoService
 8{
 9    Task PopulateForOrdersAsync(IReadOnlyList<Order> ordersForPage, CancellationToken cancellationToken = default);
 10
 11    Task<List<string>> GetWarehouseOptionsAsync(CancellationToken cancellationToken = default);
 12}
 13
 14public sealed class OrderReturnInfoService : IOrderReturnInfoService
 15{
 16    private readonly ApplicationDbContext _context;
 17
 018    public OrderReturnInfoService(ApplicationDbContext context)
 19    {
 020        _context = context;
 021    }
 22
 23    public async Task PopulateForOrdersAsync(IReadOnlyList<Order> ordersForPage, CancellationToken cancellationToken = d
 24    {
 025        var returnedOrderIds = ordersForPage.Where(o => o.IsReturned).Select(o => o.Id).ToList();
 026        if (returnedOrderIds.Count == 0)
 27        {
 028            return;
 29        }
 30
 031        var returnInfoMap = await _context.OrderReturns
 032            .Where(r => r.OrderId.HasValue && returnedOrderIds.Contains(r.OrderId.Value))
 033            .GroupBy(r => r.OrderId!.Value)
 034            .Select(g => new
 035            {
 036                OrderId = g.Key,
 037                Barcode = g.OrderByDescending(x => x.UpdatedAt).Select(x => x.LogisticBarcode).FirstOrDefault(),
 038                ReturnStatusSysName = g.OrderByDescending(x => x.UpdatedAt).Select(x => x.VisualStatusSysName).FirstOrDe
 039                ReturnStatusDisplayName = g.OrderByDescending(x => x.UpdatedAt).Select(x => x.VisualStatusDisplayName).F
 040                ReturnWarehouseName = g.OrderByDescending(x => x.UpdatedAt).Select(x =>
 041                    x.PlaceName ?? x.TargetPlaceName).FirstOrDefault(),
 042                ReturnStatusChangeDate = g.OrderByDescending(x => x.UpdatedAt).Select(x => x.VisualStatusChangeMoment).F
 043            })
 044            .ToDictionaryAsync(x => x.OrderId, x => x, cancellationToken);
 45
 046        foreach (var order in ordersForPage)
 47        {
 048            if (!returnInfoMap.TryGetValue(order.Id, out var returnInfo))
 49            {
 50                continue;
 51            }
 52
 053            order.ReturnLogisticBarcode = returnInfo.Barcode;
 054            order.ReturnStatusSysName = returnInfo.ReturnStatusSysName;
 055            order.ReturnStatusDisplayName = returnInfo.ReturnStatusDisplayName;
 056            order.ReturnWarehouseName = returnInfo.ReturnWarehouseName;
 057            order.ReturnStatusChangeDate = returnInfo.ReturnStatusChangeDate;
 58        }
 059    }
 60
 61    public async Task<List<string>> GetWarehouseOptionsAsync(CancellationToken cancellationToken = default)
 62    {
 063        var placeNames = await _context.OrderReturns
 064            .Where(r => !string.IsNullOrWhiteSpace(r.PlaceName))
 065            .Select(r => r.PlaceName!)
 066            .Distinct()
 067            .ToListAsync(cancellationToken);
 68
 069        var targetPlaceNames = await _context.OrderReturns
 070            .Where(r => !string.IsNullOrWhiteSpace(r.TargetPlaceName))
 071            .Select(r => r.TargetPlaceName!)
 072            .Distinct()
 073            .ToListAsync(cancellationToken);
 74
 075        return placeNames
 076            .Concat(targetPlaceNames)
 077            .Where(n => !string.IsNullOrWhiteSpace(n))
 078            .Distinct(StringComparer.OrdinalIgnoreCase)
 079            .OrderBy(n => n)
 080            .ToList();
 081    }
 82}