Class: AmazonCatalogItemFlag

Inherits:
ApplicationRecord show all
Defined in:
app/models/amazon_catalog_item_flag.rb

Overview

== Schema Information

Table name: amazon_catalog_item_flags
Database name: primary

id :bigint not null, primary key
competitive_price :decimal(10, 2)
flag_type :string not null
issues :text
our_price_at_flagging :decimal(10, 2)
resolved_at :datetime
created_at :datetime not null
updated_at :datetime not null
catalog_item_id :bigint not null
competitor_seller_id :string
resolved_by_id :integer

Indexes

index_amazon_catalog_item_flags_on_catalog_item_id (catalog_item_id)
index_amazon_catalog_item_flags_on_flag_type (flag_type)
index_amazon_catalog_item_flags_on_resolved_at (resolved_at)

Foreign Keys

fk_rails_... (catalog_item_id => catalog_items.id)

Constant Summary collapse

FLAG_TYPES =

Recognised flag types.

{
  has_issues: 'has_issues',
  cannot_automatically_win_buy_box: 'cannot_automatically_win_buy_box',
  no_buyable_offers: 'no_buyable_offers',
  out_of_stock: 'out_of_stock',
  external_competitor: 'external_competitor',
  reprice_blocked_by_external: 'reprice_blocked_by_external'
}.freeze

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Belongs to collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.activeActiveRecord::Relation<AmazonCatalogItemFlag>

A relation of AmazonCatalogItemFlags that are active. Active Record Scope

Returns:

See Also:



46
# File 'app/models/amazon_catalog_item_flag.rb', line 46

scope :active, -> { where(resolved_at: nil) }

.by_catalogActiveRecord::Relation<AmazonCatalogItemFlag>

A relation of AmazonCatalogItemFlags that are by catalog. Active Record Scope

Returns:

See Also:



49
# File 'app/models/amazon_catalog_item_flag.rb', line 49

scope :by_catalog, ->(catalog_id) { joins(:catalog_item).where(catalog_items: { catalog_id: catalog_id }) }

.by_flag_typeActiveRecord::Relation<AmazonCatalogItemFlag>

A relation of AmazonCatalogItemFlags that are by flag type. Active Record Scope

Returns:

See Also:



48
# File 'app/models/amazon_catalog_item_flag.rb', line 48

scope :by_flag_type, ->(type) { where(flag_type: type) }

.resolvedActiveRecord::Relation<AmazonCatalogItemFlag>

A relation of AmazonCatalogItemFlags that are resolved. Active Record Scope

Returns:

See Also:



47
# File 'app/models/amazon_catalog_item_flag.rb', line 47

scope :resolved, -> { where.not(resolved_at: nil) }

Instance Method Details

#catalog_itemCatalogItem

Returns:



42
# File 'app/models/amazon_catalog_item_flag.rb', line 42

belongs_to :catalog_item

#competitor_nameObject

Get the competitor's display name (from database lookup or raw seller ID)



65
66
67
68
69
# File 'app/models/amazon_catalog_item_flag.rb', line 65

def competitor_name
  return nil if competitor_seller_id.blank?

  AmazonCompetitor.name_for(competitor_seller_id)
end

#competitor_storefront_urlObject

Get the Amazon storefront URL for the competitor



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/amazon_catalog_item_flag.rb', line 72

def competitor_storefront_url
  return nil if competitor_seller_id.blank?

  # Determine marketplace from catalog item
  marketplace_domain = case catalog_item&.catalog&.amazon_marketplace&.marketplace_identifier
                       when 'ATVPDKIKX0DER' then 'amazon.com'
                       when 'A2EUQ1WTGCTBG2' then 'amazon.ca'
                       when 'A1F83G8C2ARO7P' then 'amazon.co.uk'
                       when 'A1PA6795UKMFR9' then 'amazon.de'
                       when 'A13V1IB3VIYZZH' then 'amazon.fr'
                       when 'APJ6JRA9NG5V4' then 'amazon.it'
                       when 'A1RKKUPIHCS9HS' then 'amazon.es'
                       else 'amazon.com'
                       end

  "https://www.#{marketplace_domain}/sp?seller=#{competitor_seller_id}"
end

#known_competitor?Boolean

Check if this is a known/named competitor

Returns:

  • (Boolean)


91
92
93
94
95
# File 'app/models/amazon_catalog_item_flag.rb', line 91

def known_competitor?
  return false if competitor_seller_id.blank?

  AmazonCompetitor.named.exists?(seller_id: competitor_seller_id)
end

#resolve!(resolved_by: nil) ⇒ Boolean

Mark the flag resolved.

Parameters:

  • resolved_by (EmployeeRecord, nil) (defaults to: nil)

    the employee resolving the flag

Returns:

  • (Boolean)


60
61
62
# File 'app/models/amazon_catalog_item_flag.rb', line 60

def resolve!(resolved_by: nil)
  update!(resolved_at: Time.current, resolved_by: resolved_by)
end

#resolved?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/amazon_catalog_item_flag.rb', line 52

def resolved?
  resolved_at.present?
end

#resolved_byEmployeeRecord

Returns:



44
# File 'app/models/amazon_catalog_item_flag.rb', line 44

belongs_to :resolved_by, class_name: 'EmployeeRecord', optional: true