Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(_ =>
Expand All @@ -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(_ =>
Expand All @@ -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
};

Expand Down
10 changes: 10 additions & 0 deletions DashboardService.Test/PolicyDocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -67,6 +76,7 @@ public PolicyDocument Build()
number,
from,
to,
salesDate,
policyHolder,
productCode,
totalPremium,
Expand Down
23 changes: 14 additions & 9 deletions DashboardService/DataAccess/InMemory/LucenePolicyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -157,7 +161,7 @@ private IEnumerable<Document> 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;
Expand All @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions DashboardService/Domain/PolicyDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }
Expand Down
6 changes: 4 additions & 2 deletions DashboardService/Init/SalesData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions DashboardService/Listeners/PolicyCreatedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions PolicyService.Api/Events/PolicyCreated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion PolicyService/Commands/CreatePolicyHandler.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down