Open geekelo opened 1 month ago
This code defines the dashboard for an ActiveAdmin interface. ActiveAdmin is a Ruby on Rails framework for creating elegant backends. This dashboard provides a customized interface for managing various aspects of an application, particularly related to financial operations such as updating rates, processing payments, and monitoring various metrics like gas fees, fees earned, verifications, and partner commissions.
Menu and Action Items:
super_admin
or manager
).Panels and Columns:
Dynamic Data Display:
total_fees_in_wallet
, total_bananacrystal_fees
, and total_user_commissions
to dynamically calculate and display financial data. The calculations appear to be related to cryptocurrency transactions and commissions.Conditional Rendering:
Styling and Alerts:
HTML Safe Content:
html_safe
to render raw HTML content within the panels. This allows for more complex formatting, such as displaying balance amounts with specific styles.The code includes several commented-out sections that hint at additional features that might have been used previously or are planned for future use. These include displaying fees earned over different periods (last 7 days, last 30 days, year-to-date), and detailed transaction data for a "MASTER WALLET."
Some action items include links that direct the user to external administrative tools or reports, such as a URL to update rates or manage payouts.
Overall, this dashboard is a comprehensive tool for managing and monitoring various aspects of the application, particularly in relation to cryptocurrency transactions and financial reporting.
app/helpers/admin_helper.rb
module AdminHelper def admin_fee_user User.find_by(email: ENV.fetch('FEES_CRYPTO_WALLET_EMAIL')) end
def admin_fee_wallet admin_fee_user.default_crypto_api_wallet end
def total_fees_in_wallet(token) balances = admin_fee_wallet&.crypto_api_wallet_balances
return balances['available_balances'][token] if balances
0.00
end
def total_fees_in_wallet_usd(token) exchange_rate = if Rate.crypto_api.current_priced_rate(token, 'USDT') Rate.crypto_api.current_priced_rate(token, 'USDT')['USDT'] else 1.0 end total_fees_in_wallet(token) * exchange_rate if total_fees_in_wallet(token) end
def total_bananacrystal_fees_usd_all_tokens total_usd = 0.00 ['BTC', 'ETH', 'DAI','XLM'].each do |token| total_usd += total_bananacrystal_fees_usd(token) end total_usd + total_bananacrystal_fees('USDT') end
def total_bananacrystal_fees(token) MasterWalletCryptoTransferFee.complete.where(currency: token).sum(&:amount) end
def total_bananacrystal_fees_usd(token) exchange_rate = if Rate.current_priced_rate(token, 'USDT') Rate.current_priced_rate(token, 'USDT')['USDT'] else 1.0 end total_bananacrystal_fees(token) * exchange_rate if total_bananacrystal_fees(token) end
def total_partners User.active.partners.count end
def total_user_commissions(token) UserCommission.where(commission_currency: token).sum(&:commission_amount) end
def total_user_commissions_usd(token) exchange_rate = Rate.crypto_api.current_priced_rate(token, 'USDT')&.dig('USDT') || 1.0 total_user_commissions(token) * exchange_rate if total_user_commissions(token) end
def total_user_commissions_due(token) UserCommission.where(commission_currency: token, paid: false).sum(&:commission_amount) end
def total_user_commissions_due_usd(token) exchange_rate = Rate.crypto_api.current_priced_rate(token, 'USDT')&.dig('USDT') || 1.0 total_user_commissions_due(token) * exchange_rate if total_user_commissions_due(token) end
def total_payments Payment.active.sum { |payment| payment.external_status == 'paid' ? payment.gross_amount : 0 } end
def total_payouts Payout.active.sum { |payout| payout.external_status == 'complete' ? payout.gross_amount : 0 } end
def total_transfers Transfer.active.sum { |transfer| transfer.external_status == 'complete' ? transfer.gross_amount : 0 } end
def total_crypto_transfers CryptoTransfer.complete.count end
def total_master_wallet_payment_debit_transfers MasterWalletTransfer.where(transfer_type: 'debit').sum do |transfer| transfer.complete? ? transfer.amount : 0 end end
def total_master_wallet_payout_credit_transfers MasterWalletPayoutTransfer.where(transfer_type: 'credit').sum do |transfer| transfer.complete? ? transfer.amount : 0 end end
def total_master_wallet_payout_fees_credit_transfers MasterWalletPayoutFeeTransfer.where(transfer_type: 'credit').sum do |transfer| transfer.complete? ? transfer.amount : 0 end end
def total_master_wallet_transfer_fees_credit_transfers MasterWalletTransferFeeTransfer.where(transfer_type: 'credit').sum do |transfer| transfer.complete? ? transfer.amount : 0 end end
def total_master_wallet_chargeback_credit_transfers MasterWalletChargebackTransfer.where(transfer_type: 'credit').sum do |transfer| transfer.complete? ? transfer.amount : 0 end end
def total_master_wallet_chargeback_fees_debit_transfers ChargebackItem.where(chargeback_type: '1st Chargeback').sum(:provider_fee) end
def total_master_wallet_credits total_master_wallet_payout_fees_credit_transfers + total_master_wallet_transfer_fees_credit_transfers end
def total_master_wallet_debits total_master_wallet_payment_debit_transfers + total_master_wallet_payout_credit_transfers + total_master_wallet_chargeback_fees_debit_transfers end
def total_net_fees(days = 7) total_payment_net_fees(days) + total_payout_net_fees(days) + total_transfer_net_fees(days) end
def total_payment_net_fees(days = 7) total_payment_fees(days) - total_payment_provider_fees(days) end
def total_payment_fees(days = 7) Payment.where(status: 'processed', external_status: 'paid').where('created_at > ?', days.days.ago).sum(:fees) end
def total_payment_provider_fees(days = 7) Payment.where(status: 'processed', external_status: 'paid').where('created_at > ?', days.days.ago).sum(:provider_fees) end
def total_payout_net_fees(days = 7) total_payout_fees(days) - total_payout_provider_fees(days) end
def total_payout_fees(days = 7) Payout.where(status: 'processed', payout_type: 'payout', external_status: %w[complete failed]) .where('created_at > ?', days.days.ago).sum(:fees) end
def total_payout_provider_fees(days = 7) Payout.where(status: 'processed', payout_type: 'payout', external_status: %w[complete failed]) .where('created_at > ?', days.days.ago).sum(:provider_fees) end
def total_transfer_net_fees(days = 7) total_transfer_fees(days) end
def total_transfer_fees(days = 7) Transfer.where(status: 'processed', external_status: 'complete').where('created_at > ?', days.days.ago).sum(:fees) end
def pending_verifications(days = 7) Verification.where(status: %w[pending]).where('created_at > ?', days.days.ago).order(created_at: :desc) end
def total_referral_rewards(days = 7) Referral.active.due.where('created_at > ?', days.days.ago).sum(:amount) end
def total_referral_rewards_due(days = 30) Referral.active.due.where('created_at > ?', days.days.ago).sum(:amount) end
def admin_list_users User.confirmed.order(:email).map { |u| ["#{u.email} - #{u.name}", u.id] }.compact end
def admin_list_verified_users User.confirmed.order(:email).map do |u| u.verification_accepted? ? ["#{u.email} - #{u.name}", u.id] : nil end.compact end
def admin_list_stores Store.where.not(status: 'inactive').all.map { |s| ["#{s.name} - owner: #{s.owner_name}", s.id] }.compact end
def admin_list_store_invoices(store) StoreInvoice.where(store: store).all.map do |i| ["#{i.external_id} - #{number_to_currency(i.amount)}", i.id] end.compact end
def admin_payment_payment_methods PaymentMethod.where(external_status: 'complete').includes(:user).sort_by(&:user).map do |pm| ["#{pm.user.email} - #{pm.name} (#{pm.description}) - #{pm.payment_method_type}", pm.id] end.compact end
def admin_payment_users User.all.map { |u| ["#{u.email} - #{u.name}", u.id] if u.verification_accepted? }.compact end
def admin_payment_wallets Wallet.where(wallet_type: 'bananacrystal_wallet').includes(:user).sort_by(&:user).map do |w| [w.id] end.compact end
def payment_method_tracking_ref(payment_method_id) payment_method = PaymentMethod.find(payment_method_id) if payment_method_id payment_method&.external_tracking_ref end
def total_days_ytd ((Time.zone.now - Time.zone.now.beginning_of_year) / (60 60 24)).truncate end end
The AdminHelper
module contains a set of methods that provide various administrative functionalities, primarily related to financial calculations, user management, and reports. In the context of the dashboard.rb
file, this helper is likely included or extended to provide access to these methods, allowing the dashboard.rb
to generate data for the admin dashboard. Here's a breakdown of how this helper might be used in dashboard.rb
:
dashboard.rb
Displaying Total Fees in Wallet (USD):
# Assuming 'BTC' is the token being considered
btc_fees_in_wallet_usd = total_fees_in_wallet_usd('BTC')
This could be used to show how much of a specific cryptocurrency (e.g., BTC) the admin fee wallet holds in USD.
Calculating Total Fees for All Tokens:
total_fees_usd = total_bananacrystal_fees_usd_all_tokens
This might be used to display the total USD value of fees across multiple cryptocurrencies.
Displaying User Commissions:
eth_commissions = total_user_commissions('ETH')
eth_commissions_usd = total_user_commissions_usd('ETH')
These methods could be used to show the total commissions earned in a specific cryptocurrency and its USD equivalent.
Tracking Net Fees Over a Time Period:
net_fees = total_net_fees(7) # Total net fees over the last 7 days
This would be useful for showing recent net fees on the dashboard.
Handling KYC Verifications:
pending_kyc_verifications = pending_verifications(7)
This might be used to list or count pending KYC verifications from the last 7 days.
Managing Referral Rewards:
rewards_due = total_referral_rewards_due(30) # Referral rewards due over the last 30 days
This could be used to display the total referral rewards that are due.
Listing Users, Stores, Payment Methods, Wallets, etc.:
user_list = admin_list_users
verified_user_list = admin_list_verified_users
store_list = admin_list_stores
These methods would be used to generate dropdowns or lists on the dashboard for admins to select users, stores, or payment methods.
dashboard.rb
Typically, the methods from AdminHelper
would be invoked within controller actions or views that render the dashboard. For example:
class DashboardController < ApplicationController
include AdminHelper
def index
@total_fees_usd = total_bananacrystal_fees_usd_all_tokens
@net_fees = total_net_fees
@pending_kyc = pending_verifications(7)
@referral_rewards_due = total_referral_rewards_due(30)
@user_list = admin_list_users
@store_list = admin_list_stores
# Other dashboard-related data...
end
end
In this example, the AdminHelper
methods are used to gather data that is then passed to the view for display on the dashboard.
The AdminHelper
module provides a range of utility methods that are likely used within dashboard.rb
to generate the data displayed on the admin dashboard, including financial metrics, user stats, and operational summaries.
FILE