Class: ProductCatalogSearch

Inherits:
Search show all
Defined in:
app/models/product_catalog_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

Constants inherited from Search

Search::DISTANCE_SEARCH_KEYS

Instance Attribute Summary

Attributes inherited from Search

#composite_columns, #pagy_count, #pagy_limit, #pagy_page, #sort_column1, #sort_column2, #sort_column3, #sql_select_columns

Class Method Summary collapse

Instance Method Summary collapse

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, custom_criteria_keys, 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_table, mass_actions_for_select, #mass_export, maximum_unpersisted_queries, 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_columns_for_select, #sort_columns_to_hash, #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::EventPublishable

#publish_event

Class Method Details

.main_resource_classObject



85
86
87
# File 'app/models/product_catalog_search.rb', line 85

def self.main_resource_class
  'CatalogItem'
end

Instance Method Details

#apply_custom_criteria(results) ⇒ Object



80
81
82
83
# File 'app/models/product_catalog_search.rb', line 80

def apply_custom_criteria(results)
  prepend_output_column(:third_party_part_number) if query_params[:third_party_part_number_cont].present?
  results
end

#mass_amazon_operation(params, cur_user) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/product_catalog_search.rb', line 147

def mass_amazon_operation(params, cur_user)
  return { status: :error, message: 'Access Denied' } unless cur_user..can?(:update, CatalogItem)

  ru = instantiate_resource_updater
  catalog_item_ids = []
  operation = params[:operation].presence
  attributes = params[:attributes]&.filter_map(&:presence) || []
  attribute_action = params[:attribute_action].presence || 'replace'
  attribute_actions = (attributes.index_with { attribute_action } if attributes.present?)
  fbm_vs_fba = params[:fbm_vs_fba]
  ru.perform do |ci|
    catalog_item_ids << ci.id if ci.amazon_catalog_item? && (ci.active? || ci.pending_onboarding? || ci.require_vendor_update? || ci.is_discontinued?)
  end
  if operation.present? && catalog_item_ids.present?
    job_params = { catalog_item_ids:, operation:, attribute_actions:, fbm_vs_fba:, redirect_to: "/searches/#{id}" }

    job_id = AmazonItemOperationWorker.perform_async(job_params.stringify_keys)
    job_id ||= SidekiqUniqueJobsJidLookup.active_jid_for_args(AmazonItemOperationWorker, job_params.stringify_keys)
    { status: :ok, job_id: job_id }
  else
    { status: :error, message: 'No amazon catalog items found or operation missing, items must be active, pending_onboarding, or require_vendor_update and belong to an amazon catalog' }
  end
end

#mass_assign_to_onboarding_packet(controller) ⇒ Object



109
110
111
# File 'app/models/product_catalog_search.rb', line 109

def mass_assign_to_onboarding_packet(controller)
  controller.redirect_to controller.view_context.assign_exported_catalog_item_packets_path(search_id: id)
end

#mass_assign_to_stores(params, cur_user = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'app/models/product_catalog_search.rb', line 113

def mass_assign_to_stores(params, cur_user = nil)
  jid = MassSearch::ProductCatalogAssignToStoresWorker.perform_async(
    'search_id'     => id,
    'action_params' => { 'store_ids' => params[:store_ids] },
    'user_id'       => cur_user&.id,
    'locale'        => I18n.locale.to_s
  )
  { status: :ok, job_id: jid }
end

#mass_compare_specs(context) ⇒ Object



196
197
198
199
200
201
# File 'app/models/product_catalog_search.rb', line 196

def mass_compare_specs(context)
  item_ids = perform_selected(selected_columns: %i[item_id]).pluck(:item_id)
  return { status: :error, message: 'No items selected' } if item_ids.blank?

  context.redirect_to context.reports_items_spec_matrix_path(command: { item_ids: })
end

#mass_export_for_amalytics(_params, cur_user) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/product_catalog_search.rb', line 171

def mass_export_for_amalytics(_params, cur_user)
  file_name = "export_#{id}.xlsx"
  directory_path = Rails.root.join(Rails.application.config.x.temp_storage_path.to_s, 'users', cur_user ? cur_user.id.to_s : '')
  FileUtils.mkdir_p(directory_path)
  output_file_path = Rails.root.join(directory_path, file_name)
  catalog_item_ids = perform_selected(selected_columns: %i[catalog_item_id]).pluck(:catalog_item_id)
  catalog_items = CatalogItem.where(id: catalog_item_ids)
  Amazon::ExportAmalyticsSnapshot.new.process(catalog_items:, output_file_path:)

  { status: :ok, result_file: file_name, result_file_path: output_file_path }
end

#mass_refresh_item_specs(context) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/product_catalog_search.rb', line 183

def mass_refresh_item_specs(context)
  return { status: :error, message: 'Access Denied' } unless context..can?(:update, CatalogItem)

  item_ids = perform_selected(selected_columns: %i[item_id]).pluck(:item_id).uniq
  if item_ids.present?
    item_ids.each { |iid| ItemAttributeWorker.perform_async(iid) }
    context.flash[:info] = "#{item_ids.size} queued for spec refresh"
  else
    context.flash[:warning] = 'No item(s) selected'
  end
  context.redirect_to(context.search_path(self))
end

#mass_sale_price(params, cur_user) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/product_catalog_search.rb', line 135

def mass_sale_price(params, cur_user)
  return { status: :error, message: 'Access Denied' } unless cur_user..can?(:update, CatalogItem)

  jid = MassSearch::ProductCatalogSalePriceWorker.perform_async(
    'search_id'     => id,
    'action_params' => { 'catalog_item' => (params[:catalog_item] || {}).to_h },
    'user_id'       => cur_user&.id,
    'locale'        => I18n.locale.to_s
  )
  { status: :ok, job_id: jid }
end

#mass_trigger_event(params, cur_user = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'app/models/product_catalog_search.rb', line 99

def mass_trigger_event(params, cur_user = nil)
  jid = ::SearchResourceUpdateWorker.perform_async(
    'search_id'     => id,
    'action_params' => { 'resource_params' => (params[:resource] || {}).to_h },
    'user_id'       => cur_user&.id,
    'locale'        => I18n.locale.to_s
  )
  { status: :ok, job_id: jid }
end

#mass_update(params, cur_user) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/product_catalog_search.rb', line 123

def mass_update(params, cur_user)
  return { status: :error, message: 'Access Denied' } unless cur_user..can?(:update, CatalogItem)

  jid = ::SearchResourceUpdateWorker.perform_async(
    'search_id'     => id,
    'action_params' => { 'resource_params' => (params[:resource] || {}).to_h },
    'user_id'       => cur_user&.id,
    'locale'        => I18n.locale.to_s
  )
  { status: :ok, job_id: jid }
end

#normalize_for_mass_update(resource, resource_params) ⇒ Object

Clears negative min_stock_to_report sentinel values before saving.



94
95
96
97
# File 'app/models/product_catalog_search.rb', line 94

def normalize_for_mass_update(resource, resource_params)
  resource.min_stock_to_report = nil if resource_params[:min_stock_to_report].to_i.negative?
  true
end