Class: EventDrivenServices::HhSearch::Producers::Restaurants::AvailabilityProducer

Inherits:
Object
  • Object
show all
Defined in:
app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb

Overview

AvailabilityProducer is responsible for producing and sending events related to restaurant availability.

Attributes:

Constant Summary collapse

VALID_EVENT_TYPES =
[
  EventDrivenClient::Constants::UPDATE_EVENT,
  EventDrivenClient::Constants::INDEX_EVENT,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(restaurant_id, event_type, changed_attributes = {}, options = {}) ⇒ AvailabilityProducer

Returns a new instance of AvailabilityProducer.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 24

def initialize(restaurant_id, event_type, changed_attributes = {}, options = {})
  @restaurant = Restaurant.find_by(id: restaurant_id) if restaurant_id.present?
  @event_type = validate_event_type(event_type)
  @topic = EventDrivenClient::Constants::RESTAURANTS_AVAILABILITY_TOPIC

  if changed_attributes.is_a?(Hash)
    reservation_id = changed_attributes[:reservation_id]

    @changed_attributes = if reservation_id.present?
                            @reservation = Reservation.find_by(id: reservation_id)
                            {} # Reset @changed_attributes if reservation_id is present
                          else
                            changed_attributes
                          end
  else
    @changed_attributes = changed_attributes # If not a Hash (Array), keep the original value
  end

  @kind = options[:kind]
  @source = options[:source]
  @batch_id = options[:batch_id]
end

Instance Attribute Details

#batch_idObject (readonly)

Returns the value of attribute batch_id.



17
18
19
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 17

def batch_id
  @batch_id
end

#changed_attributesObject (readonly)

Returns the value of attribute changed_attributes.



17
18
19
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 17

def changed_attributes
  @changed_attributes
end

#event_typeObject (readonly)

Returns the value of attribute event_type.



17
18
19
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 17

def event_type
  @event_type
end

#kindObject (readonly)

Returns the value of attribute kind.



17
18
19
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 17

def kind
  @kind
end

#reservationObject (readonly)

Returns the value of attribute reservation.



17
18
19
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 17

def reservation
  @reservation
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



17
18
19
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 17

def restaurant
  @restaurant
end

#sourceObject (readonly)

Returns the value of attribute source.



17
18
19
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 17

def source
  @source
end

#topicObject (readonly)

Returns the value of attribute topic.



17
18
19
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 17

def topic
  @topic
end

Instance Method Details

#reindex_all(batch_id: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 72

def reindex_all(batch_id: nil)
  # NOTE: Added explicit ordering to ensure deterministic batching and prevent duplicate events
  restaurants = Restaurant.bookable.not_expired.order(:id)

  # skip if there are no restaurants
  return if restaurants.blank?

  total_items = restaurants.size
  restaurants.each_with_index do |restaurant_param, batch_index|
    args = {
      topic: EventDrivenClient::Constants::RESTAURANTS_AVAILABILITY_TOPIC,
      event_type: EventDrivenClient::Constants::INDEX_EVENT,
      object_id: restaurant_param.id,
      changed_attributes: { batch_number: batch_index + 1, total_items: total_items },
      options: { source: 'AvailabilityProducer', batch_id: batch_id },
    }
    EventDrivenWorkers::HhSearch::InventoryProducerWorker.perform_conditionally(args, async: true)
  end
end

#send_eventObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/services/event_driven_services/hh_search/producers/restaurants/availability_producer.rb', line 47

def send_event
  return if restaurant.blank?
  return unless restaurant.bookable_and_not_expired?

  payload = case event_type
            when EventDrivenClient::Constants::INDEX_EVENT
              generate_index_payload(restaurant)
            when EventDrivenClient::Constants::UPDATE_EVENT
              generate_update_payload(restaurant)
            else
              raise NotImplementedError, "Invalid event type: #{event_type}"
            end

  payload_hash = JSON.parse(payload).with_indifferent_access

  # Skip sending event if availability is empty and event type is 'update'
  # We allow empty availability for index event
  return if payload_hash[:payload][:availability].blank? && event_type == EventDrivenClient::Constants::UPDATE_EVENT

  client.send_event_async(payload)
rescue StandardError => e
  APMErrorHandler.report("Failed to send event to topic #{topic}", error: e)
  raise e
end