Class: Dscf::Credit::LoanApplicationsController

Inherits:
ApplicationController
  • Object
show all
Includes:
Dscf::Core::AuditableController, Dscf::Core::Common, Dscf::Core::ReviewableController
Defined in:
app/controllers/dscf/credit/loan_applications_controller.rb

Instance Method Summary collapse

Instance Method Details

#calculate_credit_scoreObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/dscf/credit/loan_applications_controller.rb', line 152

def calculate_credit_score
  loan_application = @clazz.find(params[:id])

  result = CreditScoringEngine.new(loan_application).calculate_score

  if result[:success]
    scoring_result_data = {
        scoring_table_id: loan_application.credit_product&.scoring_table&.id,
      breakdown: result[:breakdown],
      scoring_input_data: build_scoring_input_snapshot(loan_application)
    }

    ActiveRecord::Base.transaction do
      loan_application.update!(score: result[:score])

      update_review_status(loan_application, result[:status])

      if result[:status] == "approved"
        create_loan_profile_for_approved_application(loan_application, result[:score], scoring_result_data)
      end

      loan_application.reload
    end

    eligibility = build_eligibility_response(loan_application)

    render_success(
      "loan_application.success.calculate_credit_score",
      data: result.merge(
        loan_application: loan_application,
        eligibility: eligibility
      ),
      status: :ok
    )
  else
    render_error(
      "loan_application.errors.calculate_credit_score",
      errors: result[:errors],
      status: :unprocessable_entity
    )
  end
rescue ActiveRecord::RecordNotFound
  render_error(
    "loan_application.errors.not_found",
    errors: [ "Loan application not found" ],
    status: :not_found
  )
rescue => e
  Rails.logger.error("Credit scoring error: #{e.class} - #{e.message}")
  Rails.logger.error(e.backtrace.join("\n"))

  render_error(
    "loan_application.errors.calculate_credit_score",
    errors: [ "An error occurred while calculating credit score" ],
    status: :unprocessable_entity
  )
end

#createObject



53
54
55
56
57
58
59
60
61
# File 'app/controllers/dscf/credit/loan_applications_controller.rb', line 53

def create
  super do
    loan_application = @clazz.new(model_params)
    loan_application.user = current_user
    loan_application.reviews.build(context: "default", status: "draft")

    loan_application
  end
end

#create_loan_profile_for_approved_application(loan_application, score, scoring_result_data = {}) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/dscf/credit/loan_applications_controller.rb', line 210

def create_loan_profile_for_approved_application(loan_application, score, scoring_result_data = {})
  profile_service = LoanProfileCreationService.new(loan_application, score, scoring_result_data)
  profile_result = profile_service.create_loan_profile

  unless profile_result[:success]
    error_message = "Failed to create loan profile for approved application #{loan_application.id}: #{profile_result[:error]}"
    Rails.logger.error error_message
    raise StandardError, error_message
  else
    Rails.logger.info "Loan profile created successfully for approved application #{loan_application.id}"
  end

  profile_result
end

#scoring_formObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/dscf/credit/loan_applications_controller.rb', line 94

def scoring_form
  set_object
  source_code = params[:source_code]
  information_source = Dscf::Credit::InformationSource.find_by(code: source_code)

  unless information_source
    return render_error(errors: [ "Information source '#{source_code}' not found" ], status: :not_found)
  end

  credit_product = @obj.credit_product
  scoring_table = credit_product&.scoring_table

  unless scoring_table
    return render_error(errors: [ "No scoring table configured for this loan application's credit product" ], status: :unprocessable_entity)
  end

  # Load parameters for this source with normalizers
  parameters = scoring_table.scoring_table_parameters
    .where(information_source: information_source)
    .includes(:scoring_parameter, :scoring_table_normalizers)
    .order(:order)

  # Load existing submitted data for this source
  existing_datum = @obj.loan_application_data.find_by(information_source: information_source)
  existing_data = existing_datum&.data || {}

  # Build response
  form_data = {
    source: {
      code: information_source.code,
      name: information_source.name
    },
    scoring_table: {
      id: scoring_table.id,
      name: scoring_table.name,
      code: scoring_table.code
    },
    submitted: existing_datum&..present? || false,
    submitted_at: existing_datum&.,
    fields: parameters.map do |stp|
      param = stp.scoring_parameter
      {
        parameter_id: param.id,
        code: param.code,
        name: param.name,
        data_type: param.data_type,
        required: stp.required,
        min_value: stp.min_value,
        max_value: stp.max_value,
        current_value: existing_data[param.id.to_s],
        options: extract_options(stp, param.data_type)
      }
    end
  }

  render_success(data: form_data)
end

#submit_source_dataObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/dscf/credit/loan_applications_controller.rb', line 73

def submit_source_data
  set_object
  source_code = params[:source_code]
  information_source = Dscf::Credit::InformationSource.find_by(code: source_code)

  unless information_source
    return render_error(errors: [ "Information source '#{source_code}' not found" ], status: :not_found)
  end

  datum = @obj.loan_application_data.find_or_initialize_by(information_source: information_source)
  datum.data = params.dig(:loan_application_datum, :data) || params[:data] || {}
  datum. = current_user
  datum. = Time.current

  if datum.save
    render_success(data: datum)
  else
    render_error(errors: datum.errors.full_messages, status: :unprocessable_entity)
  end
end

#updateObject



63
64
65
66
67
68
69
70
71
# File 'app/controllers/dscf/credit/loan_applications_controller.rb', line 63

def update
  unless @obj.editable?
    return render_error(
      errors: [ "Cannot update loan application after submission. Use modification request workflow instead." ],
      status: :unprocessable_entity
    )
  end
  super
end