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 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 }; }