Sha256: a1959297fc3c612baed5937c9c60f31f5d04d06fe308e6cd0e19649a3f84d7a7

Contents?: true

Size: 1.96 KB

Versions: 14

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module Decidim
  module BulletinBoard
    module Voter
      # This command uses the GraphQL client to cast the vote.
      class CastVote < Decidim::BulletinBoard::Command
        # Public: Initializes the command.
        #
        # form - A form object with the params.
        def initialize(election_id, voter_id, encrypted_vote)
          @election_id = election_id
          @voter_id = voter_id
          @encrypted_vote = encrypted_vote
        end

        # Returns the message_id related to the operation
        def message_id
          @message_id ||= build_message_id(unique_election_id(election_id), "vote.cast", voter_id)
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid and the mutation operation is successful.
        # - :error if the form wasn't valid or the mutation 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, { content: encrypted_vote })
          }

          begin
            response = graphql.query do
              mutation do
                vote(messageId: args[:message_id], signedData: args[:signed_data]) do
                  pendingMessage do
                    messageId
                    status
                  end
                  error
                end
              end
            end

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

            broadcast(:ok, response.data.vote.pending_message)
          rescue Graphlient::Errors::FaradayServerError
            broadcast(:error, "something went wrong")
          end
        end

        private

        delegate :cast_vote_message_id, to: :class

        attr_reader :election_id, :voter_id, :encrypted_vote
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
decidim-bulletin_board-0.17.1 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.17.0 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.16.1 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.16.0 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.15.2 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.15.1 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.14.0 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.13.1 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.13.0 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.12.1 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.12.0 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.11.0 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.10.1 lib/decidim/bulletin_board/voter/cast_vote.rb
decidim-bulletin_board-0.10.0 lib/decidim/bulletin_board/voter/cast_vote.rb