Class: Dscf::Credit::LoanProfilesController

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

Instance Method Summary collapse

Instance Method Details

#calculate_facility_limitsObject



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
# File 'app/controllers/dscf/credit/loan_profiles_controller.rb', line 28

def calculate_facility_limits
  loan_profile = @clazz.find(params[:id])
  credit_product_id = facility_params[:credit_product_id]

  return render_error(
    "loan_profile.errors.calculate_facility_limits",
    errors: [ "Credit Product ID is required" ],
    status: :unprocessable_entity
  ) unless credit_product_id

  facility_engine = FacilityLimitCalculationEngine.new(loan_profile.id, credit_product_id)
  result = facility_engine.calculate_facility_limits

  if result[:success]
    loan_profile.reload
    render_success(
      "loan_profile.success.calculate_facility_limits",
      data: {
        loan_profile: loan_profile,
        facility_result: result
      },
      serializer_options: { include: [ :eligible_credit_lines ] }
    )
  else
    render_error(
      "loan_profile.errors.calculate_facility_limits",
      errors: [ result[:error] ],
      status: :unprocessable_entity
    )
  end
rescue ActiveRecord::RecordNotFound
  render_error(
    "loan_profile.errors.not_found",
    status: :not_found
  )
rescue => e
  Rails.logger.error("Facility limit calculation error: #{e.class} - #{e.message}")
  Rails.logger.error(e.backtrace.join("\n"))

  if Rails.env.development? || Rails.env.test?
    render_error(
      "loan_profile.errors.calculate_facility_limits",
      errors: [ e.message ],
      status: :unprocessable_entity
    )
  else
    render_error(
      "loan_profile.errors.calculate_facility_limits",
      errors: [ "An error occurred while calculating facility limits" ],
      status: :unprocessable_entity
    )
  end
end

#createObject



6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/dscf/credit/loan_profiles_controller.rb', line 6

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

#updateObject



17
18
19
20
21
22
23
24
25
# File 'app/controllers/dscf/credit/loan_profiles_controller.rb', line 17

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