public async Task get_deposits_report_should_return_report() { var query = new GetDepositsReport(); var item = GetDepositReportItem(); var report = new DepositsReport(1, 1, 0, PagedResult <DepositReportItem> .Create(new[] { item }, 1, 1, 1, 1)); _depositReportService.GetAsync(query).Returns(report); var result = await _rpc.ndm_getDepositsReport(query); await _depositReportService.Received().GetAsync(query); result.Data.Should().NotBeNull(); result.Data.Deposits.Should().NotBeNull(); result.Data.Deposits.Items.Should().ContainSingle(); result.Data.Deposits.Page.Should().Be(1); result.Data.Deposits.Results.Should().Be(1); result.Data.Deposits.TotalPages.Should().Be(1); result.Data.Deposits.TotalResults.Should().Be(1); result.Data.Deposits.IsEmpty.Should().BeFalse(); result.Data.ClaimedValue.Should().Be(1); result.Data.RefundedValue.Should().Be(0); result.Data.RemainingValue.Should().Be(0); result.Data.TotalValue.Should().Be(1); VerifyDepositReportItem(result.Data.Deposits.Items.Single(), item); }
public async Task <DepositsReport> GetAsync(GetDepositsReport query) { var deposits = query.DepositId is null ? await _depositRepository.BrowseAsync(new GetDeposits { Results = int.MaxValue }) : PagedResult <DepositDetails> .Create(new[] { await _depositRepository.GetAsync(query.DepositId) }, 1, 1, 1, 1); if (!deposits.Items.Any() || deposits.Items.Any(d => d is null)) { return(DepositsReport.Empty); } var foundDeposits = deposits.Items .Where(d => (query.Provider is null || d.DataAsset.Provider.Address == query.Provider) && (query.AssetId is null || d.DataAsset.Id == query.AssetId)) .ToDictionary(d => d.Id, d => d); if (!foundDeposits.Any()) { return(DepositsReport.Empty); } var assetIds = foundDeposits.Select(d => d.Value.DataAsset.Id); var receipts = await _receiptRepository.BrowseAsync(query.DepositId, query.AssetId); var depositsReceipts = receipts.Where(r => assetIds.Contains(r.DataAssetId)) .GroupBy(r => r.DepositId).ToDictionary(r => r.Key, r => r.AsEnumerable()); var page = query.Page; if (page <= 0) { page = 1; } var results = query.Results; if (results <= 0) { results = 10; } var timestamp = (uint)_timestamper.EpochSeconds; var skip = (page - 1) * results; var items = new List <DepositReportItem>(); foreach (var(_, deposit) in foundDeposits.OrderByDescending(d => d.Value.Timestamp).Skip(skip) .Take(results)) { depositsReceipts.TryGetValue(deposit.Id, out var depositReceipts); var expired = deposit.IsExpired(timestamp); var receiptItems = depositReceipts?.Select(r => new DataDeliveryReceiptReportItem(r.Id, r.Number, r.SessionId, r.ConsumerNodeId, r.Request, r.Receipt, r.Timestamp, r.IsMerged, r.IsClaimed)); var sessions = await _sessionRepository.BrowseAsync(new GetConsumerSessions { DepositId = deposit.Id, Results = int.MaxValue }); var consumedUnits = sessions.Items.Any() ? (uint)sessions.Items.Sum(s => s.ConsumedUnits) : 0; items.Add(new DepositReportItem(deposit.Id, deposit.DataAsset.Id, deposit.DataAsset.Name, deposit.DataAsset.Provider.Address, deposit.DataAsset.Provider.Name, deposit.Deposit.Value, deposit.Deposit.Units, deposit.Consumer, deposit.Timestamp, deposit.Deposit.ExpiryTime, expired, deposit.Transaction?.Hash, deposit.ConfirmationTimestamp, deposit.Confirmations, deposit.RequiredConfirmations, deposit.Confirmed, deposit.Rejected, deposit.ClaimedRefundTransaction?.Hash, deposit.RefundClaimed, consumedUnits, receiptItems)); } var(total, claimed, refunded) = CalculateValues(foundDeposits, depositsReceipts); var totalResults = foundDeposits.Count; var totalPages = (int)Math.Ceiling((double)totalResults / query.Results); return(new DepositsReport(total, claimed, refunded, PagedResult <DepositReportItem> .Create(items.OrderByDescending(i => i.Timestamp).ToList(), query.Page, query.Results, totalPages, totalResults))); }
public async Task <DepositsReport> GetAsync(GetDepositsReport query) { PagedResult <DepositDetails> deposits; if (query.DepositId == null) { deposits = await _depositRepository.BrowseAsync(new GetDeposits { Results = int.MaxValue }); } else { DepositDetails?detailsOfOne = await _depositRepository.GetAsync(query.DepositId); if (detailsOfOne is null) { return(DepositsReport.Empty); } deposits = PagedResult <DepositDetails> .Create(new[] { detailsOfOne }, 1, 1, 1, 1); } if (deposits.Items.Count == 0) { return(DepositsReport.Empty); } if (!deposits.Items.Any() || deposits.Items.Any(d => d is null)) { return(DepositsReport.Empty); } Dictionary <Keccak, DepositDetails> foundDeposits = deposits.Items .Where(d => (query.Provider is null || d.DataAsset.Provider.Address == query.Provider) && (query.AssetId is null || d.DataAsset.Id == query.AssetId)) .ToDictionary(d => d.Id, d => d); if (!foundDeposits.Any()) { return(DepositsReport.Empty); } IEnumerable <Keccak> assetIds = foundDeposits.Select(d => d.Value.DataAsset.Id); IReadOnlyList <DataDeliveryReceiptDetails> receipts = await _receiptRepository.BrowseAsync(query.DepositId, query.AssetId); Dictionary <Keccak, IEnumerable <DataDeliveryReceiptDetails> > depositsReceipts = receipts.Where(r => assetIds.Contains(r.DataAssetId)) .GroupBy(r => r.DepositId).ToDictionary(r => r.Key, r => r.AsEnumerable()); int page = query.Page; if (page <= 0) { page = 1; } int results = query.Results; if (results <= 0) { results = 10; } uint timestamp = (uint)_timestamper.EpochSeconds; int skip = (page - 1) * results; List <DepositReportItem> items = new List <DepositReportItem>(); foreach ((Keccak _, DepositDetails deposit) in foundDeposits.OrderByDescending(d => d.Value.Timestamp).Skip(skip) .Take(results)) { depositsReceipts.TryGetValue(deposit.Id, out IEnumerable <DataDeliveryReceiptDetails>?depositReceipts); bool expired = deposit.IsExpired(timestamp); IEnumerable <DataDeliveryReceiptReportItem>?receiptItems = depositReceipts?.Select(r => new DataDeliveryReceiptReportItem(r.Id, r.Number, r.SessionId, r.ConsumerNodeId, r.Request, r.Receipt, r.Timestamp, r.IsMerged, r.IsClaimed)); PagedResult <ConsumerSession> sessions = await _sessionRepository.BrowseAsync(new GetConsumerSessions { DepositId = deposit.Id, Results = int.MaxValue }); uint consumedUnits = sessions.Items.Any() ? (uint)sessions.Items.Sum(s => s.ConsumedUnits) : 0; items.Add(ToReportItem(deposit, expired, consumedUnits, receiptItems ?? Enumerable.Empty <DataDeliveryReceiptReportItem>())); } (UInt256 total, UInt256 claimed, UInt256 refunded) = CalculateValues(foundDeposits, depositsReceipts); int totalResults = foundDeposits.Count; int totalPages = (int)Math.Ceiling((double)totalResults / query.Results); return(new DepositsReport(total, claimed, refunded, PagedResult <DepositReportItem> .Create(items.OrderByDescending(i => i.Timestamp).ToList(), query.Page, query.Results, totalPages, totalResults))); }