Class: BookingSummaryWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
app/workers/booking_summary_worker.rb

Overview

BookingSummaryWorker is a Sidekiq worker that updates booking summaries for users and restaurants. It processes either:

  1. All reservations from the previous day.

  2. A specific set of users provided via `user_ids` (for data migrations).

Examples:

Perform the worker for all reservations

BookingSummaryWorker.perform_async

Perform the worker for specific users

BookingSummaryWorker.perform_async([1, 2, 3])

Instance Method Summary collapse

Instance Method Details

#perform(user_ids = nil) ⇒ void

This method returns an undefined value.

Updates booking summaries for users and associated restaurants.

Parameters:

  • user_ids (Array<Integer>, nil) (defaults to: nil)

    An optional list of user IDs. If nil, it processes reservations from the previous day for all users.



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
52
53
54
55
56
57
58
59
60
61
# File 'app/workers/booking_summary_worker.rb', line 21

def perform(user_ids = nil)
  # temporary disable this worker
  # because it takes hours to finish
  return

  start_of_day = Time.now.in_time_zone('Asia/Bangkok').yesterday.beginning_of_day
  end_of_day = Time.now.in_time_zone('Asia/Bangkok').yesterday.end_of_day

  # If user_ids are not provided, fetch unique user IDs from reservations created yesterday.
  # This handles cases where the worker is triggered without specific user targets.
  user_ids ||= Reservation.
    select(:user_id).
    where(created_at: start_of_day..end_of_day).
    pluck(:user_id).
    uniq.
    compact

  # Determine the list of restaurant IDs associated with the users or reservations.
  restaurant_ids = if user_ids.present?
                     Reservation.
                       select(:restaurant_id).
                       where(user_id: user_ids).
                       pluck(:restaurant_id).
                       uniq.
                       compact
                   else
                     Reservation.select(:restaurant_id).
                       where(created_at: start_of_day..end_of_day).
                       pluck(:restaurant_id).
                       uniq.
                       compact
                   end

  # Update booking summaries for each user and their associated restaurants.
  User.where(id: user_ids).find_each do |user|
    user.update_booking_summary(nil) # Update summary for all restaurants
    restaurant_ids.each do |restaurant_id|
      user.update_booking_summary(restaurant_id)
    end
  end
end