Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 37 additions & 30 deletions EstateReportingAPI.BusinessLogic/ReportingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public async Task<Result<Contract>> GetContract(ContractQueries.GetContractQuery
using ResolvedDbContext<EstateManagementContext>? resolvedContext = this.Resolver.Resolve(EstateManagementDatabaseName, request.EstateId.ToString());
await using EstateManagementContext context = resolvedContext.Context;

// Step 1: load contracts with operator name via a left-join (translatable)
// Step 1: load contract with operator name via a left-join (translatable)
var baseContractQuery = (from c in context.Contracts
join o in context.Operators on c.OperatorId equals o.OperatorId into ops
from o in ops.DefaultIfEmpty()
Expand All @@ -322,9 +322,27 @@ from o in ops.DefaultIfEmpty()

var baseContract = baseContractQueryResult.Data;

// Steps 2 & 3: load products and fees, then assemble
var productsResult = await this.LoadProductsWithFees(context, baseContract.ContractId, cancellationToken);

// Step 2: load related products for all contracts in one query
var productsQuery = context.ContractProducts.Where(cp => cp.ContractId == baseContract.ContractId).Select(cp => new {
if (productsResult.IsFailed)
return ResultHelpers.CreateFailure(productsResult);

return Result.Success(new Contract {
ContractId = baseContract.ContractId,
ContractReportingId = baseContract.ContractReportingId,
Description = baseContract.Description,
EstateId = baseContract.EstateId,
OperatorName = baseContract.OperatorName,
OperatorId = baseContract.OperatorId,
Products = productsResult.Data
});
}

private async Task<Result<List<Models.ContractProduct>>> LoadProductsWithFees(EstateManagementContext context,
Guid contractId,
CancellationToken cancellationToken) {
var productsQuery = context.ContractProducts.Where(cp => cp.ContractId == contractId).Select(cp => new {
cp.ContractProductId,
cp.ContractId,
cp.DisplayText,
Expand All @@ -341,7 +359,6 @@ from o in ops.DefaultIfEmpty()
var products = productsQueryResult.Data;
var productIds = products.Select(p => p.ContractProductId).ToList();

// Step 3: load fees for those products in one query
var feesQuery = context.ContractProductTransactionFees.Where(tf => productIds.Contains(tf.ContractProductId)).Select(tf => new {
tf.CalculationType,
tf.ContractProductTransactionFeeId,
Expand All @@ -357,33 +374,23 @@ from o in ops.DefaultIfEmpty()
return ResultHelpers.CreateFailure(feesQueryResult);

var fees = feesQueryResult.Data;

// Assemble the model in memory
Contract result = new Contract {
ContractId = baseContract.ContractId,
ContractReportingId = baseContract.ContractReportingId,
Description = baseContract.Description,
EstateId = baseContract.EstateId,
OperatorName = baseContract.OperatorName,
OperatorId = baseContract.OperatorId,
Products = products.Where(p => p.ContractId == baseContract.ContractId).Select(p => new Models.ContractProduct {
ContractId = p.ContractId,
ProductId = p.ContractProductId,
DisplayText = p.DisplayText,
ProductName = p.ProductName,
ProductType = p.ProductType,
Value = p.Value,
TransactionFees = fees.Where(f => f.ContractProductId == p.ContractProductId).Select(f => new ContractProductTransactionFee {
Description = f.Description,
Value = f.Value,
CalculationType = f.CalculationType,
FeeType = f.FeeType,
TransactionFeeId = f.ContractProductTransactionFeeId
}).ToList()
var feesLookup = fees.ToLookup(f => f.ContractProductId);

return Result.Success(products.Select(p => new Models.ContractProduct {
ContractId = p.ContractId,
ProductId = p.ContractProductId,
DisplayText = p.DisplayText,
ProductName = p.ProductName,
ProductType = p.ProductType,
Value = p.Value,
TransactionFees = feesLookup[p.ContractProductId].Select(f => new ContractProductTransactionFee {
Description = f.Description,
Value = f.Value,
CalculationType = f.CalculationType,
FeeType = f.FeeType,
TransactionFeeId = f.ContractProductTransactionFeeId
}).ToList()
};

return Result.Success(result);
}).ToList());
}

public async Task<Result<List<Contract>>> GetRecentContracts(ContractQueries.GetRecentContractsQuery request,
Expand Down
Loading