Class: Workarea::OrderCancellationMetrics

Inherits:
Object
  • Object
show all
Defined in:
app/queries/workarea/order_cancellation_metrics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, item_values: {}, shipping_value: nil, occured_at: Time.current) ⇒ OrderCancellationMetrics

Returns a new instance of OrderCancellationMetrics.



6
7
8
9
10
11
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 6

def initialize(order, item_values: {}, shipping_value: nil, occured_at: Time.current)
  @order = order
  @item_values = item_values
  @shipping_value = shipping_value
  @occured_at = occured_at&.to_time
end

Instance Attribute Details

#occured_atObject (readonly)

Returns the value of attribute occured_at.



3
4
5
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 3

def occured_at
  @occured_at
end

#orderObject (readonly)

Returns the value of attribute order.



3
4
5
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 3

def order
  @order
end

Instance Method Details

#categoriesObject



40
41
42
43
44
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 40

def categories
  @categories ||= items_by_via
    .select { |gid| gid.model_class.name == 'Workarea::Catalog::Category' }
    .transform_keys(&:model_id)
end

#countryObject



78
79
80
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 78

def country
  payment.address&.country&.alpha2
end


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 64

def menus
  @menus ||= categories.each_with_object({}) do |(category_id, data), memo|
    category = category_models[category_id]
    taxon_ids = [category&.taxon&.id, *category&.taxon&.parent_ids].compact

    Navigation::Menu.any_in(taxon_id: taxon_ids).pluck(:id).map(&:to_s).each do |menu_id|
      memo[menu_id] ||= Hash.new(0)
      memo[menu_id].merge!(data) { |_, total, current| total + current }
    end

    memo
  end.reject { |_, v| v.blank? }
end

#productsObject



33
34
35
36
37
38
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 33

def products
  @products ||= order.items
     .group_by(&:product_id)
     .transform_values { |items| calculate_based_on_items(items) }
     .reject { |_, v| v.blank? }
end

#sales_dataObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 13

def sales_data
  @sales_data ||= begin
    based_on_items = calculate_based_on_items(items)
    refund = based_on_items[:refund] + shipping_refund_total
    {
      cancellations: 1,
      units_canceled: based_on_items[:units_canceled],
      refund: refund,
      revenue: refund
    }
  end
end

#searchesObject



46
47
48
49
50
51
52
53
54
55
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 46

def searches
  @searches ||= items_by_via
    .select { |gid| gid.model_class.name == 'Workarea::Navigation::SearchResults' }
    .each_with_object({}) do |(gid, data), memo|
      query_id = gid.find.query_string.id

      memo[query_id] ||= Hash.new(0)
      memo[query_id].merge!(data) { |_, total, current| total + current }
    end
end

#segmentsObject



86
87
88
89
90
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 86

def segments
  @segments ||= order.segment_ids.each_with_object({}) do |segment_id, data|
    data[segment_id] = sales_data
  end
end

#shipping_refund_totalObject



82
83
84
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 82

def shipping_refund_total
  (@shipping_value || shipping_total || 0) * -1
end

#skusObject



57
58
59
60
61
62
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 57

def skus
  @skus ||= order.items
    .group_by(&:sku)
    .transform_values { |items| calculate_based_on_items(items) }
    .reject { |_, v| v.blank? }
end

#user_dataObject



26
27
28
29
30
31
# File 'app/queries/workarea/order_cancellation_metrics.rb', line 26

def user_data
  {
    email: email,
    refund: sales_data[:refund]
  }
end