Class: Dscf::Credit::FacilityLimitCalculationEngine

Inherits:
Object
  • Object
show all
Defined in:
app/services/dscf/credit/facility_limit_calculation_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loan_profile_id, credit_product_id) ⇒ FacilityLimitCalculationEngine

Returns a new instance of FacilityLimitCalculationEngine.



5
6
7
8
9
10
11
# File 'app/services/dscf/credit/facility_limit_calculation_engine.rb', line 5

def initialize(loan_profile_id, credit_product_id)
  @errors = []
  @loan_profile = LoanProfile.find(loan_profile_id)
  @credit_product_id = credit_product_id
rescue ActiveRecord::RecordNotFound => e
  @errors << "Loan profile not found: \#{e.message}"
end

Instance Attribute Details

#credit_product_idObject (readonly)

Returns the value of attribute credit_product_id.



3
4
5
# File 'app/services/dscf/credit/facility_limit_calculation_engine.rb', line 3

def credit_product_id
  @credit_product_id
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'app/services/dscf/credit/facility_limit_calculation_engine.rb', line 3

def errors
  @errors
end

#loan_profileObject (readonly)

Returns the value of attribute loan_profile.



3
4
5
# File 'app/services/dscf/credit/facility_limit_calculation_engine.rb', line 3

def loan_profile
  @loan_profile
end

Instance Method Details

#calculate_facility_limitsObject



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
# File 'app/services/dscf/credit/facility_limit_calculation_engine.rb', line 13

def calculate_facility_limits
  return error_result("Loan profile is required") unless loan_profile
  return error_result("Credit Product ID is required") unless credit_product_id

  begin
    ActiveRecord::Base.transaction do
      @data_by_source_id = LoanApplicationDatum
                              .where(loan_application: loan_profile.loan_application)
                              .index_by(&:information_source_id)

      credit_lines = get_credit_lines_for_calculation
      if credit_lines.empty?
        return error_result("No credit lines found for credit product #{credit_product_id} and bank #{loan_profile.loan_application.bank_id}")
      end

      eligible_credit_lines = []
      credit_lines.each do |credit_line|
        credit_line_spec = credit_line.credit_line_specs.active.order(created_at: :desc).first
        unless credit_line_spec
          @errors << "No active credit line spec for credit line \#{credit_line.id}"
          next
        end
        if loan_profile.score < credit_line_spec.minimum_score
          @errors << "Credit line #{credit_line.id} (#{credit_line.name}) skipped: score #{loan_profile.score} below minimum #{credit_line_spec.minimum_score}"
          next
        end
        base_metric = calculate_base_metric(credit_line_spec)
        if base_metric <= 0
          @errors << "Invalid base metric for credit line \#{credit_line.id}: \#{base_metric}"
          next
        end

        limit_result = calculate_credit_line_limit(credit_line, base_metric, credit_line_spec)
        unless limit_result[:success]
          @errors << limit_result[:error]
          next
        end

        if limit_result[:limit] >= 0
          eligible_credit_line = create_or_update_eligible_credit_line(credit_line, limit_result[:limit])
          eligible_credit_lines << eligible_credit_line
        end
      end

      update_loan_profile_total_limit
      success_result(eligible_credit_lines, "Facility limits calculated successfully")
    end
  rescue StandardError => e
    Rails.logger.error "Facility limit calculation failed: \#{e.message}"
    Rails.logger.error e.backtrace.join("\n")
    error_result("Failed to calculate facility limits: \#{e.message}")
  end
end