Class: BackfillSpendingTierDiscountWorker

Inherits:
Object
  • Object
show all
Includes:
ElasticAPM::SpanHelpers, Sidekiq::Worker
Defined in:
app/workers/backfill_spending_tier_discount_worker.rb

Overview

Worker to backfill spending tier discount on package metadata for a single reservation This worker is called by the BackfillSpendingTierDiscountOnPackageMetadata data migration

Instance Method Summary collapse

Instance Method Details

#perform(reservation_id) ⇒ Object

Process a single reservation to add spending tier discount to package metadata

Parameters:

  • reservation_id (Integer)

    The ID of the reservation to process



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/workers/backfill_spending_tier_discount_worker.rb', line 16

def perform(reservation_id)
  reservation = Reservation.find_by(id: reservation_id)
  return unless reservation

  property = reservation.property
  return if property&.package.blank?

   = (property.package)
  return unless 

  # Get spending tier discount from selected_packages
  spending_tier_discount = get_spending_tier_discount(reservation)
  return unless spending_tier_discount

  # Update package metadata with the discount
  updated = (, spending_tier_discount)
  return unless updated

  # Save updated metadata
  property.package = 
  property.save!

  BUSINESS_LOGGER.info(
    'Successfully backfilled spending tier discount for reservation',
    reservation_id: reservation_id,
    discount_cents: (spending_tier_discount['price'] || 0).abs,
  )
rescue StandardError => e
  BUSINESS_LOGGER.error(
    'Failed to backfill spending tier discount for reservation',
    reservation_id: reservation_id,
    error: e.message,
    backtrace: e.backtrace.first(10),
  )
  raise # Re-raise to trigger Sidekiq retry
end