Skip to content

Commit 1cbd25a

Browse files
feat: update policy created message model
1 parent 0bcca9a commit 1cbd25a

8 files changed

Lines changed: 46 additions & 23 deletions

File tree

DashboardService.E2ETest/PolicyServiceToDashboardServiceIntegrationTest.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public async Task PolicyCreation_Should_UpdateDashboardAnalytics()
113113
await Task.Delay(1000);
114114
// Step 4: Wait for event processing and verify dashboard analytics are updated
115115
var policyNumber = createPolicyResult.PolicyNumber;
116-
var saleDate = policyDetails.Policy.DateFrom;
116+
// Sales date is when the policy was created (now), not the coverage start date
117+
var saleDate = DateTime.Now;
117118

118119
// Try querying dashboard multiple times with a delay to account for eventual consistency
119120
GetTotalSalesResult totalSalesResult = null;
@@ -281,11 +282,12 @@ public async Task MultiplePoliciesWithDifferentAgents_Should_ShowCorrectAggregat
281282
await Task.Delay(1000);
282283

283284
// Verify total sales aggregation
284-
var policyFrom = DateTime.Now.AddDays(5);
285+
// Sales date is when policies were created (now), not the coverage start date
286+
var saleDate = DateTime.Now;
285287
var totalSalesQuery = new GetTotalSalesQuery
286288
{
287-
SalesDateFrom = policyFrom.AddDays(-1),
288-
SalesDateTo = policyFrom.AddDays(1)
289+
SalesDateFrom = saleDate.AddDays(-1),
290+
SalesDateTo = saleDate.AddDays(1)
289291
};
290292

291293
var totalSalesScenario = await DashboardHost.Scenario(_ =>
@@ -308,8 +310,8 @@ public async Task MultiplePoliciesWithDifferentAgents_Should_ShowCorrectAggregat
308310
var agentSalesQuery = new GetAgentsSalesQuery
309311
{
310312
AgentLogin = agent,
311-
SalesDateFrom = policyFrom.AddDays(-1),
312-
SalesDateTo = policyFrom.AddDays(1)
313+
SalesDateFrom = saleDate.AddDays(-1),
314+
SalesDateTo = saleDate.AddDays(1)
313315
};
314316

315317
var agentSalesScenario = await DashboardHost.Scenario(_ =>
@@ -325,15 +327,15 @@ public async Task MultiplePoliciesWithDifferentAgents_Should_ShowCorrectAggregat
325327

326328
True(agentSalesResult.PerAgentTotal.ContainsKey(agent), $"Agent {agent} should be in sales results");
327329
var agentTotal = agentSalesResult.PerAgentTotal[agent];
328-
Equal((long)policyCounts[agent], agentTotal.PoliciesCount);
330+
Equal(policyCounts[agent], agentTotal.PoliciesCount);
329331
True(agentTotal.PremiumAmount > 0, $"Agent {agent} should have positive premium");
330332
}
331333

332334
// Verify sales trends show correct data
333335
var salesTrendsQuery = new GetSalesTrendsQuery
334336
{
335-
SalesDateFrom = policyFrom.AddDays(-1),
336-
SalesDateTo = policyFrom.AddDays(1),
337+
SalesDateFrom = saleDate.AddDays(-1),
338+
SalesDateTo = saleDate.AddDays(1),
337339
Unit = TimeUnit.Day
338340
};
339341

DashboardService.Test/PolicyDocumentBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public class PolicyDocumentBuilder
88
private readonly string policyHolder;
99
private string agentLogin;
1010
private DateTime from;
11+
private DateTime salesDate;
1112
private string number;
1213
private string productCode;
1314
private DateTime to;
@@ -18,6 +19,7 @@ public PolicyDocumentBuilder()
1819
number = Guid.NewGuid().ToString();
1920
from = new DateTime(2020, 1, 1);
2021
to = from.AddYears(1).AddDays(-1);
22+
salesDate = from;
2123
policyHolder = "Jan Test";
2224
productCode = "TRI";
2325
totalPremium = 100M;
@@ -39,6 +41,13 @@ public PolicyDocumentBuilder WithDates(string start, string end)
3941
{
4042
from = DateTime.Parse(start);
4143
to = DateTime.Parse(end);
44+
salesDate = from;
45+
return this;
46+
}
47+
48+
public PolicyDocumentBuilder WithSalesDate(string date)
49+
{
50+
salesDate = DateTime.Parse(date);
4251
return this;
4352
}
4453

@@ -67,6 +76,7 @@ public PolicyDocument Build()
6776
number,
6877
from,
6978
to,
79+
salesDate,
7080
policyHolder,
7181
productCode,
7282
totalPremium,

DashboardService/DataAccess/InMemory/LucenePolicyRepository.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ public void Save(PolicyDocument policy)
3535
doc.Add(new Field("agentLogin", policy.AgentLogin ?? string.Empty, Field.Store.YES, Field.Index.NOT_ANALYZED));
3636
doc.Add(new Field("productCode", policy.ProductCode ?? string.Empty, Field.Store.YES, Field.Index.NOT_ANALYZED));
3737
doc.Add(new Field("from", policy.From.ToString("o", CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED));
38+
doc.Add(new Field("to", policy.To.ToString("o", CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED));
39+
doc.Add(new Field("salesDate", policy.SalesDate.ToString("o", CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED));
40+
doc.Add(new Field("policyHolder", policy.PolicyHolder, Field.Store.YES, Field.Index.NOT_ANALYZED));
3841
doc.Add(new Field("totalPremium", Convert.ToDouble(policy.TotalPremium).ToString(CultureInfo.InvariantCulture), Field.Store.YES, Field.Index.NOT_ANALYZED));
3942

4043
writer.UpdateDocument(new Term("number", policy.Number), doc);
44+
writer.Flush(false, false, false);
4145
writer.Commit();
4246
}
4347
}
@@ -89,7 +93,7 @@ public SalesTrendsResult GetSalesTrend(SalesTrendsQuery query)
8993
var docs = SearchByFilters(null, query.FilterByProductCode, query.FilterBySalesDateStart, query.FilterBySalesDateEnd);
9094
var periods = docs.GroupBy(d =>
9195
{
92-
var dt = DateTime.Parse(d.Get("from"), null, DateTimeStyles.RoundtripKind);
96+
var dt = DateTime.Parse(d.Get("salesDate"), null, DateTimeStyles.RoundtripKind);
9397
return query.AggregationUnit switch
9498
{
9599
TimeAggregationUnit.Month => new DateTime(dt.Year, dt.Month, 1),
@@ -157,7 +161,7 @@ private IEnumerable<Document> SearchByFilters(string agentLogin, string productC
157161
{
158162
docs = docs.Where(d =>
159163
{
160-
var dt = DateTime.Parse(d.Get("from"), null, DateTimeStyles.RoundtripKind);
164+
var dt = DateTime.Parse(d.Get("salesDate"), null, DateTimeStyles.RoundtripKind);
161165
if (fromDate != default && dt < fromDate) return false;
162166
if (toDate != default && dt > toDate) return false;
163167
return true;
@@ -171,13 +175,14 @@ private static PolicyDocument DocToPolicy(Document doc)
171175
{
172176
var number = doc.Get("number");
173177
var from = DateTime.Parse(doc.Get("from"), null, DateTimeStyles.RoundtripKind);
174-
var to = from.AddYears(1).AddDays(-1);
175-
var insured = doc.Get("insuredName");
176-
var product = doc.Get("productCode");
177-
var premium = Convert.ToDecimal(doc.Get("totalPremium"));
178-
var agent = doc.Get("agentLogin");
179-
180-
return new PolicyDocument(number, from, to, insured, product, premium, agent);
178+
var to = DateTime.Parse(doc.Get("to"), null, DateTimeStyles.RoundtripKind);
179+
var salesDate = DateTime.Parse(doc.Get("salesDate"), null, DateTimeStyles.RoundtripKind);
180+
var policyHolder = doc.Get("policyHolder");
181+
var productCode = doc.Get("productCode");
182+
var totalPremium = Convert.ToDecimal(doc.Get("totalPremium"));
183+
var agentLogin = doc.Get("agentLogin");
184+
185+
return new PolicyDocument(number, from, to, salesDate, policyHolder, productCode, totalPremium, agentLogin);
181186
}
182187

183188
public void Dispose()

DashboardService/Domain/PolicyDocument.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ namespace DashboardService.Domain;
44

55
public class PolicyDocument
66
{
7-
public PolicyDocument(string number, DateTime from, DateTime to, string policyHolder, string productCode,
8-
decimal totalPremium, string agentLogin)
7+
public PolicyDocument(string number, DateTime from, DateTime to, DateTime salesDate, string policyHolder,
8+
string productCode, decimal totalPremium, string agentLogin)
99
{
1010
Number = number;
1111
From = from;
1212
To = to;
13+
SalesDate = salesDate;
1314
PolicyHolder = policyHolder;
1415
ProductCode = productCode;
1516
TotalPremium = totalPremium;
@@ -19,6 +20,7 @@ public PolicyDocument(string number, DateTime from, DateTime to, string policyHo
1920
public string Number { get; }
2021
public DateTime From { get; }
2122
public DateTime To { get; }
23+
public DateTime SalesDate { get; }
2224
public string PolicyHolder { get; }
2325
public string ProductCode { get; }
2426
public decimal TotalPremium { get; }

DashboardService/Init/SalesData.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public async Task SeedData()
3434

3535
for (var index = 0; index < policies; index++)
3636
{
37+
var salesDate = new DateTime(startMonth.Year, startMonth.Month, 1);
3738
var policy = new PolicyDocument
3839
(
3940
Guid.NewGuid().ToString(),
40-
new DateTime(startMonth.Year, startMonth.Month, 1),
41-
new DateTime(startMonth.Year, startMonth.Month, 1).AddYears(1).AddDays(-1),
41+
salesDate,
42+
salesDate.AddYears(1).AddDays(-1),
43+
salesDate,
4244
"Anonymous Mike",
4345
product,
4446
100M,

DashboardService/Listeners/PolicyCreatedHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public Task Handle(PolicyCreated notification, CancellationToken cancellationTok
2222
notification.PolicyNumber,
2323
notification.PolicyFrom,
2424
notification.PolicyTo,
25+
notification.SalesDate,
2526
$"{notification.PolicyHolder.FirstName} {notification.PolicyHolder.LastName}",
2627
notification.ProductCode,
2728
notification.TotalPremium,

PolicyService.Api/Events/PolicyCreated.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class PolicyCreated : INotification
1010
public string ProductCode { get; set; }
1111
public DateTime PolicyFrom { get; set; }
1212
public DateTime PolicyTo { get; set; }
13+
public DateTime SalesDate { get; set; }
1314
public PersonDto PolicyHolder { get; set; }
1415
public decimal TotalPremium { get; set; }
1516
public string AgentLogin { get; set; }

PolicyService/Commands/CreatePolicyHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Linq;
22
using System.Threading;
33
using System.Threading.Tasks;
4-
using FluentValidation;
54
using MediatR;
65
using PolicyService.Api.Commands;
76
using PolicyService.Api.Commands.Dtos;
@@ -64,6 +63,7 @@ private static PolicyCreated PolicyCreated(Policy policy)
6463
PolicyNumber = policy.Number,
6564
PolicyFrom = version.CoverPeriod.ValidFrom,
6665
PolicyTo = version.CoverPeriod.ValidTo,
66+
SalesDate = policy.CreationDate,
6767
ProductCode = policy.ProductCode,
6868
TotalPremium = version.TotalPremiumAmount,
6969
PolicyHolder = new PersonDto

0 commit comments

Comments
 (0)