Class: SpreeCmCommissioner::VotingSessions::AutoOpen

Inherits:
Object
  • Object
show all
Includes:
Spree::ServiceModule::Base
Defined in:
app/services/spree_cm_commissioner/voting_sessions/auto_open.rb

Overview

Opens voting the moment a pre-set vote_open_at arrives, for the two cases that never go through #start_vote!:

already enabled — vote_open_at/vote_close_at were written directly (e.g. the organizer edit
form) with status already :enabled. can_vote? is purely time-driven once enabled, so
nothing needs to change here except re-publishing Firestore's `state` from 'upcoming' to
'active' — a missed run only costs a stale label in the app, never a wrongly rejected vote.

on_air — revealed but not yet votable (see VotingSessionTimingConcern's class comment).
visible? gates the flip: an organizer chose to reveal this session, so honoring its
pre-set vote_open_at here is completing their intent, not bypassing it. A missed run here
does matter (nothing else flips on_air -> enabled on a timer), so this path is the one
genuine "auto-open" — the organizer would otherwise have to notice and press Activate by
hand once vote_open_at has passed.

Called by VotingSessionAutoOpenJob, enqueued from VotingSessions::ScheduleVoteWindowJobs with wait_until: vote_open_at.

Instance Method Summary collapse

Instance Method Details

#call(voting_session:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/spree_cm_commissioner/voting_sessions/auto_open.rb', line 23

def call(voting_session:)
  # vote_open_at may have moved since this was scheduled (an organizer editing the window
  # again, or a restart). Only act once the window has genuinely arrived; otherwise the run
  # that matches the new vote_open_at will handle it.
  return success(:skipped) if voting_session.vote_open_at.present? && Time.current < voting_session.vote_open_at

  # Triggers VotingSessionTimingConcern's own after_commit callbacks on this same save, which
  # schedules VotingSessionAutoCloseJob for vote_close_at now that the session is enabled —
  # no need to enqueue it again here.
  voting_session.update!(status: :enabled) if voting_session.on_air? && voting_session.visible?

  SpreeCmCommissioner::VotingSessions::PublishToFirestore.call(voting_session: voting_session)
end