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

trades_controller.rb #43

Open geekelo opened 1 month ago

geekelo commented 1 month ago
module Api::V1
  class P2pTrades::TradesController < Api::BaseController
    include ::WalletsHelper
    include ::P2pTrades::TradeHelper

    def index
      unless current_user.profile
        render json: { error: 'Please verify your account to access P2P trades' }, status: :unauthorized
      end
      @trades = Kaminari.paginate_array(find_trades).page(params[:page]).per(20)

      render json: @trades, adapter: :json,
             each_serializer: P2pTradeSerializer,
             meta: meta_attributes(@trades)
    end

    def show
      if @trade
        render json: @trade, serializer: P2pTradeSerializer
      else
        render json: { errors: 'Not Found' }, status: :not_found
      end
    end

    def create
      p2p_trade = P2pTrade.new(start_service.p2p_trade_params)

      unless p2p_trade.available_balance?
        return render json: error_response(:bad_request, 'Not enough balance to create this trade'), 
                      status: :bad_request
      end

      if p2p_trade.save!
        P2pApi::Trade::StartJob.perform_now(p2p_trade, trade_params)

        render json: { trade: p2p_trade }, status: :ok
      end
    rescue StandardError => e
      Rails.logger.info e

      errors = if p2p_trade && ResourceErrors
                 ResourceErrors.format(p2p_trade)
               else
                 Rails.env.production? ? 'An error occurred' : e
               end

      render json: error_response(:bad_request, errors), status: :bad_request
    end

    def paid
      P2pApi::Trade::PaidJob.perform_now(@trade)

      head :ok
    end

    def accept
      if @trade.available_escrow_balance?
        P2pApi::Trade::AcceptJob.perform_now(@trade)
      else
        render json: error_response(:bad_request), status: :bad_request
      end
    end

    def cancel
      P2pApi::Trade::CancelJob.perform_now(@trade)

      head :ok
    end

    def reopen
      unless @trade.system_cancelled?
        return render json: error_response(:bad_request, 'You cannot reopen this trade'), status: :bad_request
      end

      if @trade.available_balance?
        P2pApi::Trade::ReopenJob.perform_now(@trade)
        head :ok
      else
        render json: error_response(:bad_request, 'Not enough balance to reopen this trade'), status: :bad_request
      end
    end

    private

    def find_trades
      profile_id = current_user.profile.id

      P2pTrade.where(buyer_profile_id: profile_id).includes(:user, :disputes)
              .or(P2pTrade.where(seller_profile_id: profile_id).includes(:user, :disputes))
              .order(created_at: :desc)
    end
  end  
end
geekelo commented 1 month ago

The P2pTrades::TradesController manages various actions related to P2P trades. Here's a detailed breakdown:

P2pTrades::TradesController Overview

Actions

Private Methods

Error Handling

Example Usage

  1. Index:

    • Request to /api/v1/p2p_trades/trades
    • Returns a paginated list of trades for the current user.
  2. Show:

    • Request to /api/v1/p2p_trades/trades/:id
    • Returns details of a specific trade.
  3. Create:

    • Request to /api/v1/p2p_trades/trades with trade parameters.
    • Creates a trade if the balance is sufficient and triggers a job to start the trade.
  4. Paid:

    • Request to /api/v1/p2p_trades/trades/:id/paid
    • Marks the trade as paid.
  5. Accept:

    • Request to /api/v1/p2p_trades/trades/:id/accept
    • Accepts the trade if escrow balance is sufficient.
  6. Cancel:

    • Request to /api/v1/p2p_trades/trades/:id/cancel
    • Cancels the trade.
  7. Reopen:

    • Request to /api/v1/p2p_trades/trades/:id/reopen
    • Reopens a system-cancelled trade if balance is sufficient.

Summary

Let me know if you need more details or further clarification!