Adding
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace LCU.Library.Internal.LoanRateModels.Responses.Validation.RateSheetValidations;
|
||||
public static class RateSheetValidation
|
||||
{
|
||||
public static RateSheetValidationItem IsWithinTrendRange(decimal oldValue, decimal newValue, List<decimal> history)
|
||||
{
|
||||
var validation = new RateSheetValidationItem
|
||||
{
|
||||
Label = "Within Trend Range",
|
||||
Description = "If there is a history of change, is the current trend within the trend history's max and min values.",
|
||||
RateSheetValidationType = RateSheetValidationTypes.IsWithinTrendRange
|
||||
};
|
||||
|
||||
if (newValue == 0 || oldValue == 0)
|
||||
{
|
||||
validation.Pass = false;
|
||||
validation.Message = $"Values cannot be zero. OldValue: {oldValue} | NewValue: {newValue}";
|
||||
return validation;
|
||||
}
|
||||
|
||||
var currentTrend = oldValue / newValue;
|
||||
var trends = history.Zip(history.Skip(1), (x, y) => y / x).ToList();
|
||||
|
||||
var currentTrendString = Math.Round(currentTrend * 100, 2).ToString("0.##");
|
||||
var minTrendString = Math.Round(trends.Min() * 100, 2).ToString("0.##");
|
||||
var maxTrendString = Math.Round(trends.Max() * 100, 2).ToString("0.##");
|
||||
|
||||
validation.Value = currentTrendString;
|
||||
validation.Formula = trends.Count == 0
|
||||
? "No history found"
|
||||
: $"{currentTrendString} >= {minTrendString} && {currentTrendString} <= {maxTrendString}";
|
||||
validation.Criteria = trends.Count == 0
|
||||
? "No history found"
|
||||
: $"{minTrendString}% - {maxTrendString}%";
|
||||
validation.Pass = trends.Count == 0 || currentTrend >= trends.Min() && currentTrend <= trends.Max();
|
||||
return validation;
|
||||
}
|
||||
|
||||
public static RateSheetValidationItem IsWithinRange(decimal value, decimal min, decimal max)
|
||||
=> new()
|
||||
{
|
||||
Label = "Within Range",
|
||||
Description = "Value is within the min and max values.",
|
||||
Value = value.ToString("0.##"),
|
||||
Formula = $"{value:0.##} >= {min:0.##} && {value:0.##} <= {max:0.##}",
|
||||
Criteria = $"{min:0.##} - {max:0.##}",
|
||||
Pass = value >= min && value <= max,
|
||||
RateSheetValidationType = RateSheetValidationTypes.IsWithinRange
|
||||
};
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
namespace LCU.Library.Internal.LoanRateModels.Responses.Validation.RateSheetValidations;
|
||||
public class RateSheetValidationItem : ValidationItem
|
||||
{
|
||||
public RateSheetValidationTypes RateSheetValidationType { get; set; }
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using LCU.Library.Internal.LoanRateModels.Entities;
|
||||
|
||||
namespace LCU.Library.Internal.LoanRateModels.Responses.Validation.RateSheetValidations;
|
||||
public class RateSheetValidationResult : ValidationResult<Rate, RateSheetValidationItem>
|
||||
{
|
||||
public override List<RateSheetValidationItem> Validations { get; set; }
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
namespace LCU.Library.Internal.LoanRateModels.Responses.Validation.RateSheetValidations;
|
||||
public enum RateSheetValidationTypes
|
||||
{
|
||||
IsWithinTrendRange,
|
||||
IsWithinRange
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
namespace LCU.Library.Internal.LoanRateModels.Responses.Validation;
|
||||
public class ValidationItem
|
||||
{
|
||||
public string Label { get; set; } = string.Empty;
|
||||
public string Value { get; set; } = string.Empty;
|
||||
public string Description { get; set; } = string.Empty;
|
||||
public string Criteria { get; set; } = string.Empty;
|
||||
public string Formula { get; set; } = string.Empty;
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public bool Pass { get; set; }
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace LCU.Library.Internal.LoanRateModels.Responses.Validation;
|
||||
public class ValidationResult<TEntity, TValidationItem> where TValidationItem : ValidationItem
|
||||
{
|
||||
public TEntity NewItem { get; set; }
|
||||
public TEntity? OriginalItem { get; set; }
|
||||
public virtual List<TValidationItem> Validations { get; set; }
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public bool Pass { get; set; } = true;
|
||||
|
||||
public List<string> ValidationMessages => Validations.Where(x => !string.IsNullOrWhiteSpace(x.Message))
|
||||
.Select(x => x.Message).ToList();
|
||||
public List<TValidationItem> PassedValidations => Validations.Where(x => x.Pass).ToList();
|
||||
public List<TValidationItem> FailedValidations => Validations.Where(x => !x.Pass).ToList();
|
||||
public double Percentage => Math.Round(((double)PassedValidations.Count / Validations.Count) * 100, 2);
|
||||
|
||||
public bool CheckValidation()
|
||||
{
|
||||
Pass = Validations.Count == 0 || Validations.All(x => x.Pass);
|
||||
return Pass;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user