From ae78b80165f516b79c45d862219a6a40df098f9a Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Thu, 12 Mar 2026 18:32:51 +0000 Subject: [PATCH] Refactor KPI logic to use LastSale from projection state Switched merchant KPI calculations to use the LastSale property from MerchantBalanceProjectionState instead of LastSaleDate/LastSaleDateTime from Merchant. Updated test data setup to set LastSale on the projection state, centralizing last sale tracking. --- EstateReportingAPI.BusinessLogic/ReportingManager.cs | 8 ++++---- EstateReportingAPI.IntegrationTests/DatabaseHelper.cs | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/EstateReportingAPI.BusinessLogic/ReportingManager.cs b/EstateReportingAPI.BusinessLogic/ReportingManager.cs index 8361301..e97d4f1 100644 --- a/EstateReportingAPI.BusinessLogic/ReportingManager.cs +++ b/EstateReportingAPI.BusinessLogic/ReportingManager.cs @@ -569,7 +569,7 @@ public async Task> GetMerchantsTransactionKpis(MerchantQueri using ResolvedDbContext? resolvedContext = this.Resolver.Resolve(EstateManagementDatabaseName, request.EstateId.ToString()); await using EstateManagementContext context = resolvedContext.Context; - var merchantsQuery = context.Merchants.Select(m => new { m.Name, m.LastSaleDate, m.LastSaleDateTime }); + var merchantsQuery = context.MerchantBalanceProjectionState.Select(m => new { m.MerchantName, m.LastSale }); var merchantsQueryResult = await ExecuteQuerySafeToList(merchantsQuery, cancellationToken, "Error retrieving merchants for KPI's"); if (merchantsQueryResult.IsFailed) return ResultHelpers.CreateFailure(merchantsQueryResult); @@ -578,11 +578,11 @@ public async Task> GetMerchantsTransactionKpis(MerchantQueri DateTime now = DateTime.Now; - Int32 merchantsWithSaleInLastHour = (from m in merchants where m.LastSaleDateTime >= now.AddHours(-1) && m.LastSaleDateTime <= now select m).Count(); + Int32 merchantsWithSaleInLastHour = (from m in merchants where m.LastSale >= now.AddHours(-1) && m.LastSale <= now select m).Count(); - Int32 merchantsWithNoSaleToday = (from m in merchants where m.LastSaleDate >= now.Date.AddDays(-7) && m.LastSaleDate <= now.Date.AddDays(-1) select m).Count(); + Int32 merchantsWithNoSaleToday = (from m in merchants where m.LastSale >= now.AddDays(-7) && m.LastSale <= now.AddDays(-1) select m).Count(); - Int32 merchantsWithNoSaleInLast7Days = (from m in merchants where m.LastSaleDate <= now.Date.AddDays(-7) select m).Count(); + Int32 merchantsWithNoSaleInLast7Days = (from m in merchants where m.LastSale <= now.AddDays(-7) select m).Count(); MerchantKpi response = new() { MerchantsWithSaleInLastHour = merchantsWithSaleInLastHour, MerchantsWithNoSaleToday = merchantsWithNoSaleToday, MerchantsWithNoSaleInLast7Days = merchantsWithNoSaleInLast7Days }; diff --git a/EstateReportingAPI.IntegrationTests/DatabaseHelper.cs b/EstateReportingAPI.IntegrationTests/DatabaseHelper.cs index 59ac51f..d109ff5 100644 --- a/EstateReportingAPI.IntegrationTests/DatabaseHelper.cs +++ b/EstateReportingAPI.IntegrationTests/DatabaseHelper.cs @@ -380,8 +380,8 @@ public async Task AddMerchant(String estateName, EstateId = estate.EstateId, MerchantId = Guid.NewGuid(), Name = merchantName, - LastSaleDate = lastSaleDateTime.Date, - LastSaleDateTime = lastSaleDateTime + //LastSaleDate = lastSaleDateTime.Date, + //LastSaleDateTime = lastSaleDateTime }; await this.Context.Merchants.AddAsync(merchant); @@ -452,7 +452,8 @@ await this.Context.MerchantContracts.AddAsync(new MerchantContract { MerchantBalanceProjectionState state = new MerchantBalanceProjectionState { Balance = balance, MerchantId = savedMerchant.MerchantId, - MerchantName = savedMerchant.Name + MerchantName = savedMerchant.Name, + LastSale = lastSaleDateTime, }; await this.Context.MerchantBalanceProjectionState.AddAsync(state);