Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (81 loc) · 2.43 KB
/
Copy pathProgram.cs
File metadata and controls
98 lines (81 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Linq;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.MapGet("/", () => "Hello World!");
app.MapPost("/api/claims/ingest", (List<Claim> claims) =>
{
var validClaims = new List<Claim>();
var flaggedClaims = new List<object>();
foreach (var claim in claims)
{
var errors = ValidateClaim(claim);
if (errors.Count == 0)
{
validClaims.Add(claim);
}
else
{
flaggedClaims.Add(new { Claim = claim, Errors = errors });
}
}
var summary = AggregateClaims(validClaims);
return Results.Ok(new
{
Summary = summary,
FlaggedClaims = flaggedClaims
});
});
static List<string> ValidateClaim(Claim claim)
{
var errors = new List<string>();
if (claim.AmountPaid < 0)
{
errors.Add($"AmountPaid cannot be negative (got {claim.AmountPaid}).");
}
if (claim.OutstandingReserve < 0)
{
errors.Add($"OutstandingReserve cannot be negative (got {claim.OutstandingReserve}).");
}
var earliestReasonableDate = new DateTime(claim.UnderwritingYear, 1, 1);
var latestReasonableDate = new DateTime(claim.UnderwritingYear + 5, 12, 31);
if (claim.ClaimDate < earliestReasonableDate || claim.ClaimDate > latestReasonableDate)
{
errors.Add(
$"ClaimDate {claim.ClaimDate:yyyy-MM-dd} is outside a reasonable range " +
$"for underwriting year {claim.UnderwritingYear}."
);
}
return errors;
}
static object AggregateClaims(List<Claim> validClaims)
{
var totalExposure = validClaims.Sum(c => c.AmountPaid + c.OutstandingReserve);
var byLineOfBusiness = validClaims
.GroupBy(c => c.LineOfBusiness)
.Select(group => new
{
LineOfBusiness = group.Key,
TotalExposure = group.Sum(c => c.AmountPaid + c.OutstandingReserve),
ClaimCount = group.Count()
});
return new
{
TotalClaimsIngested = validClaims.Count,
TotalFinancialExposure = totalExposure,
ExposureByLineOfBusiness = byLineOfBusiness
};
}
app.Run();
public record Claim(
string ClaimId,
string PolicyNumber,
int UnderwritingYear,
DateTime ClaimDate,
decimal AmountPaid,
decimal OutstandingReserve,
string LineOfBusiness
);