Sha256: bd954f0f1b3e37582dfdf21d0fe5de2deaadb619336b07474d276ad599c59262

Contents?: true

Size: 1.23 KB

Versions: 28

Compression:

Stored size: 1.23 KB

Contents

module Workarea
  module Reports
    class Customers
      include Report

      self.reporting_class = Metrics::User
      self.sort_fields = %w(revenue refund first_order_at last_order_at orders average_order_value cancellations)

      def aggregation
        [filter_orders, add_returning, filter_returning, project_fields].compact
      end

      def filter_orders
        { '$match' => { 'orders' => { '$gt' => 0 } } }
      end

      def add_returning
        {
          '$addFields' => {
            'returning' => { '$ne' => ['$first_order_at', '$last_order_at'] }
          }
        }
      end

      def filter_returning
        if params[:results_filter] == 'returning'
          { '$match' => { 'returning' => true } }
        elsif params[:results_filter] == 'one_time'
          { '$match' => { 'returning' => false } }
        end
      end

      def project_fields
        {
          '$project' => {
            '_id' => 1,
            'first_order_at' => 1,
            'last_order_at' => 1,
            'orders' => 1,
            'cancellations' => 1,
            'revenue' => 1,
            'refund' => 1,
            'average_order_value' => { '$divide': ['$revenue', '$orders'] }
          }
        }
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.6 app/queries/workarea/reports/customers.rb
workarea-core-3.5.5 app/queries/workarea/reports/customers.rb
workarea-core-3.5.4 app/queries/workarea/reports/customers.rb
workarea-core-3.5.3 app/queries/workarea/reports/customers.rb
workarea-core-3.5.2 app/queries/workarea/reports/customers.rb
workarea-core-3.5.1 app/queries/workarea/reports/customers.rb
workarea-core-3.5.0 app/queries/workarea/reports/customers.rb
workarea-core-3.5.0.beta.1 app/queries/workarea/reports/customers.rb