Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module V1::Admin
- class CustomersController < ApplicationController
- include Rails::Pagination
- def index
- customers = if check_params
- current_user.customers.order("id ASC")
- else
- filtered_customers
- end
- customers = search_for_customers(customers) if params[:search_value].present?
- paginate json: customers, per_page: 25
- end
- private
- def search_for_customers(customers)
- if is_number?(ConvertFullWidth.call(params[:search_value]))
- 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')
- else
- 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')
- end
- end
- def filtered_customers
- keys = params[:key].split('_')
- value = params[:value].split('_')
- keys.each_with_index do |key, index|
- if key == "paymentStatusFilter"
- if value[index] == 'Failed'
- Customer.failed_subscriptions_customers
- elsif value[index] == 'Active'
- Customer.active_subscriptions_customers
- end
- elsif key == 'orderCountFilter'
- Customer.customers_with_max_orders
- end
- end
- end
- def is_number? (string)
- true if Float(string) rescue false
- end
- def check_params
- params[:key].blank? && params[:value].blank?
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement