Advertisement
Guest User

customers_controller.rb

a guest
May 25th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.25 KB | None | 0 0
  1. module Api::V2
  2.   class Events::CustomersController < BaseController
  3.     before_action :set_customer, except: %i[index create]
  4.     before_action :check_credits, only: %i[topup virtual_topup]
  5.  
  6.     # POST api/v2/events/:event_id/customers/:id/ban
  7.     def gtag_replacement
  8.       old_gtag = @customer.active_gtag
  9.       new_gtag = @current_event.gtags.find_or_initialize_by(tag_uid: params[:new_tag_uid])
  10.       render(json: new_gtag.errors, status: :unprocessable_entity) && return unless new_gtag.validate_assignation
  11.       render(json: { customer: "Has no current active Gtag" }, status: :unprocessable_entity) && return if old_gtag.nil?
  12.  
  13.       old_gtag.replace!(new_gtag)
  14.       render json: @customer.reload, serializer: Full::CustomerSerializer
  15.     end
  16.  
  17.     # POST api/v2/events/:event_id/customers/:id/ban
  18.     def ban
  19.       @customer.credentials.map { |c| c.update(banned: true) }
  20.       render json: @customer, serializer: Full::CustomerSerializer
  21.     end
  22.  
  23.     # POST api/v2/events/:event_id/customers/:id/unban
  24.     def unban
  25.       @customer.credentials.map { |c| c.update(banned: false) }
  26.       render json: @customer, serializer: Full::CustomerSerializer
  27.     end
  28.  
  29.     # POST api/v2/events/:event_id/customers/:id/assign_gtag
  30.     def assign_gtag
  31.       params[:active] ||= "true"
  32.       @code = params[:tag_uid].strip
  33.       @gtag = @current_event.gtags.find_or_initialize_by(tag_uid: @code)
  34.  
  35.       render(json: @gtag.errors, status: :unprocessable_entity) && return unless @gtag.validate_assignation
  36.  
  37.       @gtag.assign_customer(@customer, @current_user) unless @gtag.customer == @customer
  38.       @gtag.make_active! if params[:active].eql?("true")
  39.  
  40.       render json: @customer, serializer: Full::CustomerSerializer
  41.     end
  42.  
  43.     # POST api/v2/events/:event_id/customers/:id/assign_gtag
  44.     def assign_ticket
  45.       @code = params[:code].strip
  46.       @ticket = @current_event.tickets.find_or_initialize_by(code: @code)
  47.       render(json: @ticket.errors, status: :unprocessable_entity) && return unless @ticket.validate_assignation
  48.  
  49.       @ticket.assign_customer(@customer, @current_user) unless @ticket.customer == @customer
  50.       render json: @customer, serializer: Full::CustomerSerializer
  51.     end
  52.  
  53.     # POST api/v2/events/:event_id/customers/:id/topup
  54.     def topup
  55.       credits = params[:credits].to_f
  56.       @order = @customer.build_order([[@current_event.credit.id, credits]], params.permit(:money_base, :money_fee))
  57.  
  58.       if @order.save
  59.         @order.complete!(params[:gateway], {}, params[:send_email])
  60.         render json: @order, serializer: OrderSerializer
  61.       else
  62.         render json: @order.errors, status: :unprocessable_entity
  63.       end
  64.     end
  65.  
  66.     # POST api/v2/events/:event_id/customers/:id/virtual_topup
  67.     def virtual_topup
  68.       credits = params[:credits].to_f
  69.       @order = @customer.build_order([[@current_event.virtual_credit.id, credits]], params.permit(:money_base, :money_fee))
  70.  
  71.       if @order.save
  72.         @order.complete!(params[:gateway], {}, params[:send_email])
  73.         render json: @order, serializer: OrderSerializer
  74.       else
  75.         render json: @order.errors, status: :unprocessable_entity
  76.       end
  77.     end
  78.  
  79.     # POST api/v2/events/:event_id/customers/:id/refunds
  80.     def refund
  81.       fee = @current_event.refund_fee.to_f
  82.       base = @customer.credits - fee
  83.       params[:gateway] ||= "other"
  84.  
  85.       @refund = @current_event.refunds.new(credit_base: base, credit_fee: fee, customer: @customer, gateway: params[:gateway])
  86.       if @refund.save
  87.         @refund.complete!(params[:send_email])
  88.         render json: @refund, status: :created, location: [:admins, @current_event, @refund]
  89.       else
  90.         render json: @refund.errors, status: :unprocessable_entity
  91.       end
  92.     end
  93.  
  94.     # GET api/v2/events/:event_id/customers/:id/refunds
  95.     def refunds
  96.       @refunds = @customer.refunds
  97.       render json: @refunds, each_serializer: RefundSerializer
  98.     end
  99.  
  100.     # GET api/v2/events/:event_id/customers/:id/transactions
  101.     def transactions
  102.       render json: @customer, serializer: Full::CustomerTransactionsSerializer
  103.     end
  104.  
  105.     # GET api/v2/events/:event_id/customers
  106.     def index
  107.       @customers = @current_event.customers
  108.       authorize @customers
  109.  
  110.       paginate json: @customers, each_serializer: Simple::CustomerSerializer
  111.     end
  112.  
  113.     # GET api/v2/events/:event_id/customers/:id
  114.     def show
  115.       render json: @customer, serializer: Full::CustomerSerializer
  116.     end
  117.  
  118.     # POST api/v2/events/:event_id/customers
  119.     def create
  120.       new_params = customer_params.merge(anonymous: false, agreed_on_registration: true)
  121.       new_params[:password_confirmation] = new_params[:password]
  122.       new_params[:encrypted_password] = SecureRandom.hex(24) if new_params[:password].blank? && new_params[:password_confirmation].blank?
  123.  
  124.       @customer = @current_event.customers.new(new_params)
  125.       @customer.skip_confirmation!
  126.       authorize @customer
  127.  
  128.       if @customer.save
  129.         render json: @customer, serializer: Full::CustomerSerializer, status: :created, location: [:admins, @current_event, @customer]
  130.       else
  131.         render json: @customer.errors, status: :unprocessable_entity
  132.       end
  133.     end
  134.  
  135.     # PATCH/PUT api/v2/events/:event_id/customers/:id
  136.     def update
  137.       if @customer.update(customer_params)
  138.         render json: @customer, serializer: Full::CustomerSerializer
  139.       else
  140.         render json: @customer.errors, status: :unprocessable_entity
  141.       end
  142.     end
  143.  
  144.     # DELETE api/v2/events/:event_id/customers/:id
  145.     def destroy
  146.       @customer.destroy
  147.       head(:ok)
  148.     end
  149.  
  150.     private
  151.  
  152.     def check_credits
  153.       render(json: { credits: "Must be present and positive" }, status: :unprocessable_entity) unless params[:credits].to_f.positive?
  154.     end
  155.  
  156.     def set_customer
  157.       customers = @current_event.customers
  158.       customer_id = customers.find_by(id: params[:id])&.id || customers.find_by(email: params[:id])&.id
  159.  
  160.       @customer = customers.find(customer_id)
  161.       authorize @customer
  162.     end
  163.  
  164.     # Only allow a trusted parameter "white list" through.
  165.     def customer_params
  166.       params.require(:customer).permit(:first_name, :last_name, :email, :phone, :birthdate, :postcode, :address, :city, :country, :gender, :password)
  167.     end
  168.   end
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement