diff --git a/DashboardService.E2ETest/PolicyServiceToDashboardServiceIntegrationTest.cs b/DashboardService.E2ETest/PolicyServiceToDashboardServiceIntegrationTest.cs index 1feef685..5d37bf55 100644 --- a/DashboardService.E2ETest/PolicyServiceToDashboardServiceIntegrationTest.cs +++ b/DashboardService.E2ETest/PolicyServiceToDashboardServiceIntegrationTest.cs @@ -113,7 +113,8 @@ public async Task PolicyCreation_Should_UpdateDashboardAnalytics() await Task.Delay(1000); // Step 4: Wait for event processing and verify dashboard analytics are updated var policyNumber = createPolicyResult.PolicyNumber; - var saleDate = policyDetails.Policy.DateFrom; + // Sales date is when the policy was created (now), not the coverage start date + var saleDate = DateTime.Now; // Try querying dashboard multiple times with a delay to account for eventual consistency GetTotalSalesResult totalSalesResult = null; @@ -281,11 +282,12 @@ public async Task MultiplePoliciesWithDifferentAgents_Should_ShowCorrectAggregat await Task.Delay(1000); // Verify total sales aggregation - var policyFrom = DateTime.Now.AddDays(5); + // Sales date is when policies were created (now), not the coverage start date + var saleDate = DateTime.Now; var totalSalesQuery = new GetTotalSalesQuery { - SalesDateFrom = policyFrom.AddDays(-1), - SalesDateTo = policyFrom.AddDays(1) + SalesDateFrom = saleDate.AddDays(-1), + SalesDateTo = saleDate.AddDays(1) }; var totalSalesScenario = await DashboardHost.Scenario(_ => @@ -308,8 +310,8 @@ public async Task MultiplePoliciesWithDifferentAgents_Should_ShowCorrectAggregat var agentSalesQuery = new GetAgentsSalesQuery { AgentLogin = agent, - SalesDateFrom = policyFrom.AddDays(-1), - SalesDateTo = policyFrom.AddDays(1) + SalesDateFrom = saleDate.AddDays(-1), + SalesDateTo = saleDate.AddDays(1) }; var agentSalesScenario = await DashboardHost.Scenario(_ => @@ -325,15 +327,15 @@ public async Task MultiplePoliciesWithDifferentAgents_Should_ShowCorrectAggregat True(agentSalesResult.PerAgentTotal.ContainsKey(agent), $"Agent {agent} should be in sales results"); var agentTotal = agentSalesResult.PerAgentTotal[agent]; - Equal((long)policyCounts[agent], agentTotal.PoliciesCount); + Equal(policyCounts[agent], agentTotal.PoliciesCount); True(agentTotal.PremiumAmount > 0, $"Agent {agent} should have positive premium"); } // Verify sales trends show correct data var salesTrendsQuery = new GetSalesTrendsQuery { - SalesDateFrom = policyFrom.AddDays(-1), - SalesDateTo = policyFrom.AddDays(1), + SalesDateFrom = saleDate.AddDays(-1), + SalesDateTo = saleDate.AddDays(1), Unit = TimeUnit.Day }; diff --git a/DashboardService.Test/PolicyDocumentBuilder.cs b/DashboardService.Test/PolicyDocumentBuilder.cs index 46d0aa5e..d0c96057 100644 --- a/DashboardService.Test/PolicyDocumentBuilder.cs +++ b/DashboardService.Test/PolicyDocumentBuilder.cs @@ -8,6 +8,7 @@ public class PolicyDocumentBuilder private readonly string policyHolder; private string agentLogin; private DateTime from; + private DateTime salesDate; private string number; private string productCode; private DateTime to; @@ -18,6 +19,7 @@ public PolicyDocumentBuilder() number = Guid.NewGuid().ToString(); from = new DateTime(2020, 1, 1); to = from.AddYears(1).AddDays(-1); + salesDate = from; policyHolder = "Jan Test"; productCode = "TRI"; totalPremium = 100M; @@ -39,6 +41,13 @@ public PolicyDocumentBuilder WithDates(string start, string end) { from = DateTime.Parse(start); to = DateTime.Parse(end); + salesDate = from; + return this; + } + + public PolicyDocumentBuilder WithSalesDate(string date) + { + salesDate = DateTime.Parse(date); return this; } @@ -67,6 +76,7 @@ public PolicyDocument Build() number, from, to, + salesDate, policyHolder, productCode, totalPremium, diff --git a/DashboardService/DataAccess/InMemory/LucenePolicyRepository.cs b/DashboardService/DataAccess/InMemory/LucenePolicyRepository.cs index 86771300..4d1953f9 100644 --- a/DashboardService/DataAccess/InMemory/LucenePolicyRepository.cs +++ b/DashboardService/DataAccess/InMemory/LucenePolicyRepository.cs @@ -35,9 +35,13 @@ public void Save(PolicyDocument policy) doc.Add(new Field("agentLogin", policy.AgentLogin ?? string.Empty, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("productCode", policy.ProductCode ?? string.Empty, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("from", policy.From.ToString("o", CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED)); + doc.Add(new Field("to", policy.To.ToString("o", CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED)); + doc.Add(new Field("salesDate", policy.SalesDate.ToString("o", CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED)); + doc.Add(new Field("policyHolder", policy.PolicyHolder, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("totalPremium", Convert.ToDouble(policy.TotalPremium).ToString(CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED)); writer.UpdateDocument(new Term("number", policy.Number), doc); + writer.Flush(false, false, false); writer.Commit(); } } @@ -89,7 +93,7 @@ public SalesTrendsResult GetSalesTrend(SalesTrendsQuery query) var docs = SearchByFilters(null, query.FilterByProductCode, query.FilterBySalesDateStart, query.FilterBySalesDateEnd); var periods = docs.GroupBy(d => { - var dt = DateTime.Parse(d.Get("from"), null, DateTimeStyles.RoundtripKind); + var dt = DateTime.Parse(d.Get("salesDate"), null, DateTimeStyles.RoundtripKind); return query.AggregationUnit switch { TimeAggregationUnit.Month => new DateTime(dt.Year, dt.Month, 1), @@ -157,7 +161,7 @@ private IEnumerable SearchByFilters(string agentLogin, string productC { docs = docs.Where(d => { - var dt = DateTime.Parse(d.Get("from"), null, DateTimeStyles.RoundtripKind); + var dt = DateTime.Parse(d.Get("salesDate"), null, DateTimeStyles.RoundtripKind); if (fromDate != default && dt < fromDate) return false; if (toDate != default && dt > toDate) return false; return true; @@ -171,13 +175,14 @@ private static PolicyDocument DocToPolicy(Document doc) { var number = doc.Get("number"); var from = DateTime.Parse(doc.Get("from"), null, DateTimeStyles.RoundtripKind); - var to = from.AddYears(1).AddDays(-1); - var insured = doc.Get("insuredName"); - var product = doc.Get("productCode"); - var premium = Convert.ToDecimal(doc.Get("totalPremium")); - var agent = doc.Get("agentLogin"); - - return new PolicyDocument(number, from, to, insured, product, premium, agent); + var to = DateTime.Parse(doc.Get("to"), null, DateTimeStyles.RoundtripKind); + var salesDate = DateTime.Parse(doc.Get("salesDate"), null, DateTimeStyles.RoundtripKind); + var policyHolder = doc.Get("policyHolder"); + var productCode = doc.Get("productCode"); + var totalPremium = Convert.ToDecimal(doc.Get("totalPremium")); + var agentLogin = doc.Get("agentLogin"); + + return new PolicyDocument(number, from, to, salesDate, policyHolder, productCode, totalPremium, agentLogin); } public void Dispose() diff --git a/DashboardService/Domain/PolicyDocument.cs b/DashboardService/Domain/PolicyDocument.cs index 9e60e42f..41bd9569 100644 --- a/DashboardService/Domain/PolicyDocument.cs +++ b/DashboardService/Domain/PolicyDocument.cs @@ -4,12 +4,13 @@ namespace DashboardService.Domain; public class PolicyDocument { - public PolicyDocument(string number, DateTime from, DateTime to, string policyHolder, string productCode, - decimal totalPremium, string agentLogin) + public PolicyDocument(string number, DateTime from, DateTime to, DateTime salesDate, string policyHolder, + string productCode, decimal totalPremium, string agentLogin) { Number = number; From = from; To = to; + SalesDate = salesDate; PolicyHolder = policyHolder; ProductCode = productCode; TotalPremium = totalPremium; @@ -19,6 +20,7 @@ public PolicyDocument(string number, DateTime from, DateTime to, string policyHo public string Number { get; } public DateTime From { get; } public DateTime To { get; } + public DateTime SalesDate { get; } public string PolicyHolder { get; } public string ProductCode { get; } public decimal TotalPremium { get; } diff --git a/DashboardService/Init/SalesData.cs b/DashboardService/Init/SalesData.cs index 50d5bb90..8f263cef 100644 --- a/DashboardService/Init/SalesData.cs +++ b/DashboardService/Init/SalesData.cs @@ -34,11 +34,13 @@ public async Task SeedData() for (var index = 0; index < policies; index++) { + var salesDate = new DateTime(startMonth.Year, startMonth.Month, 1); var policy = new PolicyDocument ( Guid.NewGuid().ToString(), - new DateTime(startMonth.Year, startMonth.Month, 1), - new DateTime(startMonth.Year, startMonth.Month, 1).AddYears(1).AddDays(-1), + salesDate, + salesDate.AddYears(1).AddDays(-1), + salesDate, "Anonymous Mike", product, 100M, diff --git a/DashboardService/Listeners/PolicyCreatedHandler.cs b/DashboardService/Listeners/PolicyCreatedHandler.cs index 64e73870..3d70daa5 100644 --- a/DashboardService/Listeners/PolicyCreatedHandler.cs +++ b/DashboardService/Listeners/PolicyCreatedHandler.cs @@ -22,6 +22,7 @@ public Task Handle(PolicyCreated notification, CancellationToken cancellationTok notification.PolicyNumber, notification.PolicyFrom, notification.PolicyTo, + notification.SalesDate, $"{notification.PolicyHolder.FirstName} {notification.PolicyHolder.LastName}", notification.ProductCode, notification.TotalPremium, diff --git a/PolicyService.Api/Events/PolicyCreated.cs b/PolicyService.Api/Events/PolicyCreated.cs index 9c7a519c..1dc08f36 100644 --- a/PolicyService.Api/Events/PolicyCreated.cs +++ b/PolicyService.Api/Events/PolicyCreated.cs @@ -10,6 +10,7 @@ public class PolicyCreated : INotification public string ProductCode { get; set; } public DateTime PolicyFrom { get; set; } public DateTime PolicyTo { get; set; } + public DateTime SalesDate { get; set; } public PersonDto PolicyHolder { get; set; } public decimal TotalPremium { get; set; } public string AgentLogin { get; set; } diff --git a/PolicyService/Commands/CreatePolicyHandler.cs b/PolicyService/Commands/CreatePolicyHandler.cs index e21139f7..d691c05a 100644 --- a/PolicyService/Commands/CreatePolicyHandler.cs +++ b/PolicyService/Commands/CreatePolicyHandler.cs @@ -1,7 +1,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using FluentValidation; using MediatR; using PolicyService.Api.Commands; using PolicyService.Api.Commands.Dtos; @@ -64,6 +63,7 @@ private static PolicyCreated PolicyCreated(Policy policy) PolicyNumber = policy.Number, PolicyFrom = version.CoverPeriod.ValidFrom, PolicyTo = version.CoverPeriod.ValidTo, + SalesDate = policy.CreationDate, ProductCode = policy.ProductCode, TotalPremium = version.TotalPremiumAmount, PolicyHolder = new PersonDto