Class: SpreeCmCommissioner::VoteCounters::SnapshotToDb

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

Instance Method Summary collapse

Instance Method Details

#call(voting_session_id:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/spree_cm_commissioner/vote_counters/snapshot_to_db.rb', line 15

def call(voting_session_id:)
  voting_session = SpreeCmCommissioner::VotingSession.find(voting_session_id)

  # TODO: manual/judge-driven sessions (scoring_model: :manual) should skip vote tallying
  # and derive leading_contestant_id from Advancer data (contestants not eliminated) instead.
  # Will be implemented in the next PR alongside the full scoring_model enum.

  # Sum all votes per contestant and count how many distinct users voted per contestant.
  db_counts           = fetch_vote_counts(voting_session_id)
  unique_voter_counts = fetch_unique_voter_counts(voting_session_id)

  # Persist per-contestant tallies, then write session-level aggregates.
  upsert_contestant_counts(voting_session_id, db_counts, unique_voter_counts)
  upsert_session_stat(voting_session, db_counts)

  success(nil)
end