Class: SpreeCmCommissioner::VotingSessions::ScheduleVoteWindowJobs

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

Overview

Single scheduling point for both auto-open and auto-close, covering every path that can set a real vote window — #start_vote!, #extend_vote!, an organizer pre-setting vote_open_at/vote_close_at directly (e.g. via the edit form) without ever calling #start_vote!, and revealing an archived session that already has a pre-set window.

Called from VotingSessionTimingConcern's after_commit callback on every relevant change — see #vote_window_scheduling_relevant_change? there.

Auto-open is offered to on_air as well as enabled: on_air is exactly "revealed but not yet votable" (see the concern's class comment), so a pre-set window on an on_air session is meant to auto-enable once vote_open_at arrives — VotingSessionAutoOpenJob does that flip itself, gated on visible? there. archived is deliberately excluded: an organizer hasn't revealed it yet, and auto-open must never bypass that manual reveal step, however far in the past vote_open_at already is.

Auto-close stays gated on enabled? only — closing something that was never actually opened (still archived/on_air) doesn't mean anything.

Each job re-checks its own timestamp/status on the way out (see VotingSessionAutoOpenJob / VotingSessionAutoCloseJob), so re-saving a window before it fires (a fresh #extend_vote!, an organizer editing the times again) just leaves a stale scheduled run that no-ops — no need to track or cancel the previous job here.

Instance Method Summary collapse

Instance Method Details

#call(voting_session:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/spree_cm_commissioner/voting_sessions/schedule_vote_window_jobs.rb', line 28

def call(voting_session:)
  return success(:skipped) unless voting_session.enabled? || voting_session.on_air?

  if voting_session.vote_open_at.present? && voting_session.vote_open_at > Time.current
    SpreeCmCommissioner::VotingSessionAutoOpenJob.set(wait_until: voting_session.vote_open_at)
                                                 .perform_later(voting_session.id)
  end

  if voting_session.enabled? && voting_session.vote_close_at.present? && voting_session.vote_close_at > Time.current
    SpreeCmCommissioner::VotingSessionAutoCloseJob.set(wait_until: voting_session.vote_close_at)
                                                  .perform_later(voting_session.id)
  end

  success(voting_session)
end