Class: Dscf::Credit::DisbursementService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount:, loan_profile:, eligible_credit_line:) ⇒ DisbursementService

Returns a new instance of DisbursementService.



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

def initialize(amount:, loan_profile:, eligible_credit_line:)
  @amount = amount.to_f
  @loan_profile = loan_profile
  @eligible_credit_line = eligible_credit_line
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

#eligible_credit_lineObject (readonly)

Returns the value of attribute eligible_credit_line.



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

def eligible_credit_line
  @eligible_credit_line
end

#loan_profileObject (readonly)

Returns the value of attribute loan_profile.



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

def loan_profile
  @loan_profile
end

Instance Method Details

#lock_other_credit_lines(loan_profile, credit_line) ⇒ Object

Lock other eligible credit lines for the loan profile except the specified credit line

Parameters:



45
46
47
48
49
# File 'app/services/dscf/credit/disbursement_service.rb', line 45

def lock_other_credit_lines(loan_profile, credit_line)
  loan_profile.eligible_credit_lines
    .where.not(credit_line: credit_line)
    .update_all(locked: true)
end

#process_disbursementHash

Process disbursement by creating a loan record from eligible credit line

Returns:

  • (Hash)

    Result containing created loan and disbursement details



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

def process_disbursement
  return error_result("Loan profile not found") unless loan_profile
  return error_result("Eligible credit line not found") unless eligible_credit_line
  return error_result("Invalid amount") unless amount > 0

  credit_line = eligible_credit_line.credit_line
  return error_result("Credit line not found") unless credit_line

  credit_line_status = credit_line.current_status_for(:default)
  return error_result("Credit line is not approved") unless credit_line_status == "approved"

  loan_profile_status = loan_profile.current_status_for(:default)
  return error_result("Loan profile not approved") unless loan_profile_status == "approved"

  validation_result = validate_disbursement_amount(credit_line)
  return validation_result unless validation_result[:success]

  ActiveRecord::Base.transaction do
    loan = create_loan_record(credit_line)
    update_credit_line_limits(loan)
    lock_other_credit_lines(loan_profile, credit_line)
    loan_transaction = create_disbursement_transaction(loan)

    success_result(loan, loan_transaction)
  end
rescue StandardError => e
  error_result("Disbursement processing failed: #{e.message}")
end