Class: Activity::ResourceList

Inherits:
BaseService show all
Defined in:
app/services/activity/resource_list.rb

Defined Under Namespace

Classes: BaseFormatter, CreditMemoFormatter, CreditMemoRetriever, InvoiceFormatter, InvoiceRetriever, OpportunityFormatter, OpportunityRetriever, OrderFormatter, OrderRetriever, PurchaseOrderFormatter, PurchaseOrderRetriever, QuoteFormatter, QuoteRetriever, ResourceRetriever, RmaFormatter, RmaRetriever, RoomConfigurationFormatter, RoomConfigurationRetriever, SupportCaseFormatter, SupportCaseRetriever

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



4
5
6
# File 'app/services/activity/resource_list.rb', line 4

def filters
  @filters
end

#pageObject (readonly)

Returns the value of attribute page.



4
5
6
# File 'app/services/activity/resource_list.rb', line 4

def page
  @page
end

#partyObject (readonly)

Returns the value of attribute party.



4
5
6
# File 'app/services/activity/resource_list.rb', line 4

def party
  @party
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



4
5
6
# File 'app/services/activity/resource_list.rb', line 4

def per_page
  @per_page
end

#queryObject (readonly)

Returns the value of attribute query.



4
5
6
# File 'app/services/activity/resource_list.rb', line 4

def query
  @query
end

Class Method Details

.build_formatter(r) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/services/activity/resource_list.rb', line 49

def self.build_formatter(r)
  formatter_class = begin
    "Activity::ResourceList::#{r.class.name}Formatter".constantize
  rescue StandardError
    nil
  end
  formatter = formatter_class.new(r) if formatter_class
  formatter ||= Activity::ResourceList::BaseFormatter.new(r)
end

Instance Method Details

#process(party, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/activity/resource_list.rb', line 6

def process(party, options = {})
  @party = party
  @page = options[:page].to_i
  @page = 1 if @page < 1
  @per_page = (options[:per_page] || 20).to_i
  @per_page = 20 if @per_page < 1
  @filters = options[:filters]
  @query = options[:query]

  # supplier don't have the rest
  @filters = %w[purchase_orders] if party.is_a?(Supplier)

  resources = %i[opportunities orders quotes room_configurations support_cases invoices rmas credit_memos purchase_orders]
  resources &= @filters.map(&:to_sym) if @filters.present?

  results = []
  total_count = 0
  total_displayed = 0
  resources.each do |resource_type|
    retriever = ResourceRetriever.build(resource_type, party: party, query: query, page: page, per_page: per_page)
    values = retriever.resources
    next unless values.present?

    option_group_text = resource_type.to_s.humanize
    date_range = values.map(&:created_at).compact
    range_start = date_range.min&.to_fs(:crm_date_only)
    range_end = date_range.max&.to_fs(:crm_date_only)
    option_group_text << " from #{range_start} until #{range_end}"
    # Get the total count from the retriever before limit/offset was applied
    total_count += retriever.total_count
    total_displayed += values.size
    results << {
      text: option_group_text,
      children: values.map do |r|
        formatter = self.class.build_formatter(r)
        { text: formatter.display, id: formatter.identifier }
      end
    }
  end
  more = total_displayed < total_count
  { results: results, pagination: { more: more } }
end