Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.90 KB | None | 0 0
  1. module V1::Admin
  2.   class CustomersController < ApplicationController
  3.     include Rails::Pagination
  4.     def index
  5.       customers = if check_params
  6.         current_user.customers.order("id ASC")
  7.       else
  8.         filtered_customers
  9.       end
  10.       customers = search_for_customers(customers) if params[:search_value].present?
  11.  
  12.       paginate json: customers, per_page: 25
  13.         end
  14.  
  15.         private
  16.  
  17.     def search_for_customers(customers)
  18.       if is_number?(ConvertFullWidth.call(params[:search_value]))
  19.         customers.joins(:orders).joins(:customer_subscriptions).where('customer_subscriptions.id = :query or customers.shopify_customer_id = :query or orders.shopify_order_id = :query or orders.id = :query', query: "#{ConvertFullWidth.call(params[:search_value]).to_i}").group('customers.id')
  20.       else
  21.         customers.joins(:customer_subscriptions).joins(:orders).where("lower(customer_subscriptions.checkout_data -> 'shipping_address' ->> 'last_name') LIKE :query or lower(customer_subscriptions.checkout_data -> 'shipping_address' ->> 'first_name') LIKE :query or lower(customer_subscriptions.checkout_data ->> 'email') LIKE :query", query: "%#{params[:search_value].downcase}%").group('customers.id')
  22.       end
  23.     end
  24.  
  25.     def filtered_customers
  26.       keys = params[:key].split('_')
  27.       value = params[:value].split('_')
  28.       keys.each_with_index do |key, index|
  29.         if key == "paymentStatusFilter"
  30.           if value[index] == 'Failed'
  31.             Customer.failed_subscriptions_customers
  32.           elsif value[index] == 'Active'
  33.             Customer.active_subscriptions_customers
  34.           end
  35.         elsif key == 'orderCountFilter'
  36.           Customer.customers_with_max_orders
  37.         end
  38.       end
  39.     end
  40.  
  41.     def is_number? (string)
  42.       true if Float(string) rescue false
  43.     end
  44.  
  45.     def check_params
  46.       params[:key].blank? && params[:value].blank?
  47.     end
  48.     end
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement