Class: PartnerService::LoyaltyChangeDetectorService

Inherits:
ApplicationService show all
Includes:
ElasticAPM::SpanHelpers
Defined in:
app/services/partner_service/loyalty_change_detector_service.rb

Overview

Service for detecting loyalty level changes and triggering batch sync operations to maintain data consistency across reservation summaries.

This service is designed to be called from loyalty transition callbacks to ensure all reservation summaries reflect the user's current loyalty status.

Examples:

Basic usage

PartnerService::LoyaltyChangeDetectorService.new(user_loyalty, transition).execute

Direct batch sync

PartnerService::LoyaltyChangeDetectorService.trigger_batch_sync(user_id, date_range_days)

Constant Summary collapse

MAX_BATCH_SIZE =

Maximum number of reservations to process in a single batch job This prevents memory issues and job timeouts for users with many reservations

100
DEFAULT_BATCH_DELAY =

Default delay between batch jobs (in seconds) to prevent overwhelming the queue

2

Instance Attribute Summary collapse

Attributes inherited from ApplicationService

#object

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute!

Constructor Details

#initialize(user_loyalty, transition = nil) ⇒ LoyaltyChangeDetectorService

Initialize the loyalty change detector service

Parameters:

  • user_loyalty (UserLoyalty)

    the user loyalty object

  • transition (Object, nil) (defaults to: nil)

    the loyalty transition object



33
34
35
36
37
38
# File 'app/services/partner_service/loyalty_change_detector_service.rb', line 33

def initialize(user_loyalty, transition = nil)
  super()
  @user_loyalty = user_loyalty
  @transition = transition
  @user = user_loyalty.user
end

Instance Attribute Details

#transitionUserLoyalty, ... (readonly)

Returns:

  • (UserLoyalty)

    the user loyalty object

  • (Object)

    the loyalty transition object

  • (User)

    the user associated with the loyalty



27
28
29
# File 'app/services/partner_service/loyalty_change_detector_service.rb', line 27

def transition
  @transition
end

#userUserLoyalty, ... (readonly)

Returns:

  • (UserLoyalty)

    the user loyalty object

  • (Object)

    the loyalty transition object

  • (User)

    the user associated with the loyalty



27
28
29
# File 'app/services/partner_service/loyalty_change_detector_service.rb', line 27

def user
  @user
end

#user_loyaltyUserLoyalty, ... (readonly)

Returns:

  • (UserLoyalty)

    the user loyalty object

  • (Object)

    the loyalty transition object

  • (User)

    the user associated with the loyalty



27
28
29
# File 'app/services/partner_service/loyalty_change_detector_service.rb', line 27

def user_loyalty
  @user_loyalty
end

Class Method Details

.trigger_batch_sync(user_id, date_range_days = nil) ⇒ void

This method returns an undefined value.

Class method for direct batch sync triggering

Parameters:

  • user_id (Integer)

    the user ID to trigger batch sync for

  • date_range_days (Integer, nil) (defaults to: nil)

    number of days to look back for reservations



55
56
57
58
59
# File 'app/services/partner_service/loyalty_change_detector_service.rb', line 55

def self.trigger_batch_sync(user_id, date_range_days = nil)
  user = User.find(user_id)
  new_service = new(user.user_loyalty)
  new_service.send(:trigger_batch_sync, date_range_days)
end

Instance Method Details

#executevoid

This method returns an undefined value.

Execute loyalty change detection and trigger batch sync if needed



43
# File 'app/services/partner_service/loyalty_change_detector_service.rb', line 43

span_method :execute