Class: Dscf::Credit::CreditScoringEngine
- Inherits:
-
Object
- Object
- Dscf::Credit::CreditScoringEngine
- Defined in:
- app/services/dscf/credit/credit_scoring_engine.rb
Instance Method Summary collapse
- #calculate_score ⇒ Object
-
#initialize(loan_application) ⇒ CreditScoringEngine
constructor
A new instance of CreditScoringEngine.
Constructor Details
#initialize(loan_application) ⇒ CreditScoringEngine
Returns a new instance of CreditScoringEngine.
3 4 5 6 7 8 |
# File 'app/services/dscf/credit/credit_scoring_engine.rb', line 3 def initialize(loan_application) @loan_application = loan_application @credit_product = loan_application.credit_product @scoring_table = @credit_product&.scoring_table @data_by_source_id = load_data_by_source end |
Instance Method Details
#calculate_score ⇒ Object
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 99 100 101 102 |
# File 'app/services/dscf/credit/credit_scoring_engine.rb', line 10 def calculate_score return failure("No credit product assigned to loan application") unless @credit_product return failure("No scoring table configured for credit product '#{@credit_product.name}'") unless @scoring_table parameters = @scoring_table.scoring_table_parameters .includes(:scoring_parameter, :scoring_table_normalizers, :information_source) .order(:order) return failure("Scoring table '#{@scoring_table.name}' has no parameters configured") if parameters.empty? total_weight = 0.0 weighted_score = 0.0 parameters_list = [] parameters_skipped = 0 missing_required = [] parameters.each do |table_param| param = table_param.scoring_parameter source_id = table_param.information_source_id datum = @data_by_source_id[source_id] raw_value = datum&.data&.[](param.id.to_s) if raw_value.nil? if table_param.required missing_required << "Missing required parameter: #{param.name}" else parameters_skipped += 1 end next end normalized = normalize(table_param, raw_value) if normalized.nil? parameters_skipped += 1 next end weight = table_param.weight.to_f weighted = normalized * weight weighted_score += weighted total_weight += weight parameters_list << { "parameter_id" => param.id, "code" => param.code, "name" => param.name, "data_type" => param.data_type, "information_source" => table_param.information_source&.name, "raw_value" => raw_value, "normalized_value" => normalized.round(4), "weight" => weight, "weighted_contribution" => weighted.round(4) } end return failure(missing_required.first) if missing_required.any? return failure("No scoreable data found for loan application") if total_weight.zero? final_score = (weighted_score / total_weight) * 100.0 passing_score = @scoring_table.passing_score.to_f pending_threshold = @scoring_table.pending_threshold.to_f status = if final_score >= passing_score "approved" elsif final_score >= pending_threshold "pending" else "rejected" end breakdown = { "parameters" => parameters_list, "total_weighted_sum" => weighted_score.round(6), "total_weight" => total_weight.round(6), "parameters_processed" => parameters_list.size, "parameters_skipped" => parameters_skipped, "scoring_table_code" => @scoring_table.code, "passing_score" => passing_score, "pending_threshold" => pending_threshold, "final_score" => final_score.round(2), "status" => status } { success: true, score: final_score.round(2), status: status, breakdown: breakdown, errors: [] } end |