Skip to content

Commit 5f358a8

Browse files
small further refactor
1 parent 374023d commit 5f358a8

2 files changed

Lines changed: 88 additions & 12 deletions

File tree

EstateReportingAPI.BusinessLogic/DatabaseProjections.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ namespace EstateReportingAPI.BusinessLogic
1616
{
1717

1818
public class DatabaseProjections {
19+
public class SettlementGroupProjection
20+
{
21+
public decimal SettledValue { get; set; }
22+
public int SettledCount { get; set; }
23+
24+
public decimal UnSettledValue { get; set; }
25+
public int UnSettledCount { get; set; }
26+
}
27+
28+
29+
30+
1931
public class FeeTransactionProjection
2032
{
2133
public MerchantSettlementFee Fee { get; set; }

EstateReportingAPI.BusinessLogic/ReportingManager.cs

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -539,25 +539,89 @@ public async Task<TodaysSettlement> GetTodaysSettlement(Guid estateId,
539539
todaySettlementData = todaySettlementData.ApplyMerchantFilter(merchantReportingId).ApplyOperatorFilter(operatorReportingId);
540540
comparisonSettlementData = comparisonSettlementData.ApplyMerchantFilter(merchantReportingId).ApplyOperatorFilter(operatorReportingId);
541541

542-
var todaySettlement = await (from f in todaySettlementData group f by f.Fee.IsSettled into grouped select new { IsSettled = grouped.Key, CalculatedValueSum = grouped.Sum(item => item.Fee.CalculatedValue), CalculatedValueCount = grouped.Count() }).ToListAsync(cancellationToken);
543-
544-
var comparisonSettlement = await (from f in comparisonSettlementData group f by f.Fee.IsSettled into grouped select new { IsSettled = grouped.Key, CalculatedValueSum = grouped.Sum(item => item.Fee.CalculatedValue), CalculatedValueCount = grouped.Count() }).ToListAsync(cancellationToken);
545-
542+
DatabaseProjections.SettlementGroupProjection todaySettlement = await this.GetSettlementSummary(todaySettlementData, cancellationToken);
543+
DatabaseProjections.SettlementGroupProjection comparisonSettlement = await this.GetSettlementSummary(comparisonSettlementData, cancellationToken);
546544

547545
TodaysSettlement response = new() {
548-
ComparisonSettlementCount = comparisonSettlement.FirstOrDefault(x => x.IsSettled)?.CalculatedValueCount ?? 0,
549-
ComparisonSettlementValue = comparisonSettlement.FirstOrDefault(x => x.IsSettled)?.CalculatedValueSum ?? 0,
550-
ComparisonPendingSettlementCount = comparisonSettlement.FirstOrDefault(x => x.IsSettled == false)?.CalculatedValueCount ?? 0,
551-
ComparisonPendingSettlementValue = comparisonSettlement.FirstOrDefault(x => x.IsSettled == false)?.CalculatedValueSum ?? 0,
552-
TodaysSettlementCount = todaySettlement.FirstOrDefault(x => x.IsSettled)?.CalculatedValueCount ?? 0,
553-
TodaysSettlementValue = todaySettlement.FirstOrDefault(x => x.IsSettled)?.CalculatedValueSum ?? 0,
554-
TodaysPendingSettlementCount = todaySettlement.FirstOrDefault(x => x.IsSettled == false)?.CalculatedValueCount ?? 0,
555-
TodaysPendingSettlementValue = todaySettlement.FirstOrDefault(x => x.IsSettled == false)?.CalculatedValueSum ?? 0
546+
ComparisonSettlementCount = comparisonSettlement.SettledCount,
547+
ComparisonSettlementValue = comparisonSettlement.SettledValue,
548+
ComparisonPendingSettlementCount = comparisonSettlement.UnSettledCount,
549+
ComparisonPendingSettlementValue = comparisonSettlement.UnSettledValue,
550+
TodaysSettlementCount = todaySettlement.SettledCount,
551+
TodaysSettlementValue = todaySettlement.SettledValue,
552+
TodaysPendingSettlementCount = todaySettlement.UnSettledCount,
553+
TodaysPendingSettlementValue = todaySettlement.UnSettledValue
556554
};
557555

558556
return response;
559557
}
560558

559+
private async Task<DatabaseProjections.SettlementGroupProjection> GetSettlementSummary(
560+
IQueryable<DatabaseProjections.ComparisonSettlementTransactionProjection> query,
561+
CancellationToken cancellationToken)
562+
{
563+
// Get the settleed fees summary
564+
var settledFees = await (from f in query
565+
where f.Fee.IsSettled
566+
group f by f.Fee.IsSettled
567+
into grouped
568+
select new
569+
{
570+
Value = grouped.Sum(g => g.Fee.CalculatedValue),
571+
Count = grouped.Count()
572+
}).SingleOrDefaultAsync(cancellationToken);
573+
574+
var unSettledFees = await (from f in query
575+
where f.Fee.IsSettled
576+
group f by f.Fee.IsSettled == false
577+
into grouped
578+
select new
579+
{
580+
Value = grouped.Sum(g => g.Fee.CalculatedValue),
581+
Count = grouped.Count()
582+
}).SingleOrDefaultAsync(cancellationToken);
583+
584+
return new DatabaseProjections.SettlementGroupProjection
585+
{
586+
SettledCount = settledFees?.Count ?? 0,
587+
SettledValue = settledFees?.Value ?? 0,
588+
UnSettledCount = unSettledFees? .Count ?? 0,
589+
UnSettledValue = unSettledFees?.Value ?? 0
590+
};
591+
}
592+
593+
private async Task<DatabaseProjections.SettlementGroupProjection> GetSettlementSummary(
594+
IQueryable<DatabaseProjections.TodaySettlementTransactionProjection> query,
595+
CancellationToken cancellationToken) {
596+
597+
// Get the settleed fees summary
598+
var settledFees = await (from f in query
599+
where f.Fee.IsSettled
600+
group f by f.Fee.IsSettled
601+
into grouped
602+
select new {
603+
Value = grouped.Sum(g => g.Fee.CalculatedValue),
604+
Count= grouped.Count()
605+
}).SingleOrDefaultAsync(cancellationToken);
606+
607+
var unSettledFees = await (from f in query
608+
where f.Fee.IsSettled
609+
group f by f.Fee.IsSettled == false
610+
into grouped
611+
select new
612+
{
613+
Value = grouped.Sum(g => g.Fee.CalculatedValue),
614+
Count = grouped.Count()
615+
}).SingleOrDefaultAsync(cancellationToken);
616+
617+
return new DatabaseProjections.SettlementGroupProjection {
618+
SettledCount = settledFees.Count,
619+
SettledValue = settledFees.Value,
620+
UnSettledCount = unSettledFees.Count,
621+
UnSettledValue = unSettledFees.Value
622+
};
623+
}
624+
561625
public async Task<List<Operator>> GetOperators(Guid estateId,
562626
CancellationToken cancellationToken) {
563627
using ResolvedDbContext<EstateManagementContext>? resolvedContext = this.Resolver.Resolve(EstateManagementDatabaseName, estateId.ToString());

0 commit comments

Comments
 (0)