Class: Dscf::Banking::AccountsController

Inherits:
ApplicationController show all
Includes:
DemoPermissionBypass, Core::Common
Defined in:
app/controllers/dscf/banking/accounts_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

#activateObject



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
143
144
145
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 116

def activate
   = Dscf::Banking::Account.find(params[:id])

  unless .can_be_activated?
    return render json: {
      success: false,
      error: "Failed to activate account",
      errors: [ "Account cannot be activated from current status: #{.status}" ]
    }, status: :unprocessable_entity
  end

  if .activate!
    render json: {
      success: true,
      data: (),
      message: "Account activated successfully"
    }
  else
    render json: {
      success: false,
      error: "Failed to activate account",
      errors: .errors.full_messages
    }, status: :unprocessable_entity
  end
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Account not found"
  }, status: :not_found
end

#closeObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 178

def close
   = Dscf::Banking::Account.find(params[:id])

  unless .can_be_closed?
    return render json: {
      success: false,
      error: "Failed to close account",
      errors: [ "Account cannot be closed from current status: #{.status}" ]
    }, status: :unprocessable_entity
  end

  if .close!
    render json: {
      success: true,
      data: (),
      message: "Account closed successfully"
    }
  else
    render json: {
      success: false,
      error: "Failed to close account",
      errors: .errors.full_messages
    }, status: :unprocessable_entity
  end
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Account not found"
  }, status: :not_found
end

#createObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 55

def create
   = Dscf::Banking::Account.new(model_params)

  if .save
    render json: {
      success: true,
      data: ()
    }, status: :created
  else
    render json: {
      success: false,
      error: "Failed to create account",
      errors: .errors.full_messages
    }, status: :unprocessable_entity
  end
end

#destroyObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 94

def destroy
   = Dscf::Banking::Account.find(params[:id])

  if .destroy
    render json: {
      success: true,
      message: "Account deleted successfully"
    }
  else
    render json: {
      success: false,
      error: "Failed to delete account",
      errors: .errors.full_messages
    }, status: :bad_request
  end
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Account not found"
  }, status: :not_found
end

#full_nameObject



32
33
34
35
36
37
38
39
40
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 32

def full_name
   = Dscf::Banking::Account.find_by(account_number: params[:id])
  return render json: { success: false, error: "Account not found" }, status: :not_found unless 

  full_name = .name.to_s.strip
  full_name = "Unnamed" if full_name.empty?

  render json: { success: true, data: { full_name: full_name } }
end

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 6

def index
  accounts = Dscf::Banking::Account.all

  # Apply filters that require joins
  if params[:user_id].present?
    accounts = accounts.joins(:application).where("dscf_banking_applications.user_id = ?", params[:user_id])
  end

  # Apply other filters
  accounts = accounts.where(status: params[:status]) if params[:status].present?
  accounts = accounts.where(currency: params[:currency]) if params[:currency].present?
  accounts = accounts.where(application_id: params[:application_id]) if params[:application_id].present?
  accounts = accounts.where(virtual_account_product_id: params[:virtual_account_product_id]) if params[:virtual_account_product_id].present?
  accounts = accounts.where(active: params[:active]) if params[:active].present?

  # Apply ordering
  order_column = allowed_order_columns.include?(params[:order_by]) ? params[:order_by] : "created_at"
  order_direction = %w[asc desc].include?(params[:order_direction]) ? params[:order_direction] : "desc"
  accounts = accounts.order("#{order_column} #{order_direction}")

  render json: {
    success: true,
    data: accounts.map { || () }
  }
end

#interest_simulationsObject



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 277

def interest_simulations
   = params[:account_number].presence || params[:id]
   = Dscf::Banking::Account.find_by(account_number: )

  unless 
    return render json: {
      success: false,
      error: "Account not found"
    }, status: :not_found
  end

  validation_errors = interest_simulation_validation_errors
  if validation_errors.any?
    return render json: {
      success: false,
      error: "Invalid interest simulation parameters",
      errors: validation_errors
    }, status: :unprocessable_entity
  end

  simulation = Dscf::Banking::InterestSimulationService.new(
    account: ,
    initial_balance: params[:initial_balance],
    start_date: params[:start_date],
    end_date: params[:end_date],
    transactions: params[:transactions] || []
  ).call

  render json: {
    success: true,
    data: simulation
  }
rescue ArgumentError => e
  render json: {
    success: false,
    error: "Invalid interest simulation parameters",
    errors: [ e.message ]
  }, status: :unprocessable_entity
end

#meObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 209

def me
  accounts = Dscf::Banking::Account
    .joins(:application)
    .where("dscf_banking_applications.user_id = ?", current_user.id)
    .includes(:application, :virtual_account_product)

  # Apply ordering
  order_column = allowed_order_columns.include?(params[:order_by]) ? params[:order_by] : "created_at"
  order_direction = %w[asc desc].include?(params[:order_direction]) ? params[:order_direction] : "desc"
  accounts = accounts.order("dscf_banking_accounts.#{order_column} #{order_direction}")

  render json: {
    success: true,
    data: accounts.map { || () },
    message: "User accounts retrieved successfully"
  }
end

#showObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 42

def show
   = Dscf::Banking::Account.find(params[:id])
  render json: {
    success: true,
    data: ()
  }
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Account not found"
  }, status: :not_found
end

#suspendObject



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
175
176
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 147

def suspend
   = Dscf::Banking::Account.find(params[:id])

  unless .can_be_suspended?
    return render json: {
      success: false,
      error: "Failed to suspend account",
      errors: [ "Account cannot be suspended from current status: #{.status}" ]
    }, status: :unprocessable_entity
  end

  if .suspend!
    render json: {
      success: true,
      data: (),
      message: "Account suspended successfully"
    }
  else
    render json: {
      success: false,
      error: "Failed to suspend account",
      errors: .errors.full_messages
    }, status: :unprocessable_entity
  end
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Account not found"
  }, status: :not_found
end

#transactionsObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 227

def transactions
   = Dscf::Banking::Account.find(params[:id])

  transactions = Dscf::Banking::Transaction
    .where("account_id = ? OR debit_account_id = ? OR credit_account_id = ?", .id, .id, .id)
    .includes(:account, :transaction_type, :debit_account, :credit_account)

  # Apply pagination if requested
  page = params[:page].to_i
  per_page = (params[:per_page] || 10).to_i

  # Apply filters
  transactions = transactions.where(status: params[:status]) if params[:status].present?
  transactions = transactions.where(transaction_type_id: params[:transaction_type_id]) if params[:transaction_type_id].present?

  # Apply ordering
  order_column = %w[id reference_number amount currency status created_at updated_at].include?(params[:order_by]) ? params[:order_by] : "created_at"
  order_direction = %w[asc desc].include?(params[:order_direction]) ? params[:order_direction] : "desc"
  transactions = transactions.order("#{order_column} #{order_direction}")

  if page.positive?
    total_count = transactions.count
    total_pages = (total_count.to_f / per_page).ceil
    transactions = transactions.offset((page - 1) * per_page).limit(per_page)

    render json: {
      success: true,
      message: "Account transactions retrieved successfully",
      data: transactions.map { |transaction| transaction_data(transaction) },
      pagination: {
        current_page: page,
        per_page: per_page,
        total_count: total_count,
        total_pages: total_pages
      }
    }
  else
    render json: {
      success: true,
      message: "Account transactions retrieved successfully",
      data: transactions.map { |transaction| transaction_data(transaction) }
    }
  end
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Account not found"
  }, status: :not_found
end

#updateObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/dscf/banking/accounts_controller.rb', line 72

def update
   = Dscf::Banking::Account.find(params[:id])

  if .update(model_params)
    render json: {
      success: true,
      data: ()
    }
  else
    render json: {
      success: false,
      error: "Failed to update account",
      errors: .errors.full_messages
    }, status: :unprocessable_entity
  end
rescue ActiveRecord::RecordNotFound
  render json: {
    success: false,
    error: "Account not found"
  }, status: :not_found
end