public RiskTuple TryFindRemovableRisk(Risk risk, DateTime validTill) { return(_InsuredRisks .Where(it => !it.Item2.HasValue) .Where(it => it.Item1 <= validTill) .FirstOrDefault()); }
private ImmutablePolicy CheckIfPolicyExistsFor(string nameOfInsuredObject, Risk risk, DateTime dateTime, bool endAllowed) { if (!_AvailableRisks.Contains(risk)) { throw new RiskNotFoundException($"The risk '{risk.Name}' is not available"); } if (dateTime < _DateTimeProvider()) { throw new PolicyDateException("Cannot add/remove a risk that starts/ends in the past"); } var policy = GetImmutablePolicy(nameOfInsuredObject, dateTime, endAllowed); if (policy == null) { throw new PolicyNotFoundException(); } if (dateTime < policy.ValidFrom || ((endAllowed && dateTime > policy.ValidTill) || (!endAllowed && dateTime > policy.ValidTill))) { throw new PolicyDateException("The provided date does not fall into policies' duration time range."); } return(policy); }
public void RemoveRisk(string nameOfInsuredObject, Risk risk, DateTime validTill) { var policy = CheckIfPolicyExistsFor(nameOfInsuredObject, risk, validTill, true); var openRisk = policy.TryFindRemovableRisk(risk, validTill); if (openRisk == null) { throw new RiskNotFoundException($"Can't find a risk that is open and starts before {validTill}"); } var newPolicy = policy.Clone(risks => risks.Select(oldRisk => (oldRisk == openRisk) ? Tuple.Create(oldRisk.Item1, new DateTime?(validTill), oldRisk.Item3) : oldRisk).ToList()); _Policies.Remove(policy); _Policies.Add(newPolicy); }
public void AddRisk(string nameOfInsuredObject, Risk risk, DateTime validFrom) { var policy = CheckIfPolicyExistsFor(nameOfInsuredObject, risk, validFrom, false); if (policy.HasRiskLaterThan(risk, validFrom)) { throw new PolicyDateException("Cannot add this risk - policy has another unclosed risk or " + "this risk comes before the latest risk in the policy."); } var newPolicy = policy.Clone(risks => { var newRisks = new List <RiskTuple>(risks); newRisks.Add(Tuple.Create(validFrom, new DateTime?(), risk)); return(newRisks); }); _Policies.Remove(policy); _Policies.Add(newPolicy); }
public bool HasRiskLaterThan(Risk risk, DateTime validFrom) { return(_InsuredRisks.Any(it => it.Item3.Equals(risk) && (!it.Item2.HasValue || it.Item1 > validFrom || it.Item2 > validFrom))); }