Class: Activity::ResourceList

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

Overview

Service object: resource list.

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

Attributes inherited from BaseService

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



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

def filters
  @filters
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#partyObject (readonly)

Returns the value of attribute party.



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

def party
  @party
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



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

def per_page
  @per_page
end

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

Class Method Details

.build_formatter(r) ⇒ BaseFormatter

Picks the formatter for a resource, falling back to BaseFormatter.

Parameters:

  • r (ActiveRecord::Base)

    resource record to format

Returns:



60
61
62
63
64
65
66
67
68
# File 'app/services/activity/resource_list.rb', line 60

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 = {}) ⇒ Hash

Returns grouped results with pagination info.

Parameters:

  • party (Party)

    the party whose resources are listed

  • options (Hash) (defaults to: {})

    listing options

Options Hash (options):

  • page (Integer)

    page number (1-based, defaults to 1)

  • per_page (Integer)

    resources per page (defaults to 20)

  • filters (Array<String>)

    resource types to include

  • query (String)

    search query matched against reference numbers

Returns:

  • (Hash)

    grouped results with pagination info



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
48
49
50
51
52
53
54
55
# File 'app/services/activity/resource_list.rb', line 14

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 if values.blank?

    option_group_text = resource_type.to_s.humanize
    date_range = values.filter_map(&:created_at)
    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