Class: Dscf::Credit::CreditLimitCalculationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/dscf/credit/credit_limit_calculations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



3
4
5
6
7
8
9
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
# File 'app/controllers/dscf/credit/credit_limit_calculations_controller.rb', line 3

def create
  authorize Dscf::Credit::LoanProfile.new, :calculate_facility_limits?

  loan_profile = Dscf::Credit::LoanProfile.find(params[:loan_profile_id])
  credit_product = Dscf::Credit::CreditProduct.find(params[:credit_product_id])

  engine = Dscf::Credit::FacilityLimitCalculationEngine.new(loan_profile.id, credit_product.id)
  result = engine.calculate_facility_limits

  if result[:success]
    loan_profile.reload
    render_success(
      "credit_limit_calculation.success.create",
      data: {
        loan_profile: loan_profile,
        credit_product: credit_product,
        eligible_credit_lines: result[:data],
        calculation_summary: {
          total_credit_lines_processed: result[:data]&.size || 0,
          calculated_at: Time.current
        }
      },
      serializer_options: {
        include: [
          :eligible_credit_lines,
          { eligible_credit_lines: [ :credit_line ] }
        ]
      }
    )
  else
    render_error(
      "credit_limit_calculation.errors.create",
      errors: [ result[:error] ],
      status: :unprocessable_entity
    )
  end
rescue ActiveRecord::RecordNotFound => e
  render_error(
    "credit_limit_calculation.errors.not_found",
    errors: [ e.message ],
    status: :not_found
  )
rescue StandardError => e
  render_error(
    "credit_limit_calculation.errors.create",
    errors: [ e.message ],
    status: :internal_server_error
  )
end