BananaCrystal / email-templates

This repository contains in-house email templates that serve as structure for our emails. Each template can be updated and modified to fit requirements.
MIT License
0 stars 0 forks source link

crypto_transfers.rb #16

Open geekelo opened 1 month ago

geekelo commented 1 month ago

FILE

ActiveAdmin.register CryptoTransfer do
  actions :all, except: :destroy

  includes :user, :source_wallet, :destination_wallet

  config.sort_order = 'created_at_desc'

  scope('Completed Today', default: true) do |scope|
    scope.where(created_at: 1.day.ago..Time.zone.now, external_status: 'completed')
  end
  scope('Completed in The Last Week') do |scope|
    scope.where(created_at: 7.days.ago..Time.zone.now, external_status: 'completed')
  end  
  scope('Has_Fees') { |scope| scope.where('fees > ?', 0).where(external_status: 'completed') }

  scope('Completed') { |scope| scope.where(external_status: 'completed') }
  scope('Pending') { |scope| scope.where(external_status: 'pending') }
  scope('Confirmed') { |scope| scope.where(external_status: 'confirmed') }
  scope('Unconfirmed') { |scope| scope.where(external_status: 'unconfirmed') }
  scope('Error') { |scope| scope.where(external_status: %w[error failed]) }
  scope('All') { |scope| scope.where.not(external_status: ['error']) }

  menu parent: 'Transactions', label: 'Crypto Transfers'
  # See permitted parameters documentation:
  # https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
  #
  # preserve_default_filters!
  filter :user_email_cont, as: :string, label: 'Email'
  filter :id_eq, as: :string, label: 'ID'
  filter :external_id_eq, as: :string, label: 'External ID'
  filter :wallet_transfer_type
  filter :destination_type
  filter :status
  filter :external_status
  filter :fees
  filter :source
  filter :destination
  filter :source_currency
  filter :destination_currency
  filter :confirmed
  filter :sender_ref
  # Uncomment all parameters which should be permitted for assignment
  #
  # permit_params :owner, :external_id, :source, :destination, :source_amount, :destination_amount, :source_currency, 
  # :destination_currency, :custom_id, :completed_at, :cancelled_at, :failure_reason, :expires_at, :external_status, :status, 
  # :gross_amount, :gross_amount_currency, :fees, :fees_currency, :net_amount, :net_amount_currency, :provider_amount, 
  # :provider_amount_currency, :provider_fees, :provider_fees_currency, :external_fees, :blockchain_tx, :reversal_reason, 
  # :reversing_sub_status, :status_histories, :pending_sub_status, :message, :wallet_transfer_type, :recipient, :currency, 
  # :blockchain_address, :destination_type, :description, :exchange_rate, :confirm_token, :confirmed, :user_id, :source_wallet_id, 
  # :destination_wallet_id, :total_external_fees, :estimated_arrival

  permit_params :status, :external_status, :note, :created_at

  index do
    selectable_column
    id_column
    column '' do |transfer|
      from = transfer&.user&.name
      to =  transfer&.destination_wallet ? transfer&.destination_wallet&.user&.name : 'Blockchain'
      store = transfer&.destination_wallet&.store_wallet? ? transfer&.destination_wallet&.store&.name : ''

      "#{from} -> #{to} <br><strong>#{store}</strong>".html_safe
    end
    column 'Type' do |transfer|
      transfer_type = if transfer.source_wallet == transfer.destination_wallet
        "wallet -> wallet<br><strong>#{transfer.convert? ? 'convert' : ''}</strong>".html_safe
      else
       "#{transfer.source_type} -> #{transfer.destination_wallet ? 'wallet' : transfer.destination_type} <br>#{transfer.wallet_transfer_type} <br><strong>#{transfer.convert? ? 'convert' : ''}</strong>".html_safe
      end
      "#{transfer_type} - #{transfer.type}".html_safe
    end
    column :gross_amount do |transfer|
      "#{transfer.gross_amount} #{transfer.gross_amount_currency}"
    end
    column :fees do |transfer|
      "#{transfer.fees.present? ? BigDecimal(transfer.fees)&.round(6) : 0.00} #{transfer.fees_currency}"
    end
    column :net_amount do |transfer|
      "#{transfer.net_amount} #{transfer.net_amount_currency}"
    end
    column :external_id
    column :source_amount do |transfer|
      "#{transfer.source_amount} #{transfer.source_currency}"
    end
    column 'External Fees' do |transfer|
      "#{transfer.total_external_fees.present? ? BigDecimal(transfer.total_external_fees)&.round(6) : 0.00} #{transfer.source_currency}"
    end
    column :destination_amount do |transfer|
      "#{transfer.destination_amount} #{transfer.destination_currency}"
    end
    column :status do |transfer|
      "#{transfer.external_status} #{transfer.status}"
    end
    column :status
    column :expires_at
    column :updated_at
    actions
  end

  show do
    panel 'Summary' do
      table_for crypto_transfer do
        column :source_wallet_user
        column 'Transfer Type' do |transfer|
          if transfer.source_wallet == transfer.destination_wallet
            "wallet -> wallet <br><strong>#{transfer.type}</strong>".html_safe
          else
            "#{transfer.transfer_type(transfer.user.default_crypto_api_wallet)} - #{transfer.wallet_transfer_type} <br><strong>#{transfer.type}</strong>".html_safe
          end
        end
        column :destination_wallet_user do |transfer|
          transfer.destination_wallet_user || 'external'
        end
        column :gross_amount do |transfer|
          "#{transfer.gross_amount} #{transfer.gross_amount_currency}"
        end
        column :fees do |transfer|
          "External Fees: #{transfer.external_fees.presence || '{}'} <br/>
          BananaCrystal Fees: #{transfer.bananacrystal_fees.presence || 0.00} #{transfer.bananacrystal_crypto_fees.presence || 0.00} #{transfer.fees_currency}<br/>
          One Time Setup Fee: #{transfer.first_time_withdrawal_fee.presence&.round(8) || 0.00} #{transfer.fees_currency}<br/>
          Fees: #{transfer.fees.presence&.round(8) || 0.00} #{transfer.fees_currency}<br/><br>
          <strong>Total Fees: #{transfer.total_fees.presence&.round(8) || 0.00} #{transfer.fees_currency}</strong>".html_safe
        end
        column :net_amount do |transfer|
          "#{transfer.net_amount} #{transfer.net_amount_currency}"
        end
        column :external_status
      end
    end

    panel 'Master Wallet Transactions' do
      table_for crypto_transfer.master_wallet_crypto_transfer_fees do
        column :id do |master_wallet_transfer|
          link_to master_wallet_transfer.id, admin_master_wallet_crypto_transfer_fee_path(master_wallet_transfer)
        end
        column :user
        column :destination do |master_wallet_transfer|
          if master_wallet_transfer&.destination
            Wallet.find_by(external_id: master_wallet_transfer&.destination&.split(":")[1]).user
          end
        end
        column :amount
        column :currency
        column :external_status
        column :status
        column :failure_reason
        column :updated_at
      end
    end

    default_main_content
  end

  form do |f|
    f.inputs do
      f.input :status, as: :select, collection: CryptoTransfer::STATUS.values
      f.input :external_status, as: :select, collection: CryptoTransfer::EXTERNAL_STATUS.values

      if f.object.type == 'CryptoTransfers::WyreMigration' && ['BTC', 'ETH', 'XLM'].include?(f.object.source_currency)
        wyre_migration_recipient = f.object.wyre_migration_recipient
        token = wyre_migration_recipient.convert_to_usdt? ? 'USDT' : wyre_migration_recipient.token
        token_balance = wyre_migration_recipient.convert_to_usdt? ? wyre_migration_recipient.token_balance_in_usdt : wyre_migration_recipient.token_balance
        rate_to_usdt = wyre_migration_recipient.rate_to_usdt
        hr
        h3 'Note for Migration'
        para 'Add equivalent amount USDT and the conversion rate as needed' 
        h4 "Amount Credited: <strong>#{token_balance} USD</strong> (1 #{wyre_migration_recipient.token} = #{rate_to_usdt} USD)".html_safe
        br
        f.input :note
      end
      hr
      h3 'Drift Fix'
      f.input :created_at
      para 'Only update when the completed_at vs created_at time has a huge drift (retroactive fix)'
    end
    f.actions
  end
end
geekelo commented 1 month ago

This file configures the ActiveAdmin interface for managing CryptoTransfer records, with features like scoped views, detailed index and show pages, and custom filters.

File: crypto_transfer.rb

1. Actions Configuration

2. Associations Configuration

3. Sorting Configuration

4. Scopes Configuration

5. Menu Configuration

6. Filters Configuration

7. Permitted Parameters

8. Index Page Configuration

9. Show Page Configuration

10. Form Configuration

Summary: