Class: SpreeCmCommissioner::VoteCounters::PerContestantCounter

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

Constant Summary collapse

EMPTY_RESULT =
{
  counts: {}.freeze,
  unique_voters: 0,
  unique_voters_per_contestant: {}.freeze
}.freeze

Constants inherited from Base

Base::REDIS_KEY_TTL

Instance Attribute Summary

Attributes inherited from Base

#voting_session_id

Instance Method Summary collapse

Instance Method Details

#call(voting_session_id:) ⇒ Object



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

def call(voting_session_id:)
  @voting_session_id = voting_session_id

  ids = contestant_ids
  return success(EMPTY_RESULT) if ids.empty?

  counts = with_redis do |redis|
    values = redis.mget(*ids.map { |id| vote_key(id) })
    ids.zip(values).to_h { |id, v| [id, v.to_i] }
  end

  success(
    counts: counts,
    unique_voters: session_unique_voter_count,
    unique_voters_per_contestant: contestant_unique_voter_counts(ids)
  )
end