Class: SpreeCmCommissioner::ApiCaches::Invalidate

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/api_caches/invalidate.rb

Constant Summary collapse

SUPPORTED_MODELS =
{
  'SpreeCmCommissioner::HomepageSection' => SpreeCmCommissioner::HomepageSection,
  'SpreeCmCommissioner::HomepageBackground' => SpreeCmCommissioner::HomepageBackground,
  'Spree::Menu' => Spree::Menu,
  'Spree::Taxon' => Spree::Taxon,
  'Spree::Product' => Spree::Product
}.freeze
SINGLE_API_PATTERN =
{
  SpreeCmCommissioner::HomepageSection => [
    '/api/v2/storefront/homepage/*/homepage_sections',
    '/api/v2/tenant/homepage/*/homepage_sections'
  ],
  SpreeCmCommissioner::HomepageBackground => ['/api/v2/storefront/homepage/*/homepage_background'],
  Spree::Menu => ['/api/v2/storefront/menus/:id'],
  Spree::Taxon => ['/api/v2/storefront/taxons/:id'],
  Spree::Product => [
    '/api/v2/tenant/products/:id',
    '/api/v2/tenant/products/:slug',
    '/api/v2/storefront/products/:id',
    '/api/v2/storefront/products/:slug'
  ]
}.freeze
ALL_API_PATTERNS =
{
  SpreeCmCommissioner::HomepageSection => [
    '/api/v2/storefront/homepage/*/homepage_sections',
    '/api/v2/tenant/homepage/*/homepage_sections'
  ],
  SpreeCmCommissioner::HomepageBackground => ['/api/v2/storefront/homepage/*/homepage_background'],
  Spree::Menu => ['/api/v2/storefront/menus/*'],
  Spree::Taxon => ['/api/v2/storefront/taxons/*'],
  Spree::Product => [
    '/api/v2/tenant/products/*',
    '/api/v2/storefront/products/*'
  ]
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(model:, id: nil) ⇒ Object

eg. ‘Spree::Product’, 123 or ‘Spree::Product’, nil



45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/spree_cm_commissioner/api_caches/invalidate.rb', line 45

def call(model:, id: nil)
  model_class = SUPPORTED_MODELS[model]

  return failure(nil, 'Model is not supported') if model_class.nil?

  if id.present?
    invalidate_single_record(model_class: model_class, id: id)
  else
    invalidate_all_records(model_class: model_class)
  end
end

#invalidate_all_records(model_class:) ⇒ Object

eg. ‘Spree::Product’ => [‘/api/v2/tenant/products/*’, ‘/api/v2/storefront/products/*’]



79
80
81
82
83
84
85
86
87
# File 'app/services/spree_cm_commissioner/api_caches/invalidate.rb', line 79

def invalidate_all_records(model_class:)
  model_class.find_each do |record|
    refresh_record_cache_key(record)
  end

  enqueue_invalidate(ALL_API_PATTERNS[model_class])

  success(api_patterns: ALL_API_PATTERNS[model_class])
end

#invalidate_single_record(model_class:, id:) ⇒ Object

eg. ‘Spree::Product’, 123 => [‘/api/v2/tenant/products/123’, ‘/api/v2/tenant/products/slug-of-product’, …]



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/services/spree_cm_commissioner/api_caches/invalidate.rb', line 58

def invalidate_single_record(model_class:, id:)
  return failure(nil, "#{model_class.name} ID is required") if id.blank?

  record = model_class.find_by(id: id)
  return failure(nil, "#{model_class.name} #{id} record not found") if record.nil?

  refresh_record_cache_key(record)

  api_patterns = SINGLE_API_PATTERN[record.class].map do |pattern|
    pattern.gsub(/:(\w+)/) do
      key = Regexp.last_match(1)
      record.public_send(key).to_s
    end
  end

  enqueue_invalidate(api_patterns)

  success(record: record, api_patterns: api_patterns)
end