Class: QuickSearch::PhoneNumberQuickSearch

Inherits:
BaseQuickSearch
  • Object
show all
Defined in:
app/queries/quick_search/phone_number_quick_search.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handles_typesObject

Declare friendly types this searcher supports (used by BaseQuickSearch#handles_query_type?)



3
4
5
# File 'app/queries/quick_search/phone_number_quick_search.rb', line 3

def self.handles_types
  %i[contact customer employee]
end

Instance Method Details

#extract_term(query) ⇒ Object



7
8
9
# File 'app/queries/quick_search/phone_number_quick_search.rb', line 7

def extract_term(query)
  PhoneNumber.parse(query)&.to_s
end

#find(params, _max_records = nil, existing_results = []) ⇒ Object

Accept filter types from params passed through the chain



28
29
30
31
# File 'app/queries/quick_search/phone_number_quick_search.rb', line 28

def find(params, _max_records = nil, existing_results = [])
  @filter_types = params && params[:entity_types]
  super
end

#perform_find(term, _existing_results = []) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/queries/quick_search/phone_number_quick_search.rb', line 11

def perform_find(term, _existing_results = [])
  scope = ContactPoint.joins(:party).includes(party: :profile_image).where.not(Party[:inactive].eq(true)).find_phones(term)
  # If an entity type filter is present (e.g., contact, customer, employee), limit to those parties
  allowed_types = Array(@filter_types || []).map(&:to_s)
  if allowed_types.present?
    type_map = {
      'contact' => 'Contact',
      'customer' => 'Customer',
      'employee' => 'Employee'
    }
    party_types = allowed_types.map { |t| type_map[t] }.compact
    scope = scope.where(Party[:type].in(party_types)) if party_types.any?
  end
  scope
end