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

trade_disputes_controller.rb #42

Open geekelo opened 3 months ago

geekelo commented 3 months ago
module Api::V1
  class P2pTrades::TradeDisputesController < Api::BaseController
    before_action :find_trade

    def index
      render json: @trade.disputes, each_serializer: P2pTradeDisputeSerializer
    end

    def create
      p2p_trade_dispute = P2pTradeDispute.new(create_service.p2p_trade_dispute_params)
      if p2p_trade_dispute.save!
        P2pApi::TradeDispute::CreateJob.perform_later(current_user.id, @trade.id, p2p_trade_dispute.id)
        render json: p2p_trade_dispute, serializer: P2pTradeDisputeSerializer
      else
        errors = if p2p_trade_dispute && ResourceErrors
                   ResourceErrors.format(p2p_trade_dispute)
                 else
                   Rails.env.production? ? 'An error occurred' : e
                 end
        render json: { errors: errors }, status: :bad_request
      end
    rescue StandardError => e
      Rails.logger.info e

      render json: { errors: e }, status: :bad_request
    end

    private

    def create_service
      P2pApi::Requests::TradeDispute::CreateService.new(
        user: current_user,
        trade: @trade
      )
    end

    def find_trade
      @trade = P2pTrade.find_by(id: params[:trade_id])
      @trade if allow_trade_access
    end

    def allow_trade_access
      @trade&.user == current_user || @trade&.offer_user == current_user
    end
  end
end
geekelo commented 3 months ago

The P2pTrades::TradeDisputesController manages disputes related to P2P trades. Here’s a detailed overview of its functionality:

P2pTrades::TradeDisputesController Overview

Actions

Private Methods

Error Handling

Example Usage

  1. Index:

    • Request to /api/v1/p2p_trades/:trade_id/trade_disputes
    • Returns a list of disputes related to the specified trade.
  2. Create:

    • Request to /api/v1/p2p_trades/:trade_id/trade_disputes with appropriate parameters.
    • Creates a dispute, triggers a job, and returns the created dispute or errors.

Summary

If you need further details on specific parts or have additional questions, let me know!