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.27 app/queries/workarea/reports/customers.rb
workarea-core-3.5.26 app/queries/workarea/reports/customers.rb
workarea-core-3.5.25 app/queries/workarea/reports/customers.rb
workarea-core-3.5.23 app/queries/workarea/reports/customers.rb
workarea-core-3.5.22 app/queries/workarea/reports/customers.rb
workarea-core-3.5.21 app/queries/workarea/reports/customers.rb
workarea-core-3.5.20 app/queries/workarea/reports/customers.rb
workarea-core-3.5.19 app/queries/workarea/reports/customers.rb
workarea-core-3.5.18 app/queries/workarea/reports/customers.rb
workarea-core-3.5.17 app/queries/workarea/reports/customers.rb
workarea-core-3.5.16 app/queries/workarea/reports/customers.rb
workarea-core-3.5.15 app/queries/workarea/reports/customers.rb
workarea-core-3.5.14 app/queries/workarea/reports/customers.rb
workarea-core-3.5.13 app/queries/workarea/reports/customers.rb
workarea-core-3.5.12 app/queries/workarea/reports/customers.rb
workarea-core-3.5.11 app/queries/workarea/reports/customers.rb
workarea-core-3.5.10 app/queries/workarea/reports/customers.rb
workarea-core-3.5.9 app/queries/workarea/reports/customers.rb
workarea-core-3.5.8 app/queries/workarea/reports/customers.rb
workarea-core-3.5.7 app/queries/workarea/reports/customers.rb