Class: Inventory::InvCheckerHungryHubService
- Inherits:
-
InvCheckerBaseService
- Object
- InvCheckerBaseService
- Inventory::InvCheckerHungryHubService
- Extended by:
- ElasticAPM::SpanHelpers
- Includes:
- ElasticAPM::SpanHelpers, Constants, BulkData, FindAvailableDates, Helper
- Defined in:
- app/services/inventory/inv_checker_hungry_hub_service.rb,
app/services/inventory/inv_checker_hungry_hub_service/helper.rb,
app/services/inventory/inv_checker_hungry_hub_service/bulk_data.rb,
app/services/inventory/inv_checker_hungry_hub_service/data_store.rb,
app/services/inventory/inv_checker_hungry_hub_service/find_available_dates.rb
Overview
Check based on available inventory, seat left, and packages This is a checker for restaurant which has packages
So this class will build following tree
18:45 = { open: boolean, seat_left: integer, slugs: ["687-PP-002", "687-PP-003"] }
/
/
Restaurant ID (685) -- 19:00 = { open: boolean, seat_left: integer, slugs: ["687-PP-001", "687-PP-004"] }
\
\
19:15 = { open: boolean, seat_left: integer, slugs: ["687-PP-002", "687-PP-003"] }
when using restaurant_package_ids data, this class is buggy see #find_available_start_times_v4 method, the cache contains `selected_slug` when the restaurant_package_ids has more than a single value, then selected_slug is nil if one of the package is not available, this method returns true, which should be false
Direct Known Subclasses
InvCheckerBistrochatService, InvCheckerMyMenuService, InvCheckerSevenRoomsService, InvCheckerTablecheckService, InvCheckerWeeloyService
Defined Under Namespace
Modules: BulkData, FindAvailableDates, Helper Classes: DataStore
Constant Summary collapse
- DEFAULT_DURATION =
120 is default duration, which is 2 hours
120
Instance Attribute Summary collapse
-
#buffer_time_in_minute ⇒ Object
writeonly
Sets the attribute buffer_time_in_minute.
-
#for_delivery ⇒ Object
Returns the value of attribute for_delivery.
-
#for_dine_in ⇒ Object
Returns the value of attribute for_dine_in.
-
#is_order_now ⇒ Object
writeonly
jika is_order_now nya adalah true, maka waktu yg di return akan dikurangi dengan 30 menit, agar user bisa memesan lebih awal dan user bisa mengambil orderan di waktu restorannya tutup toko jika waktu end time tidak dikurangi dengan cooking time, maka restaurant harus memasak diwaktu mereka tutup restaurant oleh karena itu, kita kurangi waktu end time, sehingga masakan ready ketika restoran pas jam tutup toko.
-
#restaurant_id ⇒ Object
Returns the value of attribute restaurant_id.
-
#restaurant_package_ids ⇒ Object
Returns the value of attribute restaurant_package_ids.
-
#skip_last_minute_error ⇒ Object
Returns the value of attribute skip_last_minute_error.
-
#skip_past_time_error ⇒ Object
Returns the value of attribute skip_past_time_error.
-
#time_zone ⇒ Object
Returns the value of attribute time_zone.
Class Method Summary collapse
-
.get_update_level(reservation_level, restaurant_level, restaurant_package_level) ⇒ Object
Helper method to determine update level for consistent logging.
-
.group_datetimes(datetimes_param) ⇒ Object
Group datetimes by date with sorted start times.
-
.process_inventory_checker(inv_checker:, restaurant:, restaurant_package:, restaurant_packages:, dates:, datetimes:, default_start_times:, reservation_level:, restaurant_level:, restaurant_package_level:, reset_mode:) ⇒ Object
Process inventory checker based on update level.
-
.process_reservation_level(inv_checker:, restaurant:, datetimes:, default_start_times:) ⇒ Object
Process inventory checker at reservation level.
-
.process_restaurant_level(inv_checker:, dates:, default_start_times:, restaurant_packages:, reset_mode:) ⇒ Object
Process inventory checker at restaurant level.
-
.process_restaurant_package_level(inv_checker:, restaurant_package:, dates:, default_start_times:, reset_mode:) ⇒ Object
Process inventory checker at restaurant package level.
-
.set_by_reservation_level(inv_checker, date, start_times) ⇒ Object
Set reservation level cache values.
-
.set_by_restaurant_level(inv_checker, date, start_times) ⇒ Object
Set restaurant level cache values.
-
.set_by_restaurant_package_level(inv_checker, date, start_times, slug) ⇒ Object
Set restaurant package level cache values.
-
.update_cache_value(restaurant_id:, restaurant_package_id: nil, restaurant_package_ids: nil, reservation_level: false, restaurant_level: false, restaurant_package_level: false, dates: [], datetimes: [], reset_mode: false, reservation_id: nil) ⇒ void
Updates cache values for restaurant inventory checking with proper date handling and Redis caching.
-
.warm_up_cache(restaurant_id, dates) ⇒ void
Warm up cache for restaurant inventory checkers This method will pre-load the cache for all restaurant packages and their available dates It will iterate through each date and start time, and pre-load the seat lefts, durations, and available dates.
Instance Method Summary collapse
- #add_on_bookable?(date:, start_time:, r_add_on_id:) ⇒ Boolean
- #available_add_ons(date:, start_time:, r_add_on_ids: []) ⇒ Object
-
#available_packages(date:, start_time:, adult:, kids:) ⇒ Array
Get available restaurant packages.
-
#bookable?(date:, start_time:, adult:, kids:) ⇒ Boolean
Check availability on restaurant level if a restaurant has any offers, then it checks inventory and packages availability, otherwise it will check inventory availability only.
-
#bookable_without_adult?(date:, start_time:) ⇒ Boolean
if restaurant has offers, then we need to check on seat lefts on package level otherwise, use Inventory model to check seat lefts.
-
#bump_restaurant_cache_key_version ⇒ String
We update the cache key when: * a restaurant is updated * restaurant inventories have been created/updated * package inventories have been created/updated.
-
#cache_key ⇒ String
Retrieves the current cache key version for this restaurant's inventory.
- #check_unavailability_reason(date:, start_time:) ⇒ Object
-
#delete_old_cache_keys ⇒ void
Deletes all cached data associated with old cache key versions This helps prevent Redis memory bloat by removing stale cache entries.
-
#dine_in_cache_keys ⇒ Array<String>
Retrieves all cache keys for dine-in service type.
-
#error_message ⇒ Object
ActiveModel::Errors instance.
-
#find_available_dates(adult:, start_date:, end_date:, kids:) ⇒ Object
Find available dates for date range.
-
#find_available_single_date(adult:, date:, kids:) ⇒ Object
Find available single date.
- #find_available_start_times(adult:, kids:, date:) ⇒ Object
- #find_available_start_times_v4(adult:, kids:, date:) ⇒ Object
-
#get_inv_by_date(date, slug = nil) ⇒ Object
get_inv_by_date(Date.today, 'AY-001') => “seat_left”=>50, “slugs”=>#<Set: {“222-AY-5199977”>, “duration”=>15 minutes}}.
- #get_inv_by_date_time(date, start_time, slug = nil) ⇒ Object
-
#initialize(restaurant_id, time_zone) ⇒ InvCheckerHungryHubService
constructor
A new instance of InvCheckerHungryHubService.
-
#inventory_bookable?(date:, start_time:, adult:, kids:, skip_seat_left_check: false) ⇒ Boolean
the concept is same as #package_bookable? method below, but this one is supposed to be for checking a restaurant that does not have any packages.
-
#open?(date, start_time, slug = nil) ⇒ Boolean
if slug is exist, then it will check in Package level otherwise it will check on restaurant level.
-
#package_bookable?(date:, start_time:, adult:, kids:, slug:, adult_from: nil, skip_seat_left_check: false) ⇒ Boolean
Check availability in package level with support for reservation updates This method supports checking inventory for both new bookings and reservation updates.
-
#packages_bookable?(date:, start_times:, adult:, kids:, slugs:) ⇒ Array<Hash>
Check availability of given packages by checking inventory and package availability.
-
#recommend_date(start_date, adult, slug_param = nil) ⇒ Object
recommend any packages on >= start_date.
-
#recommend_package(date:, start_time:, adult:, kids:, slug:) ⇒ Object
recommend available package.
-
#restaurant_packages(filter_by_restaurant_package_ids: true) ⇒ Object
set this parameter to be false to return all active restaurant packages.
- #seat_left(date, start_time, slug = nil) ⇒ Object
- #set_delivery_inventory? ⇒ Boolean
-
#slugs(date, start_time) ⇒ Object
no need to filter by slug.
-
#take_away_cache_keys ⇒ Array<String>
Retrieves all cache keys for take-away (delivery) service type.
Methods included from Helper
#cache_key_hash_by_restaurant, #cache_key_hash_by_restaurant_package, #clear_restaurant_cache_by_dates, #clear_restaurant_package_cache, #fetch, #fetch_from_local_cache, #inv_redis_pool, #inv_redis_pool_replica, #inventories_start_times, #invert_slugs_by_start_times, #namespace_default, #namespace_raw
Methods included from BulkData
#find_durations, #res_durations_is_enough?, #seat_lefts, #set_durations, #set_res_durations_is_enough, #set_seat_lefts, #set_slugs_by_start_times, #slugs_by_start_times
Methods included from FindAvailableDates
#find_available_date, #find_available_dates_without_start_time, #set_find_available_date
Constructor Details
#initialize(restaurant_id, time_zone) ⇒ InvCheckerHungryHubService
Returns a new instance of InvCheckerHungryHubService.
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 404 def initialize(restaurant_id, time_zone) super self.skip_past_time_error = false self.skip_last_minute_error = false @errors = ActiveModel::Errors.new(self) @buffer_time_in_minute = 0 @for_dine_in = true @for_delivery = false # default is dine in @restaurant_package_ids = nil @is_order_now = false @data_store = Inventory::InvCheckerHungryHubService::DataStore.new restaurant_id, time_zone @local_cache = {} # HH_LOGGER.set_request_context({ # restaurant_id: restaurant_id, # time_zone: time_zone, # for_dine_in: @for_dine_in, # for_delivery: @for_delivery, # restaurant_package_ids: @restaurant_package_ids, # }) end |
Instance Attribute Details
#buffer_time_in_minute=(value) ⇒ Object
Sets the attribute buffer_time_in_minute
392 393 394 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 392 def buffer_time_in_minute=(value) @buffer_time_in_minute = value end |
#for_delivery ⇒ Object
Returns the value of attribute for_delivery.
389 390 391 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 389 def for_delivery @for_delivery end |
#for_dine_in ⇒ Object
Returns the value of attribute for_dine_in.
389 390 391 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 389 def for_dine_in @for_dine_in end |
#is_order_now=(value) ⇒ Object
jika is_order_now nya adalah true, maka waktu yg di return akan dikurangi dengan 30 menit, agar user bisa memesan lebih awal dan user bisa mengambil orderan di waktu restorannya tutup toko jika waktu end time tidak dikurangi dengan cooking time, maka restaurant harus memasak diwaktu mereka tutup restaurant oleh karena itu, kita kurangi waktu end time, sehingga masakan ready ketika restoran pas jam tutup toko
400 401 402 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 400 def is_order_now=(value) @is_order_now = value end |
#restaurant_id ⇒ Object
Returns the value of attribute restaurant_id.
389 390 391 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 389 def restaurant_id @restaurant_id end |
#restaurant_package_ids ⇒ Object
Returns the value of attribute restaurant_package_ids.
389 390 391 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 389 def restaurant_package_ids @restaurant_package_ids end |
#skip_last_minute_error ⇒ Object
Returns the value of attribute skip_last_minute_error.
389 390 391 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 389 def skip_last_minute_error @skip_last_minute_error end |
#skip_past_time_error ⇒ Object
Returns the value of attribute skip_past_time_error.
389 390 391 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 389 def skip_past_time_error @skip_past_time_error end |
#time_zone ⇒ Object
Returns the value of attribute time_zone.
389 390 391 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 389 def time_zone @time_zone end |
Class Method Details
.get_update_level(reservation_level, restaurant_level, restaurant_package_level) ⇒ Object
Helper method to determine update level for consistent logging
171 172 173 174 175 176 177 178 179 180 181 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 171 def get_update_level(reservation_level, restaurant_level, restaurant_package_level) if reservation_level 'reservation' elsif restaurant_level 'restaurant' elsif restaurant_package_level 'restaurant_package' else 'unknown' end end |
.group_datetimes(datetimes_param) ⇒ Object
Group datetimes by date with sorted start times
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 325 def group_datetimes(datetimes_param) ElasticAPM.with_span('Group datetimes', 'app', subtype: 'inventory') do dates = {} datetimes_param.each do |datetime| date = datetime.to_date dates[date] = [] unless dates.key?(date) start_time = datetime.strftime('%H:%M') dates[date] << start_time end # Sort the start times dates.each do |date, start_times| dates[date] = start_times.sort end dates end end |
.process_inventory_checker(inv_checker:, restaurant:, restaurant_package:, restaurant_packages:, dates:, datetimes:, default_start_times:, reservation_level:, restaurant_level:, restaurant_package_level:, reset_mode:) ⇒ Object
Process inventory checker based on update level
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 186 def process_inventory_checker(inv_checker:, restaurant:, restaurant_package:, restaurant_packages:, dates:, datetimes:, default_start_times:, reservation_level:, restaurant_level:, restaurant_package_level:, reset_mode:) ElasticAPM.with_span('Process inventory checker', 'app.cache', subtype: 'inventory') do |_span| ElasticAPM.set_label(:checker_type, inv_checker.class.name) if restaurant_level process_restaurant_level( inv_checker: inv_checker, dates: dates, default_start_times: default_start_times, restaurant_packages: restaurant_packages, reset_mode: reset_mode, ) elsif restaurant_package_level if restaurant_package.present? process_restaurant_package_level( inv_checker: inv_checker, restaurant_package: restaurant_package, dates: dates, default_start_times: default_start_times, reset_mode: reset_mode, ) elsif restaurant_packages.present? restaurant_packages.each do |package| process_restaurant_package_level( inv_checker: inv_checker, restaurant_package: package, dates: dates, default_start_times: default_start_times, reset_mode: reset_mode, ) end else raise NotImplementedError, 'Restaurant package must be provided for package level updates' end elsif reservation_level process_reservation_level( inv_checker: inv_checker, restaurant: restaurant, datetimes: datetimes, default_start_times: default_start_times, ) else error_msg = "Invalid parameters: restaurant_level: #{restaurant_level}, restaurant_package_level: #{restaurant_package_level}, reservation_level: #{reservation_level}" # HH_LOGGER.error(error_msg) raise NotImplementedError, error_msg end end end |
.process_reservation_level(inv_checker:, restaurant:, datetimes:, default_start_times:) ⇒ Object
Process inventory checker at reservation level
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 293 def process_reservation_level(inv_checker:, restaurant:, datetimes:, default_start_times:) ElasticAPM.with_span('Process reservation level', 'app.cache', subtype: 'inventory') do restaurant_packages = restaurant.restaurant_packages.valid_to_have_agendas if datetimes.blank? raise NotImplementedError, 'Datetimes are required for reservation level cache updates' end processed_datetimes = datetimes.map(&:to_datetime) grouped_dates = group_datetimes(processed_datetimes) if restaurant_packages.present? restaurant_packages.each do |package| grouped_dates.each do |date, start_times| set_by_restaurant_package_level(inv_checker, date, start_times, package.slug) end end grouped_dates.each do |date, start_times| set_by_reservation_level(inv_checker, date, start_times) end else grouped_dates.each do |date, start_times| set_by_reservation_level(inv_checker, date, start_times) end end end end |
.process_restaurant_level(inv_checker:, dates:, default_start_times:, restaurant_packages:, reset_mode:) ⇒ Object
Process inventory checker at restaurant level
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 242 def process_restaurant_level(inv_checker:, dates:, default_start_times:, restaurant_packages:, reset_mode:) ElasticAPM.with_span('Process restaurant level', 'app.cache', subtype: 'inventory') do if dates.present? inv_checker.clear_restaurant_cache_by_dates(dates) if reset_mode dates.each do |date| set_by_restaurant_level(inv_checker, date, default_start_times) end restaurant_packages.each do |package| dates.each do |date| set_by_restaurant_package_level(inv_checker, date, default_start_times, package.slug) end end else raise NotImplementedError, 'Dates are required for restaurant level cache updates' end end end |
.process_restaurant_package_level(inv_checker:, restaurant_package:, dates:, default_start_times:, reset_mode:) ⇒ Object
Process inventory checker at restaurant package level
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 265 def process_restaurant_package_level(inv_checker:, restaurant_package:, dates:, default_start_times:, reset_mode:) ElasticAPM.with_span('Process restaurant package level', 'app.cache', subtype: 'inventory') do if dates.present? dates.each do |date| inv_checker.clear_restaurant_package_cache(restaurant_package, date) set_by_restaurant_package_level(inv_checker, date, default_start_times, restaurant_package.slug) set_by_restaurant_level(inv_checker, date, default_start_times) end elsif reset_mode # Use current date as start date if package start date is in the past start_date = [restaurant_package.start_date, Date.current].max dates = (start_date..restaurant_package.end_date).to_a dates.each do |date| inv_checker.clear_restaurant_package_cache(restaurant_package, date) set_by_restaurant_package_level(inv_checker, date, default_start_times, restaurant_package.slug) set_by_restaurant_level(inv_checker, date, default_start_times) end else raise NotImplementedError, 'Either dates or reset_mode must be provided for package level updates' end end end |
.set_by_reservation_level(inv_checker, date, start_times) ⇒ Object
Set reservation level cache values
379 380 381 382 383 384 385 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 379 def set_by_reservation_level(inv_checker, date, start_times) ElasticAPM.with_span('Set reservation cache', 'app.cache', subtype: 'inventory') do inv_checker.set_durations(date, start_times) inv_checker.set_res_durations_is_enough(date, start_times) inv_checker.set_seat_lefts(date, start_times) end end |
.set_by_restaurant_level(inv_checker, date, start_times) ⇒ Object
Set restaurant level cache values
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 348 def set_by_restaurant_level(inv_checker, date, start_times) ElasticAPM.with_span('Set restaurant cache', 'app.cache', subtype: 'inventory') do # Durations data is the core data, so it must be set first inv_checker.set_durations(date, start_times) # Then we can define the reservation durations availability inv_checker.set_res_durations_is_enough(date, start_times) # Then we can define the seat lefts inv_checker.set_seat_lefts(date, start_times) # Slugs by Start Times doesn't have any dependency to other data inv_checker.set_slugs_by_start_times(date, start_times) end end |
.set_by_restaurant_package_level(inv_checker, date, start_times, slug) ⇒ Object
Set restaurant package level cache values
367 368 369 370 371 372 373 374 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 367 def set_by_restaurant_package_level(inv_checker, date, start_times, slug) ElasticAPM.with_span('Set package cache', 'app.cache', subtype: 'inventory') do inv_checker.set_durations(date, start_times, slug) inv_checker.set_res_durations_is_enough(date, start_times, slug) inv_checker.set_seat_lefts(date, start_times, slug) inv_checker.set_find_available_date(date, slug) end end |
.update_cache_value(restaurant_id:, restaurant_package_id: nil, restaurant_package_ids: nil, reservation_level: false, restaurant_level: false, restaurant_package_level: false, dates: [], datetimes: [], reset_mode: false, reservation_id: nil) ⇒ void
This method returns an undefined value.
Updates cache values for restaurant inventory checking with proper date handling and Redis caching
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 106 def update_cache_value(restaurant_id:, restaurant_package_id: nil, restaurant_package_ids: nil, reservation_level: false, restaurant_level: false, restaurant_package_level: false, dates: [], datetimes: [], reset_mode: false, reservation_id: nil) ElasticAPM.with_span('InventoryChecker.update_cache_value', 'app.cache', subtype: 'inventory', action: 'update') do |_span| ElasticAPM.set_label(:update_level, get_update_level(reservation_level, restaurant_level, restaurant_package_level)) # Load required data with span tracking restaurant, restaurant_packages, processed_dates, default_start_times = ElasticAPM.with_span('Load inventory data', 'db') do processed_dates = dates.map(&:to_date).uniq # Load restaurant and package data restaurant = Restaurant.find(restaurant_id) rp = restaurant_package_id.present? ? HhPackage::RestaurantPackage.find(restaurant_package_id) : nil rps = if restaurant_package_ids.present? HhPackage::RestaurantPackage.where(id: restaurant_package_ids) else restaurant.restaurant_packages end default_times = Reservation::PERIODS_WITHOUT_00_AM [restaurant, rps, processed_dates, default_times] end # Process inventory checkers with span tracking ElasticAPM.with_span('Process inventory checkers', 'app.cache') do restaurant.create_inv_checkers.each do |inv_checker| process_inventory_checker( inv_checker: inv_checker, restaurant: restaurant, restaurant_package: restaurant_package_id.present? ? HhPackage::RestaurantPackage.find(restaurant_package_id) : nil, restaurant_packages: restaurant_packages, dates: processed_dates, datetimes: datetimes, default_start_times: default_start_times, reservation_level: reservation_level, restaurant_level: restaurant_level, restaurant_package_level: restaurant_package_level, reset_mode: reset_mode, ) end end # HH_LOGGER.info('Successfully updated inventory cache', { # restaurant_id: restaurant_id, # package_id: restaurant_package_id, # level: get_update_level(reservation_level, restaurant_level, restaurant_package_level), # date_count: processed_dates.size, # }) end end |
.warm_up_cache(restaurant_id, dates) ⇒ void
This method should be called after the restaurant and its packages have been created to ensure that the cache is populated with the correct data.
This method is designed to be called in a background job or during application startup to avoid blocking the main thread and to ensure that the cache is ready for use when needed.
This method returns an undefined value.
Warm up cache for restaurant inventory checkers This method will pre-load the cache for all restaurant packages and their available dates It will iterate through each date and start time, and pre-load the seat lefts, durations, and available dates
This method is useful for pre-loading the cache to improve performance when checking availability for a large number of dates and start times.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 58 def warm_up_cache(restaurant_id, dates) restaurant = Restaurant.find(restaurant_id) start_times = Reservation::PERIODS_WITHOUT_00_AM restaurant.create_inv_checkers.each do |inv_checker| restaurant_packages = restaurant.restaurant_packages&.valid_to_have_agendas if restaurant_packages.present? slugs = restaurant_packages&.map(&:slug) dates.each do |date| # dates_batch.each do |date| slugs.each do |slug| inv_checker.seat_lefts(date, start_times, slug) inv_checker.find_durations(date, start_times, slug) inv_checker.find_available_date(date, slug) end inv_checker.slugs_by_start_times(date, start_times) inv_checker.res_durations_is_enough?(date, start_times) # end end else dates.each do |date| # dates_batch.each do |date| inv_checker.seat_lefts(date, start_times) inv_checker.find_durations(date, start_times) inv_checker.slugs_by_start_times(date, start_times) inv_checker.res_durations_is_enough?(date, start_times) # end end end end end |
Instance Method Details
#add_on_bookable?(date:, start_time:, r_add_on_id:) ⇒ Boolean
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1540 def add_on_bookable?(date:, start_time:, r_add_on_id:) return false unless valid_date_time_params?(date, start_time, r_add_on_id: r_add_on_id) r_add_on = find_restaurant_add_on_by_id(r_add_on_id) return false if r_add_on.blank? return false unless r_add_on.active_and_not_expired? # check add-on availability based on agenda unless r_add_on.available_at?(date, start_time, restaurant.time_zone) errors.add(:base, I18n.t('inventory.add_on_not_available', add_on_name: r_add_on.fetch_addon.name)) return false end true end |
#available_add_ons(date:, start_time:, r_add_on_ids: []) ⇒ Object
1530 1531 1532 1533 1534 1535 1536 1537 1538 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1530 def available_add_ons(date:, start_time:, r_add_on_ids: []) return [] if date.blank? || start_time.blank? # Empty array means check for all restaurant add-ons in the restaurant r_add_on_ids = r_add_on_ids.presence || restaurant.restaurant_add_ons.pluck(:id) r_add_on_ids.select do |r_add_on_id| add_on_bookable?(date: date, start_time: start_time, r_add_on_id: r_add_on_id) end end |
#available_packages(date:, start_time:, adult:, kids:) ⇒ Array
Get available restaurant packages
845 846 847 848 849 850 851 852 853 854 855 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 845 def available_packages(date:, start_time:, adult:, kids:) start_times_by_slugs = slugs_by_start_times(date, [start_time]) if start_times_by_slugs.include?(start_time) && res_durations_is_enough?( date, start_times_by_slugs.keys, )[start_time] return start_times_by_slugs[start_time].to_a end [] end |
#bookable?(date:, start_time:, adult:, kids:) ⇒ Boolean
Check availability on restaurant level if a restaurant has any offers, then it checks inventory and packages availability, otherwise it will check inventory availability only
440 441 442 443 444 445 446 447 448 449 450 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 440 def bookable?(date:, start_time:, adult:, kids:) if restaurant.any_offers? result = slugs(date, start_time)&.find do |slug| package_bookable?(date: date, start_time: start_time, adult: adult.to_i, kids: kids.to_i, slug: slug) end errors.clear result.present? else inventory_bookable?(date: date, start_time: start_time, adult: adult.to_i, kids: kids.to_i) end end |
#bookable_without_adult?(date:, start_time:) ⇒ Boolean
if restaurant has offers, then we need to check on seat lefts on package level otherwise, use Inventory model to check seat lefts
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 807 def bookable_without_adult?(date:, start_time:) slugs = slugs_by_start_times(date, [start_time]).values.map(&:to_a).flatten.uniq if restaurant.any_offers? && slugs.blank? errors.add(:base, 'Package is not available at selected time') return false end unless res_duration_is_enough?(date, start_time) errors.add(:base, I18n.t('inventory.not_available')) return false end seat_left = 0 if restaurant.any_offers? seat_lefts_data = slugs.map do |slug| seat_left(date, start_time, slug) end seat_left = seat_lefts_data.max || 0 else seat_left = seat_left(date, start_time) end if seat_left <= 0 errors.add(:base, I18n.t('inventory.not_available')) return false end true end |
#bump_restaurant_cache_key_version ⇒ String
We update the cache key when:
-
a restaurant is updated
-
restaurant inventories have been created/updated
-
package inventories have been created/updated
The redis_key is a Sorted Set (ZSET) type in Redis. When we bump the cache key, we add the new cache key to the sorted set with the current timestamp as score. This ensures uniqueness and provides chronological ordering. The latest cache key (with highest score) is used as the main cache key.
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1455 def bump_restaurant_cache_key_version redis_key = "#{namespace_raw}:cache_keys" current_time = Time.now.to_i new_version = nil inv_redis_pool.with do |redis| # Get the highest scored (latest) cache key last_versions = redis.zrevrange(redis_key, 0, 0, with_scores: true) # If no keys exist yet, start with VERSION_0 last_version_in_redis = last_versions.empty? ? 'VERSION_0' : last_versions[0][0] # Increment the version number bump_version = last_version_in_redis.split('_').last.to_i + 1 new_version = "VERSION_#{bump_version}" # Add the new version to the sorted set with current timestamp as score redis.zadd(redis_key, current_time, new_version) @cache_key = new_version end @cache_key end |
#cache_key ⇒ String
Retrieves the current cache key version for this restaurant's inventory. This method fetches the latest version from the sorted set in Redis. If no cache key exists yet, it creates a new one by calling bump_restaurant_cache_key_version.
The cache key is used to namespace all cached inventory data for this restaurant, ensuring that when inventory data changes, old cached data is effectively invalidated.
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1429 def cache_key @cache_key ||= begin key = "#{namespace_raw}:cache_keys" value = nil inv_redis_pool.with do |redis| # Get the highest scored (latest) version from the sorted set latest_versions = redis.zrevrange(key, 0, 0) value = latest_versions.first.presence || bump_restaurant_cache_key_version end value end end |
#check_unavailability_reason(date:, start_time:) ⇒ Object
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 940 def check_unavailability_reason(date:, start_time:) # check inventory record is exist? if restaurant.inventories.where(date: date, start_time: start_time).blank? 'There is no allotment at selected time!' # check is it blocked? elsif blocked?(date: date, start_time: start_time) 'You blocked all the seats at selected time!' elsif !bookable?(date: date, start_time: start_time, adult: 1, kids: 0) 'Your seats are full for selected time!' elsif !valid_date_time_params?(date, start_time) elsif available_add_ons(date: date, start_time: start_time).blank? 'There is no available add-ons at selected time!' else 'Unfortunately seat is unavailable with unknown reason' end end |
#delete_old_cache_keys ⇒ void
This method returns an undefined value.
Deletes all cached data associated with old cache key versions This helps prevent Redis memory bloat by removing stale cache entries
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1506 def delete_old_cache_keys Karafka.producer.produce_async( topic: EVENTS::INVENTORY::CACHE::TOPIC, payload: { event_type: EVENTS::INVENTORY::CACHE::TYPES::CLEAR_OLD_CACHE, # @param restaurant_id [Integer] ID of the restaurant whose cache needs clearing # @param service_type [String] The service type: "dine_in" or "take_away" # @param version_number [String, nil] Optional specific version to target (e.g., 'VERSION_123') restaurant_id: restaurant_id, service_type: for_dine_in ? 'dine_in' : 'take_away', version_number: cache_key, # Debug attributes trigger_source: self.class.name, trigger_method: 'delete_old_cache_keys', trigger_context: { service_type: for_dine_in ? 'dine_in' : 'take_away', cache_cleanup: true, version_number: cache_key, }, caller: Rails.backtrace_cleaner.clean(caller), }.to_json, ) end |
#dine_in_cache_keys ⇒ Array<String>
Retrieves all cache keys for dine-in service type
1483 1484 1485 1486 1487 1488 1489 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1483 def dine_in_cache_keys redis_key = "#{namespace_raw}:cache_keys" keys = [] inv_redis_pool.with do |redis| keys = redis.zrange(redis_key, 0, -1) end end |
#error_message ⇒ Object
Returns ActiveModel::Errors instance.
428 429 430 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 428 def errors..uniq.to_sentence end |
#find_available_dates(adult:, start_date:, end_date:, kids:) ⇒ Object
Find available dates for date range
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 964 def find_available_dates(adult:, start_date:, end_date:, kids:) start_date = Date.parse(start_date) end_date = Date.parse(end_date) today = Time.now_in_tz(time_zone).to_date dates = (start_date..end_date).to_a.select { |date| date >= today } local_restaurant_packages = restaurant_packages(filter_by_restaurant_package_ids: true) has_packages = local_restaurant_packages.count.positive? expiry_dates = [restaurant.expiry_date] if has_packages expiry_dates = expiry_dates + local_restaurant_packages.map { |r| r.end_date } end expiry_date = expiry_dates.min data = [] dates.each do |date| result = { 'date' => date, 'seat_left' => 0, 'max_seat' => 0, 'min_seat' => 0, 'booked_seat' => 0, 'is_available' => false, 'quantity_available' => 0, } if date > expiry_date data << result next end _inventories = inventories(date) if _inventories.present? start_times = periods(date) seat_left_data = seat_lefts(date, start_times) if start_times.present? && seat_left_data.present? available_inventories = _inventories.select { |inv| start_times.include?(inv['start_time_format']) } available_inventories.each { |inv| inv['seat_left'] = seat_left_data[inv['start_time_format']] } invs = available_inventories.sort_by { |inv| -inv['seat_left'] } result['booked_seat'] = invs.sum { |inv| inv['party_size'] } result['quantity_available'] = invs.map { |inv| inv['quantity_available'] }.max || 0 is_available = false invs.each do |inv| break if is_available if inv['seat_left'] > 0 if has_packages local_restaurant_packages.each do |restaurant_package| if res_duration_is_enough?(date, inv['start_time_format'], restaurant_package.slug) is_available = true break end end elsif res_duration_is_enough?(date, inv['start_time_format']) is_available = true break end end end result['is_available'] = is_available result['seat_left'] = if is_available invs.max_by { |inv| inv['seat_left'] }['seat_left'] else 0 end end if has_packages && result['is_available'] max_seat_package = local_restaurant_packages.max_by { |r| r.package.decide_max_seat }&.package min_seat_package = local_restaurant_packages.min_by { |r| r.package.decide_min_seat }&.package result['max_seat'] = max_seat_package.package_attr.max_seat.presence || 0 if max_seat_package.present? result['min_seat'] = min_seat_package.package_attr.min_seat.presence || 0 if min_seat_package.present? end end data << result end data.map do |item| seat_left = item['seat_left'] min_seat = item['min_seat'] max_seat = item['max_seat'] availability = if item['is_available'] if seat_left >= adult.to_i if min_seat.positive? adult.to_i.between?(min_seat, max_seat) else true end else false end else false end { 'date' => item['date'], 'availability' => availability, 'seat_left' => seat_left, 'min_seat' => min_seat, 'max_seat' => max_seat, 'booked_seat' => item['booked_seat'], 'quantity_available' => item['quantity_available'], } end end |
#find_available_single_date(adult:, date:, kids:) ⇒ Object
Find available single date
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1085 def find_available_single_date(adult:, date:, kids:) date = Date.parse(date) party_size = adult.to_i data = fetch(['find_available_single_date', 'by_date', 'by_slug', date, selected_slug], {}, selected_slug) do local_restaurant_packages = restaurant_packages(filter_by_restaurant_package_ids: true) has_packages = local_restaurant_packages.count.positive? _inventories = inventories(date) result = { 'date' => date, 'seat_left' => 0, 'max_seat' => 0, 'min_seat' => 0, 'booked_seat' => 0, } inventories_date = if _inventories.present? _inventories.select do |inv| if inv.is_a?(Date) inv == date else inv['date'] == date end end else [] end is_available = if inventories_date.present? start_times = periods(date, selected_slug) inventories_date_local = if inventories_date.respond_to?(:values) inventories_date.values.flatten else inventories_date.flatten end invs = inventories_date_local.select do |inv| start_times.include?(inv['start_time_format']) end result['booked_seat'] = invs.map { |inv| inv['party_size'] }.sum seat_left_data = seat_lefts(date, start_times, selected_slug) package_duration_is_enough = {} local_restaurant_packages.each do |r| package_duration_is_enough[r.id] = res_durations_is_enough?(date, start_times, r.slug) end duration_data = res_durations_is_enough?(date, start_times, selected_slug) invs.sort_by do |inv| seat_left_data[inv['start_time_format']] end.reverse.find do |inv| if less_than_min_booking_time?(date, inv['start_time_format']) false else seat_left = seat_left_data[inv['start_time_format']] result['seat_left'] = seat_left if result['seat_left'] < 1 false elsif has_packages filtered_packages = local_restaurant_packages.select do |r| package_duration_is_enough[r.id][inv['start_time_format']] end max_seat_package = filtered_packages&.max_by do |r| r.package.decide_max_seat end&.package min_seat_package = filtered_packages&.min_by do |r| r.package.decide_min_seat end&.package if max_seat_package.present? || min_seat_package.present? result['max_seat'] = max_seat_package.package_attr.max_seat result['min_seat'] = min_seat_package.package_attr.min_seat true else false end elsif duration_data[inv['start_time_format']] true else false end end end.present? else false end result['is_available'] = is_available result.as_json end availability = if data['is_available'] if data['seat_left'] && data['seat_left'].to_i >= party_size.to_i if data.key?('min_seat') party_size.between?(data['min_seat'], data['max_seat']) else true end else false end else false end { 'date' => data['date'], 'availability' => availability, 'seat_left' => data['seat_left'], 'min_seat' => data['min_seat'], 'max_seat' => data['max_seat'], 'booked_seat' => data['booked_seat'], } end |
#find_available_start_times(adult:, kids:, date:) ⇒ Object
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1209 def find_available_start_times(adult:, kids:, date:) hour, min = Time.zone.now.strftime('%H:%M').split(':').map(&:to_i) mins = [0, 15, 30, 45, 60] current_min = mins.min_by { |x| (min - x).abs } fetch(['find_available_start_times', 'by_date', 'by_start_times', date, adult, kids, "#{hour}:#{current_min}", selected_slug], [], selected_slug) do date = date.to_date local_restaurant_packages = restaurant_packages(filter_by_restaurant_package_ids: true) has_packages = local_restaurant_packages.count.positive? start_times = inventories_start_times(inventories(date)) filtered_start_times_by_slugs = slugs_by_start_times(date, start_times) || {} if restaurant_package_ids.present? && selected_slug.present? filtered_start_times_by_durations = res_durations_is_enough?( date, filtered_start_times_by_slugs.keys, selected_slug ).select do |_key, value| value end.keys filtered_start_times_by_seat_lefts = seat_lefts(date, filtered_start_times_by_durations, selected_slug) filtered_start_times_by_seat_lefts.select do |_start_time, value| value.positive? end.keys.sort elsif has_packages filtered_start_times_by_durations = res_durations_is_enough?( date, filtered_start_times_by_slugs.keys ).select do |_key, value| value end.keys filtered_start_times_by_seat_lefts = seat_lefts(date, filtered_start_times_by_durations) filtered_start_times_by_seat_lefts.select do |_start_time, value| value.positive? end.keys.sort else start_times.select do |start_time| inventory_bookable?(date: date, start_time: start_time, adult: adult, kids: kids) end end end end |
#find_available_start_times_v4(adult:, kids:, date:) ⇒ Object
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1253 def find_available_start_times_v4(adult:, kids:, date:) hour, min = Time.zone.now.strftime('%H:%M').split(':').map(&:to_i) mins = [0, 15, 30, 45, 60] current_min = mins.min_by { |x| (min - x).abs } adult = adult.to_i fetch(['find_available_start_times_v4', 'by_date', 'by_start_times', 'by_slug', date, adult, kids, "#{hour}:#{current_min}", selected_slug], [], selected_slug) do date = date.to_date local_restaurant_packages = restaurant_packages(filter_by_restaurant_package_ids: true) has_packages = local_restaurant_packages.count.positive? available_times = [] inv = inventories(date, selected_slug) quantity_available = inv.map { |i| i['quantity_available'] }.max || 0 inv_start_times = inventories_start_times(inv) start_times = periods(date, selected_slug).select do |start_time| next unless reservation_meets_min_booking_time?(date, start_time, restaurant.time_zone) inv_start_times.include?(start_time) end seat_left_data = seat_lefts(date, start_times, selected_slug) return available_times if seat_left_data.blank? durations_are_enough = {} if has_packages slugs_data = slugs_by_start_times(date, start_times) # remove empty slugs slugs_data.each do |key, value| if selected_slug.present? value = value.to_a if value.include?(selected_slug) slugs_data[key] = value else slugs_data.delete key end else slugs_data[key] = value.to_a end end return available_times if slugs_data.blank? seat_left_data_by_slug = {} local_restaurant_packages.each do |rp| seat_left_data_by_slug[rp.slug] = seat_lefts(date, start_times, rp.slug) end duration_is_enough_by_slug = invert_slugs_by_start_times(slugs_data) duration_is_enough_by_slug.each do |slug, duration_start_times| duration_is_enough_by_slug[slug] = res_durations_is_enough?(date, duration_start_times.to_a, slug) end else durations_are_enough = res_durations_is_enough?(date, start_times, nil) end start_times.each do |start_time| is_bookable = false seat_left_by_start_time = seat_left_data[start_time] if HhTime.time_has_passed?(date, start_time, restaurant.time_zone) && !reservation_meets_min_booking_time?(date, start_time, restaurant.time_zone) is_bookable = false elsif has_packages && seat_left_by_start_time.present? && seat_left_by_start_time.positive? && slugs_data[start_time].present? slugs_data[start_time].each do |slug| next unless seat_left_data_by_slug[slug].present? && seat_left_data_by_slug[slug][start_time].to_i >= adult if for_dine_in min_max_seat = get_min_max_seat(slug) unless adult.between?(min_max_seat['min_seat'].to_i, min_max_seat['max_seat'].to_i) && duration_is_enough_by_slug[slug].present? && duration_is_enough_by_slug[slug][start_time] next end elsif for_delivery unless duration_is_enough_by_slug[slug].present? && duration_is_enough_by_slug[slug][start_time] next end else raise NotImplementedError end is_bookable = true break end elsif seat_left_by_start_time.present? && durations_are_enough[start_time] && seat_left_by_start_time.to_i >= adult is_bookable = true else is_bookable = false end available_times.push({ start_time: start_time, availability: is_bookable, seat_left: seat_left_data[start_time], quantity_available: quantity_available, }) end # sort by start time available_times.sort_by { |time| time[:start_time] } end end |
#get_inv_by_date(date, slug = nil) ⇒ Object
get_inv_by_date(Date.today, 'AY-001')
> “seat_left”=>50, “slugs”=>#<Set: {“222-AY-5199977”>, “duration”=>15 minutes}}
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1363 def get_inv_by_date(date, slug = nil) fetch(['get_inv_by_date', 'by_date', 'by_slug', date, slug], {}, slug) do start_times = inventories_start_times(inventories(date, slug)) seat_left_data = seat_lefts(date, start_times, slug) slugs_data = slugs_by_start_times(date, start_times) duration_data = find_durations(date, start_times, slug) result = {} periods(date, slug).each do |st| result[st] = { 'open' => start_times.include?(st), 'seat_left' => seat_left_data[st], 'slugs' => slugs_data[st], 'duration' => duration_data[st].presence || 0.minutes, } end result end end |
#get_inv_by_date_time(date, start_time, slug = nil) ⇒ Object
1396 1397 1398 1399 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1396 def get_inv_by_date_time(date, start_time, slug = nil) result = get_inv_by_date(date, slug)[start_time] result.presence || { 'slugs' => [], 'open' => false, 'seat_left' => 0, 'duration' => 0.minutes } end |
#inventory_bookable?(date:, start_time:, adult:, kids:, skip_seat_left_check: false) ⇒ Boolean
the concept is same as #package_bookable? method below, but this one is supposed to be for checking a restaurant that does not have any packages
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 457 def inventory_bookable?(date:, start_time:, adult:, kids:, skip_seat_left_check: false) return false unless valid_date_time_params?(date, start_time) if !skip_last_minute_error && less_than_min_booking_time?(date, start_time) errors.add(:base, I18n.t('inventory.you_may_wait_up_to', min_booking_time: restaurant.min_booking_time)) return false end start_times = inventories_start_times(inventories(date)) unless start_times.include?(start_time) errors.add(:base, 'Invalid start time') return false end # Skip cached seat_left check when skip_seat_left_check is true # This is used during booking flow where Level 1 check (with DB lock) will verify seat availability if !skip_seat_left_check && !(seat_left(date, start_time) >= (adult + kids)) errors.add(:base, I18n.t('inventory.seat_is_full')) return false end unless res_duration_is_enough?(date, start_time) errors.add(:base, I18n.t('inventory.not_available')) return false end true end |
#open?(date, start_time, slug = nil) ⇒ Boolean
if slug is exist, then it will check in Package level otherwise it will check on restaurant level
1391 1392 1393 1394 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1391 def open?(date, start_time, slug = nil) start_times = get_inv_by_date(date, slug) (start_times[start_time] && start_times[start_time]['open']) || false end |
#package_bookable?(date:, start_time:, adult:, kids:, slug:, adult_from: nil, skip_seat_left_check: false) ⇒ Boolean
Check availability in package level with support for reservation updates This method supports checking inventory for both new bookings and reservation updates
For reservation updates (when adult_from is provided):
-
Calculates the net change in party size (adult - adult_from)
-
Only checks availability for the additional seats needed
-
Allows party size reductions without availability checks
-
Still validates min/max seat limits against the total new party size
For new bookings (when adult_from is nil):
-
Checks availability for the full party size as before
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 508 def package_bookable?(date:, start_time:, adult:, kids:, slug:, adult_from: nil, skip_seat_left_check: false) # Create a unique cache key for memoization cache_key = "package_bookable:#{slug}:#{date}:#{start_time}:#{adult}:#{kids}:#{adult_from}:#{skip_seat_left_check}" # Check if we already computed this result in the current request request_cache = RequestStore.store[:package_bookable_cache] ||= {} return request_cache[cache_key] if request_cache.key?(cache_key) result = package_bookable_implementation(date: date, start_time: start_time, adult: adult, kids: kids, slug: slug, adult_from: adult_from, skip_seat_left_check: skip_seat_left_check) # Cache the result for this request request_cache[cache_key] = result result end |
#packages_bookable?(date:, start_times:, adult:, kids:, slugs:) ⇒ Array<Hash>
Check availability of given packages by checking inventory and package availability.
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 761 def packages_bookable?(date:, start_times:, adult:, kids:, slugs:) start_times = Array(start_times.dup).sort! slugs = Array(slugs) valid_start_times_by_slugs = packages_bookable_start_time_validations?(date, start_times, slugs) # valid_start_times_by_slugs => {"222-AY-4776621"=> ["16:00", "16:15", "16:30"]} if valid_start_times_by_slugs.blank? return slugs.map do |slug| { slug: slug, start_times: start_times.map { |st| { st => false } } } end end # Iterate over each slug to evaluate the start times slugs.map do |slug| valid_start_times_by_slug = valid_start_times_by_slugs[slug] if valid_start_times_by_slug.blank? { slug: slug, start_times: start_times.map { |st| { st => false } } } else invalid_start_times_by_slug = start_times - valid_start_times_by_slug result = invalid_start_times_by_slug.map do |st| { st => false } end seat_lefts(date, valid_start_times_by_slug, slug).each do |start_time, seat_left| result << if seat_left >= adult.to_i { start_time => true } else { start_time => false } end end # sort result by start time result = result.sort_by { |item| item.keys.first } # Return the slug and its processed start times (either true or false) { slug: slug, start_times: result } end end.compact # Remove any nil values resulting from 'next' statements end |
#recommend_date(start_date, adult, slug_param = nil) ⇒ Object
recommend any packages on >= start_date
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 859 def recommend_date(start_date, adult, slug_param = nil) return nil if adult.blank? || adult.zero? next_week = now.to_date + 14.days limit_date = restaurant.expiry_date < next_week ? restaurant.expiry_date : next_week while start_date <= limit_date invs = get_inv_by_date(start_date, slug_param) if slug_param.nil? && invs.find do |_, data| data['open'] && data['seat_left'] >= adult && data['slugs'].length.positive? end.present? return start_date elsif !slug_param.nil? && invs.find do |_, data| data['open'] && data['seat_left'] >= adult && data['slugs'].include?(slug_param) end.present? return start_date else start_date += 1.day end end nil end |
#recommend_package(date:, start_time:, adult:, kids:, slug:) ⇒ Object
recommend available package
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 900 def recommend_package(date:, start_time:, adult:, kids:, slug:) time_slots_found = [] checker = InvCheckerFactory.new(restaurant_id, time_zone) checker.create_inv_checker_service start_time_index = periods(date, slug).index(start_time) return [] if start_time_index.nil? date = date.dup original_date = date.dup last_date = original_date + 7.days loop do break if time_slots_found.length == 3 || date == last_date start_time_enum = periods(date, slug) start_time_enum.each do |st| break if time_slots_found.length == 3 if checker.package_bookable?(date: date, start_time: st, adult: adult.to_i, kids: kids.to_i, slug: slug) time_slots_found.push(date: date, start_time: st) end end date += 1.day end time_slots_found end |
#restaurant_packages(filter_by_restaurant_package_ids: true) ⇒ Object
set this parameter to be false to return all active restaurant packages
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1557 def restaurant_packages(filter_by_restaurant_package_ids: true) # allow inactive package to be bookable today = Time.now_in_tz(time_zone).to_date fetch_from_local_cache( local_cache_namespace: 'restaurant_packages', local_cache_key: [today, filter_by_restaurant_package_ids, restaurant_package_ids], default_value: HhPackage::RestaurantPackage.none, ) do data = restaurant.all_restaurant_packages.preload(:restaurant, package: :package_attr) data = if filter_by_restaurant_package_ids && restaurant_package_ids.present? data.where(id: restaurant_package_ids) else data.valid_to_have_agendas end if restaurant.has_delivery_inventories? if for_dine_in && !for_delivery data.select { |rp| rp.package.for_dine_in? } elsif !for_dine_in && for_delivery data.select { |rp| rp.package.for_delivery? } else data end elsif for_dine_in && !for_delivery data.select { |rp| rp.package.for_dine_in? } elsif !for_dine_in && for_delivery data.select { |rp| rp.package.for_delivery? } else data end # HH_LOGGER.debug("Update RestaurantPackages", { # filter_by_restaurant_package_ids: filter_by_restaurant_package_ids, # data: data.map(&:id), # }) data end end |
#seat_left(date, start_time, slug = nil) ⇒ Object
931 932 933 934 935 936 937 938 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 931 def seat_left(date, start_time, slug = nil) fetch_from_local_cache(local_cache_namespace: 'seat_lefts', local_cache_key: [date, start_time, slug], default_value: 0) do result = seat_lefts(date, Reservation::PERIODS_WITHOUT_00_AM, slug) result[start_time].presence || 0 end end |
#set_delivery_inventory? ⇒ Boolean
1415 1416 1417 1418 1419 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1415 def set_delivery_inventory? return false unless for_delivery.to_s == 'true' restaurant.has_delivery_inventories? end |
#slugs(date, start_time) ⇒ Object
no need to filter by slug
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1402 def slugs(date, start_time) fetch_from_local_cache(local_cache_namespace: 'slugs', local_cache_key: [date, start_time], default_value: []) do result = slugs_by_start_times(date, [start_time]) if result.present? result[start_time] else {} end end end |
#take_away_cache_keys ⇒ Array<String>
Retrieves all cache keys for take-away (delivery) service type
1494 1495 1496 1497 1498 1499 1500 |
# File 'app/services/inventory/inv_checker_hungry_hub_service.rb', line 1494 def take_away_cache_keys redis_key = "#{namespace_raw}:cache_keys" keys = [] inv_redis_pool.with do |redis| keys = redis.zrange(redis_key, 0, -1) end end |