< Summary

Information
Class: OzonRepositories.Data.OrderShipmentTotalsRepository
Assembly: OzonRepositories
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonRepositories/Data/OrderShipmentTotalsRepository.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 74
Coverable lines: 74
Total lines: 128
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 48
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%
SetOrUpdateAsync()0%420200%
GetByOrderIdForUpsertAsync()100%210%
GetHistoryByOrderIdAsync()100%210%
Add()100%210%
Delete()100%210%
Get()100%210%
GetAsync()0%620%
GetAsync()0%620%
Update()100%210%
Norm()100%210%
IsChanged(...)0%600240%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonRepositories/Data/OrderShipmentTotalsRepository.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace OzonRepositories.Data;
 6
 7public class OrderShipmentTotalsRepository : MainRepository, IOrderShipmentTotalsRepository
 8{
 09    public OrderShipmentTotalsRepository(ApplicationDbContext orderContext) : base(orderContext)
 10    {
 011    }
 12
 13    public async Task<OrderShipmentTotals> SetOrUpdateAsync(OrderShipmentTotals value)
 14    {
 15        // Не сохраняем пустые/нулевые результаты (когда запрос не прошёл или Ozon не вернул операции).
 016        var isEmptyOrZero =
 017            value.OperationsCount <= 0 ||
 018            Math.Abs(value.ListAmount) < 0.000001 ||
 019            string.IsNullOrWhiteSpace(value.OperationsJson) ||
 020            value.OperationsJson.Trim() is "[]" or "";
 21
 22        // Берём последнюю запись по OrderId (история).
 023        var existingLatest = await GetByOrderIdForUpsertAsync(value.OrderId).ConfigureAwait(false);
 024        if (isEmptyOrZero)
 025            return existingLatest ?? value;
 26
 027        if (existingLatest != null && !IsChanged(existingLatest, value))
 028            return existingLatest;
 29
 030        value.CreatedAt = DateTime.UtcNow;
 031        value.UpdatedAt = DateTime.UtcNow;
 032        await _context.OrderShipmentTotals.AddAsync(value).ConfigureAwait(false);
 033        await _context.SaveChangesAsync().ConfigureAwait(false);
 034        return value;
 035    }
 36
 37    public async Task<OrderShipmentTotals?> GetByOrderIdForUpsertAsync(int orderId)
 38    {
 039        return await _context.OrderShipmentTotals
 040            .Where(x => x.OrderId == orderId)
 041            .OrderByDescending(x => x.UpdatedAt)
 042            .ThenByDescending(x => x.Id)
 043            .FirstOrDefaultAsync()
 044            .ConfigureAwait(false);
 045    }
 46
 47    public async Task<List<OrderShipmentTotals>> GetHistoryByOrderIdAsync(int orderId)
 48    {
 049        return await _context.OrderShipmentTotals
 050            .AsNoTracking()
 051            .Where(x => x.OrderId == orderId)
 052            .OrderByDescending(x => x.UpdatedAt)
 053            .ThenByDescending(x => x.Id)
 054            .ToListAsync()
 055            .ConfigureAwait(false);
 056    }
 57
 58    public async Task<int> Add(OrderShipmentTotals value)
 59    {
 060        _context.OrderShipmentTotals.Add(value);
 061        return await _context.SaveChangesAsync().ConfigureAwait(false);
 062    }
 63
 64    public async Task<int> Delete(OrderShipmentTotals value)
 65    {
 066        _context.OrderShipmentTotals.Remove(value);
 067        return await _context.SaveChangesAsync().ConfigureAwait(false);
 068    }
 69
 70    public async Task<List<OrderShipmentTotals>> Get()
 71    {
 072        return await _context.OrderShipmentTotals
 073            .OrderByDescending(x => x.UpdatedAt)
 074            .ToListAsync()
 075            .ConfigureAwait(false);
 076    }
 77
 78    public async Task<OrderShipmentTotals?> GetAsync(int id)
 79    {
 080        var entity = await _context.OrderShipmentTotals
 081            .FirstOrDefaultAsync(x => x.Id == id)
 082            .ConfigureAwait(false);
 83
 084        return entity ?? throw new InvalidOperationException($"OrderShipmentTotals с Id={id} не найден.");
 085    }
 86
 87    public async Task<OrderShipmentTotals?> GetAsync(OrderShipmentTotals value)
 88    {
 089        var entity = await GetByOrderIdForUpsertAsync(value.OrderId).ConfigureAwait(false);
 090        return entity ?? throw new InvalidOperationException($"OrderShipmentTotals с OrderId={value.OrderId} не найден."
 091    }
 92
 93    public async Task<int> Update(OrderShipmentTotals value)
 94    {
 095        value.UpdatedAt = DateTime.UtcNow;
 096        _context.OrderShipmentTotals.Update(value);
 097        return await _context.SaveChangesAsync().ConfigureAwait(false);
 098    }
 99
 100    private static bool IsChanged(OrderShipmentTotals latest, OrderShipmentTotals incoming)
 101    {
 0102        static double Norm(double d) => Math.Round(d, 2);
 103
 0104        if (!string.Equals(latest.ShipmentNumber, incoming.ShipmentNumber, StringComparison.Ordinal))
 0105            return true;
 106
 0107        if (Norm(latest.ListAmount) != Norm(incoming.ListAmount))
 0108            return true;
 109
 0110        if (Norm(latest.AccrualsForSale) != Norm(incoming.AccrualsForSale)) return true;
 0111        if (Norm(latest.CompensationAmount) != Norm(incoming.CompensationAmount)) return true;
 0112        if (Norm(latest.ProcessingAndDelivery) != Norm(incoming.ProcessingAndDelivery)) return true;
 0113        if (Norm(latest.RefundsAndCancellations) != Norm(incoming.RefundsAndCancellations)) return true;
 0114        if (Norm(latest.SaleCommission) != Norm(incoming.SaleCommission)) return true;
 0115        if (Norm(latest.ServicesAmount) != Norm(incoming.ServicesAmount)) return true;
 0116        if (Norm(latest.MoneyTransfer) != Norm(incoming.MoneyTransfer)) return true;
 0117        if (Norm(latest.OthersAmount) != Norm(incoming.OthersAmount)) return true;
 118
 0119        if (latest.OperationsCount != incoming.OperationsCount)
 0120            return true;
 121
 0122        if (!string.Equals(latest.OperationsJson, incoming.OperationsJson, StringComparison.Ordinal))
 0123            return true;
 124
 0125        return false;
 126    }
 127}
 128