dscf-banking 0.1.2 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +0 -0
  3. data/README.md +0 -0
  4. data/Rakefile +0 -0
  5. data/app/controllers/dscf/banking/accounts_controller.rb +0 -0
  6. data/app/controllers/dscf/banking/application_controller.rb +0 -0
  7. data/app/controllers/dscf/banking/applications_controller.rb +0 -0
  8. data/app/controllers/dscf/banking/interest_configurations_controller.rb +0 -0
  9. data/app/controllers/dscf/banking/interest_rate_tiers_controller.rb +0 -0
  10. data/app/controllers/dscf/banking/interest_rate_types_controller.rb +0 -0
  11. data/app/controllers/dscf/banking/product_approvals_controller.rb +0 -0
  12. data/app/controllers/dscf/banking/product_categories_controller.rb +0 -0
  13. data/app/controllers/dscf/banking/virtual_account_products_controller.rb +0 -0
  14. data/app/jobs/dscf/banking/application_job.rb +0 -0
  15. data/app/mailers/dscf/banking/application_mailer.rb +0 -0
  16. data/app/models/concerns/dscf/banking/auditable.rb +0 -0
  17. data/app/models/dscf/banking/account.rb +2 -24
  18. data/app/models/dscf/banking/application.rb +0 -0
  19. data/app/models/dscf/banking/application_record.rb +0 -0
  20. data/app/models/dscf/banking/interest_configuration.rb +0 -0
  21. data/app/models/dscf/banking/interest_rate_tier.rb +0 -0
  22. data/app/models/dscf/banking/interest_rate_type.rb +0 -0
  23. data/app/models/dscf/banking/product_approval.rb +0 -0
  24. data/app/models/dscf/banking/product_audit_log.rb +0 -0
  25. data/app/models/dscf/banking/product_category.rb +0 -0
  26. data/app/models/dscf/banking/virtual_account_product.rb +0 -0
  27. data/app/serializers/dscf/banking/account_serializer.rb +0 -0
  28. data/app/serializers/dscf/banking/application_serializer.rb +0 -0
  29. data/app/serializers/dscf/banking/interest_configuration_serializer.rb +0 -0
  30. data/app/serializers/dscf/banking/interest_rate_tier_serializer.rb +0 -0
  31. data/app/serializers/dscf/banking/interest_rate_type_serializer.rb +0 -0
  32. data/app/serializers/dscf/banking/product_approval_serializer.rb +0 -0
  33. data/app/serializers/dscf/banking/product_category_serializer.rb +0 -0
  34. data/app/serializers/dscf/banking/virtual_account_product_serializer.rb +0 -0
  35. data/app/services/dscf/banking/account_creation_service.rb +0 -0
  36. data/config/routes.rb +0 -14
  37. data/db/migrate/20250830211002_create_dscf_banking_product_categories.rb +0 -0
  38. data/db/migrate/20250830211027_create_dscf_banking_interest_rate_types.rb +0 -0
  39. data/db/migrate/20250831063605_add_code_and_remove_fields_from_interest_rate_types.rb +0 -0
  40. data/db/migrate/20250831064917_create_dscf_banking_virtual_account_products.rb +0 -0
  41. data/db/migrate/20250831072627_create_dscf_banking_interest_configurations.rb +0 -0
  42. data/db/migrate/20250831080745_create_dscf_banking_interest_rate_tiers.rb +0 -0
  43. data/db/migrate/20250831082356_create_dscf_banking_product_approvals.rb +0 -0
  44. data/db/migrate/20250831083907_create_dscf_banking_product_audit_logs.rb +0 -0
  45. data/db/migrate/20250831084706_allow_null_virtual_account_product_id_in_product_audit_logs.rb +0 -0
  46. data/db/migrate/20250912193134_create_dscf_banking_applications.rb +0 -0
  47. data/db/migrate/20250912203527_create_dscf_banking_accounts.rb +0 -0
  48. data/lib/dscf/banking/engine.rb +0 -0
  49. data/lib/dscf/banking/version.rb +1 -1
  50. data/lib/dscf/banking.rb +0 -0
  51. data/lib/tasks/dscf/banking_tasks.rake +0 -0
  52. data/spec/factories/dscf/banking/accounts.rb +0 -28
  53. data/spec/factories/dscf/banking/applications.rb +5 -6
  54. data/spec/factories/dscf/banking/interest_configurations.rb +0 -0
  55. data/spec/factories/dscf/banking/interest_rate_tiers.rb +0 -0
  56. data/spec/factories/dscf/banking/interest_rate_types.rb +0 -0
  57. data/spec/factories/dscf/banking/product_approvals.rb +0 -0
  58. data/spec/factories/dscf/banking/product_audit_logs.rb +0 -0
  59. data/spec/factories/dscf/banking/product_categories.rb +0 -0
  60. data/spec/factories/dscf/banking/virtual_account_products.rb +0 -0
  61. metadata +2 -19
  62. data/app/controllers/dscf/banking/transaction_types_controller.rb +0 -27
  63. data/app/controllers/dscf/banking/transactions_controller.rb +0 -275
  64. data/app/models/dscf/banking/transaction.rb +0 -68
  65. data/app/models/dscf/banking/transaction_type.rb +0 -20
  66. data/app/serializers/dscf/banking/transaction_serializer.rb +0 -19
  67. data/app/serializers/dscf/banking/transaction_type_serializer.rb +0 -9
  68. data/app/services/dscf/banking/base_transaction_service.rb +0 -99
  69. data/app/services/dscf/banking/deposit_service.rb +0 -97
  70. data/app/services/dscf/banking/transfer_service.rb +0 -78
  71. data/app/services/dscf/banking/withdrawal_service.rb +0 -100
  72. data/db/migrate/20250919084147_add_account_type_to_accounts.rb +0 -6
  73. data/db/migrate/20250919084927_make_account_associations_optional.rb +0 -6
  74. data/db/migrate/20250919182831_create_dscf_banking_transaction_types.rb +0 -14
  75. data/db/migrate/20250919184220_create_dscf_banking_transactions.rb +0 -21
  76. data/db/seeds.rb +0 -125
  77. data/spec/factories/dscf/banking/transaction_types.rb +0 -56
  78. data/spec/factories/dscf/banking/transactions.rb +0 -52
@@ -1,275 +0,0 @@
1
- module Dscf::Banking
2
- class TransactionsController < ApplicationController
3
- include Dscf::Core::Common
4
-
5
- # Transfer money between accounts (from test cases)
6
- def transfer
7
- debit_account = Dscf::Banking::Account.find_by(account_number: transfer_params[:debit_account_number])
8
- credit_account = Dscf::Banking::Account.find_by(account_number: transfer_params[:credit_account_number])
9
-
10
- unless debit_account
11
- return render json: {
12
- success: false,
13
- error: "Debit account not found",
14
- errors: [ "Account with number #{transfer_params[:debit_account_number]} not found" ]
15
- }, status: :not_found
16
- end
17
-
18
- unless credit_account
19
- return render json: {
20
- success: false,
21
- error: "Credit account not found",
22
- errors: [ "Account with number #{transfer_params[:credit_account_number]} not found" ]
23
- }, status: :not_found
24
- end
25
-
26
- result = Dscf::Banking::TransferService.new(
27
- debit_account: debit_account,
28
- credit_account: credit_account,
29
- amount: transfer_params[:amount],
30
- description: transfer_params[:description] || "Transfer between accounts",
31
- transaction_type_code: transfer_params[:transaction_type_code] || "TRANSFER"
32
- ).execute
33
-
34
- if result.success?
35
- render json: {
36
- success: true,
37
- data: transaction_data(result.transaction),
38
- message: "Transfer completed successfully"
39
- }
40
- else
41
- render json: {
42
- success: false,
43
- error: "Transfer failed",
44
- errors: result.errors
45
- }, status: :unprocessable_entity
46
- end
47
- rescue StandardError => e
48
- render json: {
49
- success: false,
50
- error: "Transfer failed",
51
- errors: [ e.message ]
52
- }, status: :internal_server_error
53
- end
54
-
55
- # Deposit money to account (from mobile banking - Abole)
56
- def deposit
57
- account = Dscf::Banking::Account.find_by(account_number: deposit_params[:account_number])
58
-
59
- unless account
60
- return render json: {
61
- success: false,
62
- error: "Account not found",
63
- errors: [ "Account with number #{deposit_params[:account_number]} not found" ]
64
- }, status: :not_found
65
- end
66
-
67
- result = Dscf::Banking::DepositService.new(
68
- account: account,
69
- amount: deposit_params[:amount],
70
- description: deposit_params[:description] || "Deposit to account",
71
- transaction_type_code: deposit_params[:transaction_type_code] || "DEPOSIT"
72
- ).execute
73
-
74
- if result.success?
75
- render json: {
76
- success: true,
77
- data: transaction_data(result.transaction),
78
- message: "Deposit completed successfully"
79
- }
80
- else
81
- render json: {
82
- success: false,
83
- error: "Deposit failed",
84
- errors: result.errors
85
- }, status: :unprocessable_entity
86
- end
87
- rescue StandardError => e
88
- render json: {
89
- success: false,
90
- error: "Deposit failed",
91
- errors: [ e.message ]
92
- }, status: :internal_server_error
93
- end
94
-
95
- # Withdraw money from account
96
- def withdrawal
97
- account = Dscf::Banking::Account.find_by(account_number: withdrawal_params[:account_number])
98
-
99
- unless account
100
- return render json: {
101
- success: false,
102
- error: "Account not found",
103
- errors: [ "Account with number #{withdrawal_params[:account_number]} not found" ]
104
- }, status: :not_found
105
- end
106
-
107
- result = Dscf::Banking::WithdrawalService.new(
108
- account: account,
109
- amount: withdrawal_params[:amount],
110
- description: withdrawal_params[:description] || "Withdrawal from account",
111
- transaction_type_code: withdrawal_params[:transaction_type_code] || "WITHDRAWAL"
112
- ).execute
113
-
114
- if result.success?
115
- render json: {
116
- success: true,
117
- data: transaction_data(result.transaction),
118
- message: "Withdrawal completed successfully"
119
- }
120
- else
121
- render json: {
122
- success: false,
123
- error: "Withdrawal failed",
124
- errors: result.errors
125
- }, status: :unprocessable_entity
126
- end
127
- rescue StandardError => e
128
- render json: {
129
- success: false,
130
- error: "Withdrawal failed",
131
- errors: [ e.message ]
132
- }, status: :internal_server_error
133
- end
134
-
135
- # Cancel a transaction
136
- def cancel
137
- transaction = Dscf::Banking::Transaction.find(params[:id])
138
-
139
- unless transaction.status == "pending"
140
- return render json: {
141
- success: false,
142
- error: "Cannot cancel transaction",
143
- errors: [ "Only pending transactions can be cancelled" ]
144
- }, status: :unprocessable_entity
145
- end
146
-
147
- if transaction.update(status: :cancelled)
148
- render json: {
149
- success: true,
150
- data: transaction_data(transaction),
151
- message: "Transaction cancelled successfully"
152
- }
153
- else
154
- render json: {
155
- success: false,
156
- error: "Failed to cancel transaction",
157
- errors: transaction.errors.full_messages
158
- }, status: :unprocessable_entity
159
- end
160
- rescue ActiveRecord::RecordNotFound
161
- render json: {
162
- success: false,
163
- error: "Transaction not found"
164
- }, status: :not_found
165
- end
166
-
167
- # Get transaction details with related account information
168
- def details
169
- transaction = Dscf::Banking::Transaction.includes(:account, :debit_account, :credit_account, :transaction_type).find(params[:id])
170
-
171
- render json: {
172
- success: true,
173
- data: transaction_data_with_details(transaction)
174
- }
175
- rescue ActiveRecord::RecordNotFound
176
- render json: {
177
- success: false,
178
- error: "Transaction not found"
179
- }, status: :not_found
180
- end
181
-
182
- private
183
-
184
- def transfer_params
185
- params.require(:transfer).permit(:debit_account_number, :credit_account_number, :amount, :description, :transaction_type_code)
186
- end
187
-
188
- def deposit_params
189
- params.require(:deposit).permit(:account_number, :amount, :description, :transaction_type_code)
190
- end
191
-
192
- def withdrawal_params
193
- params.require(:withdrawal).permit(:account_number, :amount, :description, :transaction_type_code)
194
- end
195
-
196
- def transaction_data(transaction)
197
- {
198
- id: transaction.id,
199
- reference_number: transaction.reference_number,
200
- amount: transaction.amount.to_f,
201
- currency: transaction.currency,
202
- description: transaction.description,
203
- status: transaction.status,
204
- account_id: transaction.account_id,
205
- debit_account_id: transaction.debit_account_id,
206
- credit_account_id: transaction.credit_account_id,
207
- transaction_type_id: transaction.transaction_type_id,
208
- created_at: transaction.created_at,
209
- updated_at: transaction.updated_at
210
- }
211
- end
212
-
213
- def transaction_data_with_details(transaction)
214
- {
215
- id: transaction.id,
216
- reference_number: transaction.reference_number,
217
- amount: transaction.amount.to_f,
218
- currency: transaction.currency,
219
- description: transaction.description,
220
- status: transaction.status,
221
- account: account_summary(transaction.account),
222
- debit_account: account_summary(transaction.debit_account),
223
- credit_account: account_summary(transaction.credit_account),
224
- transaction_type: {
225
- id: transaction.transaction_type.id,
226
- code: transaction.transaction_type.code,
227
- name: transaction.transaction_type.name
228
- },
229
- created_at: transaction.created_at,
230
- updated_at: transaction.updated_at
231
- }
232
- end
233
-
234
- def account_summary(account)
235
- return nil unless account
236
- {
237
- id: account.id,
238
- account_number: account.account_number,
239
- name: account.name,
240
- currency: account.currency,
241
- current_balance: account.current_balance
242
- }
243
- end
244
-
245
- def model_params
246
- params.require(:payload).permit(
247
- :account_id,
248
- :transaction_type_id,
249
- :debit_account_id,
250
- :credit_account_id,
251
- :amount,
252
- :currency,
253
- :description,
254
- :status
255
- )
256
- end
257
-
258
- def eager_loaded_associations
259
- [ :account, :transaction_type, :debit_account, :credit_account ]
260
- end
261
-
262
- def allowed_order_columns
263
- %w[id reference_number amount currency status created_at updated_at]
264
- end
265
-
266
- def default_serializer_includes
267
- {
268
- account: {},
269
- transaction_type: {},
270
- debit_account: {},
271
- credit_account: {}
272
- }
273
- end
274
- end
275
- end
@@ -1,68 +0,0 @@
1
- module Dscf::Banking
2
- class Transaction < ApplicationRecord
3
- belongs_to :account, class_name: "Dscf::Banking::Account"
4
- belongs_to :transaction_type, class_name: "Dscf::Banking::TransactionType"
5
- belongs_to :debit_account, class_name: "Dscf::Banking::Account"
6
- belongs_to :credit_account, class_name: "Dscf::Banking::Account"
7
-
8
- enum :status, {
9
- pending: 0,
10
- processing: 1,
11
- completed: 2,
12
- failed: 3,
13
- cancelled: 4
14
- }
15
-
16
- validates :amount, presence: true, numericality: { greater_than: 0 }
17
- validates :currency, presence: true
18
- validates :reference_number, presence: true, uniqueness: true
19
-
20
- validate :different_debit_credit_accounts
21
- validate :account_currency_consistency
22
-
23
- before_validation :set_defaults, on: :create
24
- before_validation :generate_reference_number, if: -> { reference_number.nil? }
25
-
26
- scope :by_account, ->(account_id) { where(account_id: account_id) }
27
- scope :by_type, ->(type_code) { joins(:transaction_type).where(dscf_banking_transaction_types: { code: type_code.upcase }) }
28
- scope :by_status, ->(status) { where(status: status) }
29
- scope :recent, -> { order(created_at: :desc) }
30
-
31
- def settlement_transaction?
32
- debit_account.system_account? || credit_account.system_account?
33
- end
34
-
35
- def customer_transaction?
36
- !settlement_transaction?
37
- end
38
-
39
- private
40
-
41
- def different_debit_credit_accounts
42
- return unless debit_account_id && credit_account_id
43
-
44
- if debit_account_id == credit_account_id
45
- errors.add(:credit_account, "must be different from debit account")
46
- end
47
- end
48
-
49
- def account_currency_consistency
50
- return unless account && currency
51
-
52
- if account.currency != currency
53
- errors.add(:currency, "must match account currency")
54
- end
55
- end
56
-
57
- def set_defaults
58
- self.currency ||= account&.currency || "ETB"
59
- self.status ||= :pending
60
- end
61
-
62
- def generate_reference_number
63
- timestamp = Time.current.strftime("%Y%m%d%H%M%S")
64
- random_suffix = SecureRandom.hex(4).upcase
65
- self.reference_number = "TXN#{timestamp}#{random_suffix}"
66
- end
67
- end
68
- end
@@ -1,20 +0,0 @@
1
- module Dscf::Banking
2
- class TransactionType < ApplicationRecord
3
- has_many :transactions, class_name: "Dscf::Banking::Transaction", dependent: :restrict_with_error
4
-
5
- validates :code, presence: true, uniqueness: { case_sensitive: false }
6
- validates :name, presence: true
7
-
8
- before_save :upcase_code
9
-
10
- def self.find_by_code(code)
11
- find_by(code: code.to_s.upcase)
12
- end
13
-
14
- private
15
-
16
- def upcase_code
17
- self.code = code.upcase if code.present?
18
- end
19
- end
20
- end
@@ -1,19 +0,0 @@
1
- module Dscf::Banking
2
- class TransactionSerializer < ActiveModel::Serializer
3
- attributes :id, :reference_number, :amount, :currency, :description,
4
- :status, :created_at, :updated_at
5
-
6
- belongs_to :account
7
- belongs_to :transaction_type
8
- belongs_to :debit_account, serializer: Dscf::Banking::AccountSerializer
9
- belongs_to :credit_account, serializer: Dscf::Banking::AccountSerializer
10
-
11
- def status
12
- object.status.humanize
13
- end
14
-
15
- def amount
16
- object.amount.to_f
17
- end
18
- end
19
- end
@@ -1,9 +0,0 @@
1
- module Dscf::Banking
2
- class TransactionTypeSerializer < ActiveModel::Serializer
3
- attributes :id, :code, :name, :description, :created_at, :updated_at
4
-
5
- def code
6
- object.code.upcase
7
- end
8
- end
9
- end
@@ -1,99 +0,0 @@
1
- module Dscf::Banking
2
- class BaseTransactionService
3
- attr_reader :errors
4
-
5
- def initialize
6
- @errors = []
7
- end
8
-
9
- def success?
10
- errors.empty?
11
- end
12
-
13
- def failure?
14
- !success?
15
- end
16
-
17
- private
18
-
19
- def add_error(message)
20
- @errors << message
21
- end
22
-
23
- def validate_account_active(account, account_type = "Account")
24
- unless account.active? && account.status == "active"
25
- add_error("#{account_type} is not active")
26
- return false
27
- end
28
- true
29
- end
30
-
31
- def validate_sufficient_funds(account, amount)
32
- unless account.sufficient_funds_for_withdrawal?(amount)
33
- add_error("Insufficient funds. Available balance: #{account.available_balance}, Required: #{amount}")
34
- return false
35
- end
36
- true
37
- end
38
-
39
- def find_or_create_transaction_type(code)
40
- transaction_type = Dscf::Banking::TransactionType.find_by_code(code)
41
- unless transaction_type
42
- add_error("Transaction type '#{code}' not found")
43
- return nil
44
- end
45
- transaction_type
46
- end
47
-
48
- def create_transaction(params)
49
- transaction = Dscf::Banking::Transaction.new(params)
50
- unless transaction.save
51
- transaction.errors.full_messages.each { |msg| add_error(msg) }
52
- return nil
53
- end
54
- transaction
55
- end
56
-
57
- def update_account_balances(debit_account, credit_account, amount)
58
- ActiveRecord::Base.transaction do
59
- # Update debit account (decrease balance)
60
- new_debit_balance = debit_account.current_balance - amount
61
- new_debit_available = debit_account.available_balance - amount
62
-
63
- unless debit_account.update(
64
- current_balance: new_debit_balance,
65
- available_balance: new_debit_available
66
- )
67
- debit_account.errors.full_messages.each { |msg| add_error("Debit account: #{msg}") }
68
- raise ActiveRecord::Rollback
69
- end
70
-
71
- # Update credit account (increase balance)
72
- new_credit_balance = credit_account.current_balance + amount
73
- new_credit_available = credit_account.available_balance + amount
74
-
75
- unless credit_account.update(
76
- current_balance: new_credit_balance,
77
- available_balance: new_credit_available
78
- )
79
- credit_account.errors.full_messages.each { |msg| add_error("Credit account: #{msg}") }
80
- raise ActiveRecord::Rollback
81
- end
82
- end
83
-
84
- success?
85
- end
86
-
87
- def process_transaction(transaction)
88
- transaction.update(status: :processing)
89
-
90
- if update_account_balances(transaction.debit_account, transaction.credit_account, transaction.amount)
91
- transaction.update(status: :completed)
92
- true
93
- else
94
- transaction.update(status: :failed)
95
- false
96
- end
97
- end
98
- end
99
- end
@@ -1,97 +0,0 @@
1
- module Dscf::Banking
2
- class DepositService < BaseTransactionService
3
- attr_reader :transaction
4
-
5
- def initialize(account:, amount:, description:, transaction_type_code: "DEPOSIT")
6
- super()
7
- @account = account
8
- @amount = amount.to_f
9
- @description = description
10
- @transaction_type_code = transaction_type_code
11
- @transaction = nil
12
- end
13
-
14
- def execute
15
- return self unless validate_deposit
16
-
17
- ActiveRecord::Base.transaction do
18
- create_deposit_transaction
19
- return self unless success?
20
-
21
- process_deposit
22
- return self unless success?
23
- end
24
-
25
- self
26
- end
27
-
28
- private
29
-
30
- def validate_deposit
31
- # Validate account
32
- return false unless validate_account_active(@account, "Target account")
33
-
34
- # Validate amount
35
- if @amount <= 0
36
- add_error("Deposit amount must be greater than zero")
37
- return false
38
- end
39
-
40
- true
41
- end
42
-
43
- def create_deposit_transaction
44
- transaction_type = find_or_create_transaction_type(@transaction_type_code)
45
- return unless transaction_type
46
-
47
- # For deposits, we need a system account as the debit account
48
- # This represents the external source (like mobile banking - Abole)
49
- system_account = find_or_create_system_account
50
-
51
- @transaction = create_transaction(
52
- account: @account, # Primary account for the transaction
53
- transaction_type: transaction_type,
54
- debit_account: system_account, # External source
55
- credit_account: @account, # Customer account receiving the deposit
56
- amount: @amount,
57
- currency: @account.currency,
58
- description: @description,
59
- status: :pending
60
- )
61
- end
62
-
63
- def process_deposit
64
- @transaction.update(status: :processing)
65
-
66
- # Update account balance (increase for deposit)
67
- new_balance = @account.current_balance + @amount
68
- new_available = @account.available_balance + @amount
69
-
70
- if @account.update(current_balance: new_balance, available_balance: new_available)
71
- @transaction.update(status: :completed)
72
- true
73
- else
74
- @account.errors.full_messages.each { |msg| add_error(msg) }
75
- @transaction.update(status: :failed)
76
- false
77
- end
78
- end
79
-
80
- def find_or_create_system_account
81
- # Find or create a system account for external deposits
82
- system_account = Dscf::Banking::Account.system_accounts
83
- .where(currency: @account.currency)
84
- .where("name LIKE ?", "%Deposit%")
85
- .first
86
-
87
- unless system_account
88
- # In a real system, this would be pre-created during setup
89
- # For now, we'll assume it exists or create a placeholder
90
- add_error("System deposit account not found for currency #{@account.currency}")
91
- return nil
92
- end
93
-
94
- system_account
95
- end
96
- end
97
- end
@@ -1,78 +0,0 @@
1
- module Dscf::Banking
2
- class TransferService < BaseTransactionService
3
- attr_reader :transaction
4
-
5
- def initialize(debit_account:, credit_account:, amount:, description:, transaction_type_code: "TRANSFER")
6
- super()
7
- @debit_account = debit_account
8
- @credit_account = credit_account
9
- @amount = amount.to_f
10
- @description = description
11
- @transaction_type_code = transaction_type_code
12
- @transaction = nil
13
- end
14
-
15
- def execute
16
- return self unless validate_transfer
17
-
18
- ActiveRecord::Base.transaction do
19
- create_transfer_transaction
20
- return self unless success?
21
-
22
- process_transaction(@transaction)
23
- return self unless success?
24
- end
25
-
26
- self
27
- end
28
-
29
- private
30
-
31
- def validate_transfer
32
- # Validate debit account
33
- return false unless validate_account_active(@debit_account, "Debit account")
34
-
35
- # Validate credit account
36
- return false unless validate_account_active(@credit_account, "Credit account")
37
-
38
- # Validate amount
39
- if @amount <= 0
40
- add_error("Transfer amount must be greater than zero")
41
- return false
42
- end
43
-
44
- # Validate sufficient funds
45
- return false unless validate_sufficient_funds(@debit_account, @amount)
46
-
47
- # Validate different accounts
48
- if @debit_account.id == @credit_account.id
49
- add_error("Cannot transfer to the same account")
50
- return false
51
- end
52
-
53
- # Validate currency match
54
- if @debit_account.currency != @credit_account.currency
55
- add_error("Currency mismatch between accounts")
56
- return false
57
- end
58
-
59
- true
60
- end
61
-
62
- def create_transfer_transaction
63
- transaction_type = find_or_create_transaction_type(@transaction_type_code)
64
- return unless transaction_type
65
-
66
- @transaction = create_transaction(
67
- account: @debit_account, # Primary account for the transaction
68
- transaction_type: transaction_type,
69
- debit_account: @debit_account,
70
- credit_account: @credit_account,
71
- amount: @amount,
72
- currency: @debit_account.currency,
73
- description: @description,
74
- status: :pending
75
- )
76
- end
77
- end
78
- end