Class: SpreeCmCommissioner::WaitingGuestsCaller

Inherits:
BaseInteractor show all
Defined in:
app/interactors/spree_cm_commissioner/waiting_guests_caller.rb

Instance Method Summary collapse

Instance Method Details

#callObject



6
7
8
9
10
11
12
13
14
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 6

def call
  available_slots = fetch_available_slots
  return unless available_slots.positive?

  long_waiting_guests = fetch_long_waiting_guests(available_slots)
  calling_all(long_waiting_guests)

  mark_as(full: long_waiting_guests.size >= available_slots, available_slots: available_slots - long_waiting_guests.size)
end

#calling_all(waiting_guests) ⇒ Object

For alert waiting guests to enter room, we just update :allow_to_enter_room_at. App will listen to firebase & start refresh session token to enter room.



36
37
38
39
40
41
42
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 36

def calling_all(waiting_guests)
  waiting_guests.each do |document|
    data = document.data.dup
    data[:allow_to_enter_room_at] = Time.zone.now
    document.ref.update(data)
  end
end

#current_dateObject



44
45
46
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 44

def current_date
  Time.zone.now.strftime('%Y-%m-%d')
end

#fetch_available_slotsObject



16
17
18
19
20
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 16

def fetch_available_slots
  max_sessions = fetch_max_sessions
  active_sessions = SpreeCmCommissioner::WaitingRoomSession.active.count
  max_sessions - active_sessions
end

#fetch_long_waiting_guests(available_slots) ⇒ Object

This query required index. create them in Firebase beforehand. Client side must create waiting_guests document with :queued_at & :allow_to_enter_room_at to null to allow fillter & order.



24
25
26
27
28
29
30
31
32
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 24

def fetch_long_waiting_guests(available_slots)
  firestore.col('waiting_guests')
           .doc(current_date)
           .col('records')
           .where('allow_to_enter_room_at', '==', nil)
           .order('queued_at')
           .limit(available_slots)
           .get.to_a
end

#fetch_max_sessionsObject



53
54
55
56
57
58
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 53

def fetch_max_sessions
  fetcher = SpreeCmCommissioner::WaitingRoomSystemMetadataFetcher.new(firestore: firestore)
  fetcher.load_document_data

  fetcher.max_sessions_count_with_min
end

#firestoreObject



60
61
62
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 60

def firestore
  @firestore ||= Google::Cloud::Firestore.new(project_id: [:project_id], credentials: )
end

#mark_as(full:, available_slots:) ⇒ Object

When open app, app request to check whether room is full or not via Firebase instead of server to minimize server requests.



49
50
51
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 49

def mark_as(full:, available_slots:)
  firestore.col('waiting_rooms').doc('lobby').set({ full: full, available_slots: available_slots })
end

#service_accountObject



64
65
66
# File 'app/interactors/spree_cm_commissioner/waiting_guests_caller.rb', line 64

def 
  @service_account ||= Rails.application.credentials.
end