Class: Dscf::Banking::TransactionsController

Inherits:
ApplicationController show all
Includes:
DemoPermissionBypass, Core::Common
Defined in:
app/controllers/dscf/banking/transactions_controller.rb

Instance Method Summary collapse

Methods included from DemoPermissionBypass

#authorize_review_action!, #bypass_permissions_for_demo?, #pundit_user

Methods inherited from ApplicationController

#bypass_permissions_for_demo?, #pundit_user

Instance Method Details

#cancelObject

Cancel a transaction



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/dscf/banking/transactions_controller.rb', line 145

def cancel
  transaction = Dscf::Banking::Transaction.find(params[:id])

  unless transaction.status == "pending"
    return render json: {
      success: false,
      error: "Cannot cancel transaction",
      errors: [ "Only pending transactions can be cancelled" ]
    }, status: :unprocessable_entity
  end

  if transaction.update(status: :cancelled)
    render json: {
      success: true,
      data: transaction_data(transaction),
      message: "Transaction cancelled successfully"
    }
  else
    render json: {
      success: false,
      error: "Failed to cancel transaction",
      errors: transaction.errors.full_messages
    }, status: :unprocessable_entity
  end
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Transaction not found"
  }, status: :not_found
end

#depositObject

Deposit money to account (from mobile banking - Abole)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/dscf/banking/transactions_controller.rb', line 65

def deposit
   = Dscf::Banking::Account.find_by(account_number: deposit_params[:account_number])

  unless 
    return render json: {
      success: false,
      error: "Account not found",
      errors: [ "Account with number #{deposit_params[:account_number]} not found" ]
    }, status: :not_found
  end

  result = Dscf::Banking::DepositService.new(
    account: ,
    amount: deposit_params[:amount],
    description: deposit_params[:description] || "Deposit to account",
    transaction_type_code: deposit_params[:transaction_type_code] || "DEPOSIT"
  ).execute

  if result.success?
    render json: {
      success: true,
      data: transaction_data(result.transaction),
      message: "Deposit completed successfully"
    }
  else
    render json: {
      success: false,
      error: "Deposit failed",
      errors: result.errors
    }, status: :unprocessable_entity
  end
rescue StandardError => e
  render json: {
    success: false,
    error: "Deposit failed",
    errors: [ e.message ]
  }, status: :internal_server_error
end

#detailsObject

Get transaction details with related account information



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/dscf/banking/transactions_controller.rb', line 177

def details
  transaction = Dscf::Banking::Transaction.includes(:account, :debit_account, :credit_account, :transaction_type).find(params[:id])

  render json: {
    success: true,
    data: transaction_data_with_details(transaction)
  }
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Transaction not found"
  }, status: :not_found
end

#transferObject

Transfer money between accounts (from test cases)



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
# File 'app/controllers/dscf/banking/transactions_controller.rb', line 15

def transfer
   = Dscf::Banking::Account.find_by(account_number: transfer_params[:debit_account_number])
   = Dscf::Banking::Account.find_by(account_number: transfer_params[:credit_account_number])

  unless 
    return render json: {
      success: false,
      error: "Debit account not found",
      errors: [ "Account with number #{transfer_params[:debit_account_number]} not found" ]
    }, status: :not_found
  end

  unless 
    return render json: {
      success: false,
      error: "Credit account not found",
      errors: [ "Account with number #{transfer_params[:credit_account_number]} not found" ]
    }, status: :not_found
  end

  result = Dscf::Banking::TransferService.new(
    debit_account: ,
    credit_account: ,
    amount: transfer_params[:amount],
    description: transfer_params[:description] || "Transfer between accounts",
    transaction_type_code: transfer_params[:transaction_type_code] || "TRANSFER"
  ).execute

  if result.success?
    render json: {
      success: true,
      data: transaction_data(result.transaction),
      message: "Transfer completed successfully"
    }
  else
    render json: {
      success: false,
      error: "Transfer failed",
      errors: result.errors
    }, status: :unprocessable_entity
  end
rescue StandardError => e
  render json: {
    success: false,
    error: "Transfer failed",
    errors: [ e.message ]
  }, status: :internal_server_error
end

#withdrawalObject

Withdraw money from account



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/dscf/banking/transactions_controller.rb', line 105

def withdrawal
   = Dscf::Banking::Account.find_by(account_number: withdrawal_params[:account_number])

  unless 
    return render json: {
      success: false,
      error: "Account not found",
      errors: [ "Account with number #{withdrawal_params[:account_number]} not found" ]
    }, status: :not_found
  end

  result = Dscf::Banking::WithdrawalService.new(
    account: ,
    amount: withdrawal_params[:amount],
    description: withdrawal_params[:description] || "Withdrawal from account",
    transaction_type_code: withdrawal_params[:transaction_type_code] || "WITHDRAWAL"
  ).execute

  if result.success?
    render json: {
      success: true,
      data: transaction_data(result.transaction),
      message: "Withdrawal completed successfully"
    }
  else
    render json: {
      success: false,
      error: "Withdrawal failed",
      errors: result.errors
    }, status: :unprocessable_entity
  end
rescue StandardError => e
  render json: {
    success: false,
    error: "Withdrawal failed",
    errors: [ e.message ]
  }, status: :internal_server_error
end