Sha256: 08664998e0c708410e4ce97fe35d117a2f194ed89986de60df4a7816d5c86fd9

Contents?: true

Size: 1.13 KB

Versions: 34

Compression:

Stored size: 1.13 KB

Contents

module Workarea
  module Reports
    class Customers
      include Report

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

      def aggregation
        result = [add_returning]
        result << filter_to_returning if params[:results_filter] == 'returning'
        result << filter_to_one_time if params[:results_filter] == 'one_time'
        result + [project_fields]
      end

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

      def filter_to_returning
        { '$match' => { 'returning' => true } }
      end

      def filter_to_one_time
        { '$match' => { 'returning' => false } }
      end

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

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/queries/workarea/reports/customers.rb
workarea-core-3.4.44 app/queries/workarea/reports/customers.rb
workarea-core-3.4.43 app/queries/workarea/reports/customers.rb
workarea-core-3.4.42 app/queries/workarea/reports/customers.rb
workarea-core-3.4.41 app/queries/workarea/reports/customers.rb
workarea-core-3.4.40 app/queries/workarea/reports/customers.rb
workarea-core-3.4.39 app/queries/workarea/reports/customers.rb
workarea-core-3.4.38 app/queries/workarea/reports/customers.rb
workarea-core-3.4.37 app/queries/workarea/reports/customers.rb
workarea-core-3.4.36 app/queries/workarea/reports/customers.rb
workarea-core-3.4.35 app/queries/workarea/reports/customers.rb
workarea-core-3.4.34 app/queries/workarea/reports/customers.rb
workarea-core-3.4.33 app/queries/workarea/reports/customers.rb
workarea-core-3.4.32 app/queries/workarea/reports/customers.rb
workarea-core-3.4.31 app/queries/workarea/reports/customers.rb
workarea-core-3.4.30 app/queries/workarea/reports/customers.rb
workarea-core-3.4.29 app/queries/workarea/reports/customers.rb
workarea-core-3.4.28 app/queries/workarea/reports/customers.rb
workarea-core-3.4.27 app/queries/workarea/reports/customers.rb
workarea-core-3.4.26 app/queries/workarea/reports/customers.rb