Class: Decidim::BulletinBoard::Authority::GetElectionResults

Inherits:
Command
  • Object
show all
Defined in:
lib/decidim/bulletin_board/authority/get_election_results.rb

Overview

This command uses the GraphQL client to get the results of an election.

Instance Attribute Summary

Attributes inherited from Command

#graphql, #settings

Instance Method Summary collapse

Methods inherited from Command

#build_message_id, #complete_message, #configure, #sign_message, #unique_election_id

Constructor Details

#initialize(election_id) ⇒ GetElectionResults

Public: Initializes the command.

election_id [String] - The local election identifier



11
12
13
# File 'lib/decidim/bulletin_board/authority/get_election_results.rb', line 11

def initialize(election_id)
  @election_id = election_id
end

Instance Method Details

#callObject

Executes the command. Broadcasts these events:

  • :ok when everything is valid and the query operation is successful.

  • :error if query operation was not successful.

Returns nothing.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/decidim/bulletin_board/authority/get_election_results.rb', line 21

def call
  # arguments used inside the graphql operation
  # unique_id [String] as election identifier
  # types [Array of Strings] to filter election log entries by their type
  args = {
    unique_id: unique_election_id(election_id),
    types: ["end_tally"]
  }

  response = graphql.query do
    query do
      election(uniqueId: args[:unique_id]) do
        logEntries(types: args[:types]) do
          signedData
        end
        verifiableResultsUrl
        verifiableResultsHash
      end
    end
  end

  return broadcast(:error, "There aren't any log entries with type: 'end_tally' for this election.") if response.data.election.log_entries.empty?

  @election = response.data.election
  @signed_data = election.log_entries.first.signed_data

  broadcast(:ok, { election_results: decoded_data["results"], verifiable_results: })
rescue Graphlient::Errors::ServerError
  broadcast(:error, "Sorry, something went wrong")
end