Class: SpreeCmCommissioner::WaitingRoomSessionCreator
- Inherits:
-
BaseInteractor
- Object
- BaseInteractor
- SpreeCmCommissioner::WaitingRoomSessionCreator
- Defined in:
- app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb
Instance Method Summary collapse
- #assign_token_and_create_session_to_db ⇒ Object
- #call ⇒ Object
- #call_other_waiting_guests ⇒ Object
- #expired_at ⇒ Object
- #firestore ⇒ Object
- #full? ⇒ Boolean
- #generate_jwt_token ⇒ Object
- #log_to_firebase ⇒ Object
- #service_account ⇒ Object
Instance Method Details
#assign_token_and_create_session_to_db ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 33 def assign_token_and_create_session_to_db # create or renew context.room_session = SpreeCmCommissioner::WaitingRoomSession.where(guest_identifier: waiting_guest_firebase_doc_id).first_or_initialize context.room_session.assign_attributes( jwt_token: context.jwt_token, expired_at: expired_at, remote_ip: remote_ip, page_path: page_path ) context.room_session.save! end |
#call ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 7 def call return context.fail!(message: 'must_provide_waiting_guest_firebase_doc_id') if waiting_guest_firebase_doc_id.blank? return context.fail!(message: 'must_provide_remote_ip') if remote_ip.blank? return context.fail!(message: 'sessions_reach_it_maximum') if full? generate_jwt_token assign_token_and_create_session_to_db log_to_firebase # commented because of following bug: https://github.com/channainfo/commissioner/issues/2185 # this job is already run every 1mn, disabling it still work. # call_other_waiting_guests end |
#call_other_waiting_guests ⇒ Object
60 61 62 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 60 def call_other_waiting_guests SpreeCmCommissioner::WaitingGuestsCallerJob.perform_later end |
#expired_at ⇒ Object
64 65 66 67 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 64 def expired_at expired_duration = ENV['WAITING_ROOM_SESSION_EXPIRE_DURATION_IN_SECOND']&.presence&.to_i || (60 * 3) context.expired_at ||= expired_duration.seconds.from_now end |
#firestore ⇒ Object
69 70 71 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 69 def firestore @firestore ||= Google::Cloud::Firestore.new(project_id: service_account[:project_id], credentials: service_account) end |
#full? ⇒ Boolean
21 22 23 24 25 26 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 21 def full? fetcher = SpreeCmCommissioner::WaitingRoomSystemMetadataFetcher.new(firestore: firestore) fetcher.load_document_data SpreeCmCommissioner::WaitingRoomSession.active.size >= fetcher.max_sessions_count_with_min end |
#generate_jwt_token ⇒ Object
28 29 30 31 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 28 def generate_jwt_token payload = { exp: expired_at.to_i } context.jwt_token = JWT.encode(payload, ENV.fetch('WAITING_ROOM_SESSION_SIGNATURE'), 'HS256') end |
#log_to_firebase ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 45 def log_to_firebase current_date = Time.zone.now.strftime('%Y-%m-%d') document = firestore.col('waiting_guests') .doc(current_date) .col('records') .doc(waiting_guest_firebase_doc_id) data = document.get.data.dup data[:entered_room_at] = Time.zone.now data[:page_path] = page_path document.update(data) end |
#service_account ⇒ Object
73 74 75 |
# File 'app/interactors/spree_cm_commissioner/waiting_room_session_creator.rb', line 73 def service_account Rails.application.credentials.cloud_firestore_service_account end |