Class: OpportunitySearch
- Inherits:
-
Search
- Object
- ActiveRecord::Base
- ApplicationRecord
- Search
- OpportunitySearch
- Includes:
- Models::RepSearchable
- Defined in:
- app/models/opportunity_search.rb
Overview
== Schema Information
Table name: searches
Database name: primary
id :integer not null, primary key
name :string(255)
persist :boolean default(FALSE)
pinned :boolean default(FALSE), not null
query_params :jsonb
result_set_size :integer default(0)
selected_columns :string is an Array
set_limit :integer default(25)
sort_column :string(255)
sort_columns :string default([]), not null, is an Array
sort_direction :string(255) default("ASC")
type :string(255) not null
created_at :datetime
updated_at :datetime
employee_id :integer
Indexes
employee_id_pinned (employee_id,pinned)
employee_id_type (employee_id,type)
Constant Summary collapse
- OUTLET_PURCHASE_FILTERS =
Presence filter values for the
outlet_purchasequery param. { 'any_link' => 'Has an outlet purchase', 'verified' => 'Has a VERIFIED outlet purchase', 'none' => 'Has no outlet purchase' }.freeze
Constants inherited from Search
Constants included from Models::Schedulable
Models::Schedulable::SIMPLE_FORM_OPTIONS
Instance Attribute Summary
Attributes inherited from Search
#composite_columns, #pagy_count, #pagy_limit, #pagy_page, #sql_select_columns
Class Method Summary collapse
-
.custom_criteria_keys ⇒ Array<String>
Non-Ransack keys handled by #apply_outlet_purchase_criteria.
Instance Method Summary collapse
- #apply_custom_criteria(results) ⇒ ActiveRecord::Relation
-
#apply_outlet_purchase_criteria(results) ⇒ ActiveRecord::Relation
Filter on whether the customer bought through a retail outlet instead of direct, optionally narrowed to particular channels.
- #mass_assign_reps(params, cur_user = nil) ⇒ Hash
- #mass_assign_source(params, cur_user = nil) ⇒ Hash
- #mass_googlemap_direct(_params, _cur_user_id = 1) ⇒ Hash
- #mass_schedule_activity(params, cur_user = nil) ⇒ Hash
Methods inherited from Search
#all_composite_columns, allowed_role_ids, #append_custom_column, #append_ouput_column, #append_to_sql_select_columns, #applicable_sort_terms, #apply_array_match, #apply_criteria, #apply_customer_cross_reference, #apply_distance_search, #apply_select, #apply_sort, #available_events, #available_output_columns, available_output_columns, base_search_class_name, #cleanup_search_results, #composite_column, composite_column, database_columns, default_columns, default_sort, #discard_excess, #effective_name, #effective_short_name, #employee, #enqueue_navbar_pinned_refresh, #fast_count, favorites, friendly_column_name, #get_pinned_results, #get_sort_term, global_favorite, has_role_for_search?, #human_query_params, instantiate_query_template, #instantiate_resource_updater, #limit_options, main_resource_class, main_resource_table, mass_actions_for_select, #mass_export, maximum_unpersisted_queries, #normalize_for_mass_update, options, options_classes, #output_columns_for_select, #pagy_from_search, #perform, #perform_selected, #pinned_query, #prepend_custom_column, #prepend_output_column, query_favorites_templates, #ransack_probe_params, recent_searches_for_employee_id, #record_list, #refresh_pinned_results, #remove_other_pins, search_name, #search_name, #search_results, search_type, search_type_humanized, select_sort_columns, #select_sort_columns, #select_statement, #set_defaults, #set_sort_term, #sort_column1, #sort_column1=, #sort_column2, #sort_column2=, #sort_column3, #sort_column3=, #sort_columns_for_select, #sort_columns_to_hash, #sortable_column?, #target_geocoding, unpersisted_queries_quote_reached?, #unrecord_list, #validated_selected_columns, view_resource_class, view_resource_klass, #view_resource_klass, view_resource_table, visible?, with_search_results
Methods inherited from ApplicationRecord
ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation
Methods included from Models::Schedulable
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
Class Method Details
.custom_criteria_keys ⇒ Array<String>
Non-Ransack keys handled by #apply_outlet_purchase_criteria.
57 58 59 |
# File 'app/models/opportunity_search.rb', line 57 def self.custom_criteria_keys %w[outlet_purchase outlet_catalog_id] end |
Instance Method Details
#apply_custom_criteria(results) ⇒ ActiveRecord::Relation
63 64 65 66 67 |
# File 'app/models/opportunity_search.rb', line 63 def apply_custom_criteria(results) results = apply_distance_search(results, lat_column: 'latitude', lng_column: 'longitude') results = apply_customer_cross_reference(results, :customer_id) apply_outlet_purchase_criteria(results) end |
#apply_outlet_purchase_criteria(results) ⇒ ActiveRecord::Relation
Filter on whether the customer bought through a retail outlet instead of
direct, optionally narrowed to particular channels.
Naming a channel implies presence, so a channel with none is a
contradiction — the channel wins and none is ignored rather than silently
returning nothing.
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/opportunity_search.rb', line 80 def apply_outlet_purchase_criteria(results) # Ignore anything not in the vocabulary rather than silently treating an # unknown value as `none` and returning the inverse of what was asked. filter = query_params[:outlet_purchase].presence filter = nil unless OUTLET_PURCHASE_FILTERS.key?(filter) catalog_ids = Array(query_params[:outlet_catalog_id]).compact_blank return results if filter.blank? && catalog_ids.empty? exists = outlet_purchase_exists(catalog_ids, verified_only: filter == 'verified') negate = filter == 'none' && catalog_ids.empty? results.where(negate ? exists.not : exists) end |
#mass_assign_reps(params, cur_user = nil) ⇒ Hash
148 149 150 151 152 153 154 155 156 |
# File 'app/models/opportunity_search.rb', line 148 def mass_assign_reps(params, cur_user = nil) jid = MassSearch::OpportunityAssignRepsWorker.perform_async( 'search_id' => id, 'action_params' => { 'resource_params' => (params[:resource] || {}).to_h, 'activity' => (params[:activity] || {}).to_h }, 'user_id' => cur_user&.id, 'locale' => I18n.locale.to_s ) { status: :ok, job_id: jid } end |
#mass_assign_source(params, cur_user = nil) ⇒ Hash
135 136 137 138 139 140 141 142 143 |
# File 'app/models/opportunity_search.rb', line 135 def mass_assign_source(params, cur_user = nil) jid = ::SearchResourceUpdateWorker.perform_async( 'search_id' => id, 'action_params' => { 'resource_params' => { 'source_id' => params[:source_id] } }, 'user_id' => cur_user&.id, 'locale' => I18n.locale.to_s ) { status: :ok, job_id: jid } end |
#mass_googlemap_direct(_params, _cur_user_id = 1) ⇒ Hash
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/opportunity_search.rb', line 97 def mass_googlemap_direct(_params, _cur_user_id = 1) result_data = [] search_results.each do |sr| unless sr.resource.geocoded? sr.resource.geocode sr.resource.save if sr.resource.geocoded? end next unless sr.resource.geocoded? result_data << { latitude: sr.resource.latitude, longitude: sr.resource.longitude, title: sr.resource.name, resource: sr.resource, json: { party_id: sr.resource.customer_id, opportunity_id: sr.resource.id } } end { status: :ok, result_action: 'googlemap_direct', result_data: } end |
#mass_schedule_activity(params, cur_user = nil) ⇒ Hash
122 123 124 125 126 127 128 129 130 |
# File 'app/models/opportunity_search.rb', line 122 def mass_schedule_activity(params, cur_user = nil) jid = MassSearch::OpportunityScheduleActivityWorker.perform_async( 'search_id' => id, 'action_params' => { 'activity' => (params[:activity] || {}).to_h, 'campaign_id' => params[:campaign_id] }, 'user_id' => cur_user&.id, 'locale' => I18n.locale.to_s ) { status: :ok, job_id: jid } end |