Sha256: 2749c7a4d78a86829082712250966718fd4f9ab78242b90ba157bd3daa245973

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Decidim
  module BulletinBoard
    module Authority
      # This command uses the GraphQL client to publish the election results.
      class PublishResults < Decidim::BulletinBoard::Command
        # Public: Initializes the command.
        #
        # election_id - The local election identifier
        def initialize(election_id)
          @election_id = election_id
        end

        # Returns the message_id related to the operation
        def message_id
          @message_id ||= build_message_id(unique_election_id(election_id), "publish_results")
        end

        # 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.
        def call
          # arguments used inside the graphql operation
          args = {
            message_id: message_id,
            signed_data: sign_message(message_id, {})
          }

          response = client.query do
            mutation do
              publishResults(messageId: args[:message_id], signedData: args[:signed_data]) do
                election do
                  status
                end
                error
              end
            end
          end

          return broadcast(:error, response.data.publish_results.error) if response.data.publish_results.error.present?

          broadcast(:ok, response.data.publish_results.election)
        rescue Graphlient::Errors::ServerError
          broadcast(:error, "Sorry, something went wrong")
        end

        private

        attr_reader :election_id
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-bulletin_board-0.9.2 lib/decidim/bulletin_board/authority/publish_results.rb
decidim-bulletin_board-0.9.1 lib/decidim/bulletin_board/authority/publish_results.rb
decidim-bulletin_board-0.9.0 lib/decidim/bulletin_board/authority/publish_results.rb