Sha256: 9ba0e4f9c9a7a488595944949d75fe0971be0d74ae8470848c38547cb18d9e52

Contents?: true

Size: 1.35 KB

Versions: 19

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    module Voter
      # This command allows the user to store and cast their vote.
      class CastVote < Rectify::Command
        # Public: Initializes the command.
        #
        # form - A form with necessary info to cast a vote.
        def initialize(form)
          @form = form
        end

        # Store and cast the vote
        #
        # Broadcasts :ok if successful, :invalid otherwise
        def call
          return broadcast(:invalid) if form.invalid?

          transaction do
            cast_vote
          end

          broadcast(:ok, vote)
        rescue StandardError => e
          broadcast(:invalid, e.message)
        end

        private

        attr_reader :form, :vote

        delegate :bulletin_board, to: :form

        def cast_vote
          bulletin_board.cast_vote(form.election_id, form.voter_id, form.encrypted_data) do |message_id|
            create_vote(message_id)
          end
        end

        def create_vote(message_id)
          @vote = Vote.create!(
            message_id: message_id,
            election: form.election,
            voter_id: form.voter_id,
            encrypted_vote_hash: form.encrypted_data_hash,
            status: :pending,
            user: form.user,
            email: form.email
          )
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-elections-0.26.10 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.9 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.8 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.7 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.5 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.4 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.3 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.2 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.1 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.0 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.0.rc2 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.26.0.rc1 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.25.2 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.25.1 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.25.0 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.25.0.rc4 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.25.0.rc3 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.25.0.rc2 app/commands/decidim/elections/voter/cast_vote.rb
decidim-elections-0.25.0.rc1 app/commands/decidim/elections/voter/cast_vote.rb