Class: Tablecheck::Availabilities::Timetable

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer, ElasticAPM::SpanHelpers, Tablecheck::ApiHelpers
Defined in:
app/services/tablecheck/availabilities/timetable.rb

Overview

Timetable service for fetching Tablecheck timeslot availabilities.

Splits requested dates into blocks of up to 7 days (Tablecheck API limit), fetches each block in parallel, and aggregates results.

Error handling and logging are performed using APMErrorHandler and BUSINESS_LOGGER. Supplier validation is handled by the calling service, not in this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods included from Tablecheck::ApiHelpers

#api_call, #channel, #find_tablecheck_vendor_application, #get_purpose, #headers, #parse_phone_number, #parse_user_or_guest_full_name, #parsed_vendor_name, #reservation_start_at, #send_error_availability_notification_to_staff

Constructor Details

#initialize(restaurant, dates, party_size) ⇒ Timetable

Initialize Timetable service.

Parameters:

  • restaurant (Restaurant)

    Restaurant object (must be validated by caller)

  • dates (Array<String>)

    Dates to check availability for (must be 'YYYY-MM-DD' strings)

  • party_size (Integer)

    Party size for availability (optional, defaults to restaurant min_seat)



22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/tablecheck/availabilities/timetable.rb', line 22

def initialize(restaurant, dates, party_size)
  @restaurant = restaurant
  @restaurant_id = restaurant.id
  @dates = dates
  @party_size = party_size
  @shop_id = restaurant&.tablecheck_restaurant&.shop_id.to_s
  @fetcher_service = VendorsService::InventorySync::InventoryFetcherService.new(
    restaurant: restaurant,
    supplier: :tablecheck,
  )
end

Instance Attribute Details

#datesObject (readonly)

Returns the value of attribute dates.



15
16
17
# File 'app/services/tablecheck/availabilities/timetable.rb', line 15

def dates
  @dates
end

#fetcher_serviceObject (readonly)

Returns the value of attribute fetcher_service.



15
16
17
# File 'app/services/tablecheck/availabilities/timetable.rb', line 15

def fetcher_service
  @fetcher_service
end

#party_sizeObject (readonly)

Returns the value of attribute party_size.



15
16
17
# File 'app/services/tablecheck/availabilities/timetable.rb', line 15

def party_size
  @party_size
end

#restaurantObject (readonly)

Returns the value of attribute restaurant.



15
16
17
# File 'app/services/tablecheck/availabilities/timetable.rb', line 15

def restaurant
  @restaurant
end

#restaurant_idObject (readonly)

Returns the value of attribute restaurant_id.



15
16
17
# File 'app/services/tablecheck/availabilities/timetable.rb', line 15

def restaurant_id
  @restaurant_id
end

#shop_idObject (readonly)

Returns the value of attribute shop_id.



15
16
17
# File 'app/services/tablecheck/availabilities/timetable.rb', line 15

def shop_id
  @shop_id
end

Instance Method Details

#fetch_timeslots_with_availabilityArray<Hash>

Fetch available timeslots for all requested dates. Uses the shared fetcher service for common HTTP handling logic.

Returns:

  • (Array<Hash>)

    Array of hashes: { datetime: <Time>, quantity_available: <Integer> }

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
# File 'app/services/tablecheck/availabilities/timetable.rb', line 38

def fetch_timeslots_with_availability
  raise ArgumentError, "shop_id is blank for restaurant_id=#{restaurant_id}" if shop_id.blank?
  raise ArgumentError, 'party_size must be present and > 0' if @party_size.nil? || @party_size <= 0

  BUSINESS_LOGGER.set_business_context({ restaurant_id: restaurant_id })

  fetcher_service.execute_parallel_requests(dates) do |block, hydra, timetables|
    queue_request(block, hydra, timetables)
  end
end