Skip to content
Merged
Show file tree
Hide file tree
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
83 changes: 0 additions & 83 deletions EstateReportingAPI.BusinessLogic/ReportingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -469,89 +469,6 @@ group f by f.IsSettled into grouped
return response;
}

public async Task<List<TopBottomData>> GetTopBottomData(Guid estateId, TopBottom direction, Int32 resultCount, Dimension dimension, CancellationToken cancellationToken){
using ResolvedDbContext<EstateManagementContext>? resolvedContext = this.Resolver.Resolve(EstateManagementDatabaseName, estateId.ToString());
await using EstateManagementContext context = resolvedContext.Context;

IQueryable<TodayTransaction> mainQuery = context.TodayTransactions
.Where(joined => joined.IsAuthorised == true
&& joined.TransactionType == "Sale"
&& joined.TransactionDate == DateTime.Now.Date);

IQueryable<TopBottomData> queryable = null;
if (dimension == Dimension.Product)
{
// Products
queryable = mainQuery
.Join(context.ContractProducts,
t => t.ContractProductReportingId,
contractProduct => contractProduct.ContractProductReportingId,
(t, contractProduct) => new
{
Transaction = t,
ContractProduct = contractProduct
})
.GroupBy(joined => joined.ContractProduct.ProductName)
.Select(g => new TopBottomData
{
DimensionName = g.Key,
SalesValue = g.Sum(t => t.Transaction.TransactionAmount)
});
}
else if (dimension == Dimension.Operator)
{
// Operators
queryable = mainQuery
.Join(context.Operators,
t => t.OperatorReportingId,
o => o.OperatorReportingId,
(t, o) => new
{
Transaction = t,
Operator = o
})
.GroupBy(joined => joined.Operator.Name)
.Select(g => new TopBottomData
{
DimensionName = g.Key,
SalesValue = g.Sum(t => t.Transaction.TransactionAmount)
});
}
else if (dimension == Dimension.Merchant)
{
// Operators
queryable = mainQuery
.Join(context.Merchants,
t => t.MerchantReportingId,
merchant => merchant.MerchantReportingId,
(t, merchant) => new
{
Transaction = t,
Merchant = merchant
})
.GroupBy(joined => joined.Merchant.Name)
.Select(g => new TopBottomData
{
DimensionName = g.Key,
SalesValue = g.Sum(t => t.Transaction.TransactionAmount)
});
}

if (direction == TopBottom.Top)
{
// Top X
queryable = queryable.OrderByDescending(g => g.SalesValue);
}
else if (direction == TopBottom.Bottom)
{
// Bottom X
queryable = queryable.OrderBy(g => g.SalesValue);
}

// TODO: bad request??
return await queryable.Take(resultCount).ToListAsync(cancellationToken);
}

private Int32 SafeDivide(Int32 number, Int32 divisor)
{
if (divisor == 0)
Expand Down
58 changes: 58 additions & 0 deletions EstateReportingAPI.BusinessLogic/ReportingManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,64 @@ join op in context.Operators on c.OperatorId equals op.OperatorId
};
}

public static IQueryable<TopBottomData> ApplyMerchantGrouping(this IQueryable<TodayTransaction> transactions,
EstateManagementContext context)
{
return transactions.Join(context.Merchants,
t => t.MerchantReportingId,
merchant => merchant.MerchantReportingId,
(t, merchant) => new
{
Transaction = t,
Merchant = merchant
})
.GroupBy(joined => joined.Merchant.Name)
.Select(g => new TopBottomData
{
DimensionName = g.Key,
SalesValue = g.Sum(t => t.Transaction.TransactionAmount)
});
}

public static IQueryable<TopBottomData> ApplyOperatorGrouping(this IQueryable<TodayTransaction> transactions,
EstateManagementContext context)
{
return transactions.Join(context.Operators,
t => t.OperatorReportingId,
o => o.OperatorReportingId,
(t, o) => new
{
Transaction = t,
Operator = o
})
.GroupBy(joined => joined.Operator.Name)
.Select(g => new TopBottomData
{
DimensionName = g.Key,
SalesValue = g.Sum(t => t.Transaction.TransactionAmount)
});
}

public static IQueryable<TopBottomData> ApplyProductGrouping(this IQueryable<TodayTransaction> transactions,
EstateManagementContext context)
{
return transactions
.Join(context.ContractProducts,
t => t.ContractProductReportingId,
contractProduct => contractProduct.ContractProductReportingId,
(t, contractProduct) => new
{
Transaction = t,
ContractProduct = contractProduct
})
.GroupBy(joined => joined.ContractProduct.ProductName)
.Select(g => new TopBottomData
{
DimensionName = g.Key,
SalesValue = g.Sum(t => t.Transaction.TransactionAmount)
});
}

public static IQueryable<UnsettledFee> ApplyMerchantGrouping(this IQueryable<DatabaseProjections.FeeTransactionProjection> fees,
EstateManagementContext context)
{
Expand Down
31 changes: 31 additions & 0 deletions EstateReportingAPI.BusinessLogic/ReportingManagerRefactored.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ public async Task<TodaysSales> GetOperatorPerformance(Guid estateId, DateTime co
return response;
}

public async Task<List<TopBottomData>> GetTopBottomData(Guid estateId,
TopBottom direction,
Int32 resultCount,
Dimension dimension,
CancellationToken cancellationToken) {
using ResolvedDbContext<EstateManagementContext>? resolvedContext = this.Resolver.Resolve(EstateManagementDatabaseName, estateId.ToString());
await using EstateManagementContext context = resolvedContext.Context;

IQueryable<TodayTransaction> mainQuery = BuildTodaySalesQuery(context);

IQueryable<TopBottomData> topBottomData = dimension switch
{
Dimension.Merchant => mainQuery.ApplyMerchantGrouping(context),
Dimension.Operator => mainQuery.ApplyOperatorGrouping(context),
Dimension.Product => mainQuery.ApplyProductGrouping(context),
};

topBottomData = direction switch {
TopBottom.Top => topBottomData.OrderByDescending(g => g.SalesValue),
_ => topBottomData.OrderBy(g => g.SalesValue)
};

return await topBottomData.Take(resultCount).ToListAsync(cancellationToken);
}

}

public class DatabaseProjections {
Expand All @@ -211,5 +236,11 @@ public class TransactionSearchProjection {
public Merchant Merchant { get; set; }
public ContractProduct Product { get; set; }
}

public class TopBottomData
{
public String DimensionName { get; set; }
public Decimal SalesValue { get; set; }
}
}
}
Loading