Sha256: 2f3296af854ace6e9e6a70cdb78d272bd1d33ef0a36f40dd7d5cbe560b83c111

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    module Voter
      # This command updates the vote status and sends a notification.
      class UpdateVoteStatus < Rectify::Command
        # Public: Initializes the command.
        #
        # message_id - the message_id to find a pending_message that is related to a vote.
        def initialize(vote)
          @vote = vote
        end

        # Update status and send notification
        #
        # Broadcasts :ok if successful, :invalid otherwise
        def call
          return broadcast(:ok) unless status_changed?

          transaction do
            update_vote_status
            send_vote_notification
          end

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

        private

        attr_reader :vote

        def status_changed?
          vote.status != vote_status
        end

        def bulletin_board
          @bulletin_board ||= Decidim::Elections.bulletin_board
        end

        def vote_status
          @vote_status ||= bulletin_board.get_pending_message_status(vote.message_id)
        end

        def update_vote_status
          vote.status = vote_status
          vote.save!
        end

        def send_vote_notification
          return unless vote.accepted?

          data = {
            event: "decidim.events.elections.votes.accepted_votes",
            event_class: Decidim::Elections::Votes::VoteAcceptedEvent,
            resource: vote.election,
            affected_users: [vote.user],
            extra: {
              vote: vote
            }
          }

          Decidim::EventsManager.publish(data)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-elections-0.24.3 app/commands/decidim/elections/voter/update_vote_status.rb
decidim-elections-0.24.2 app/commands/decidim/elections/voter/update_vote_status.rb
decidim-elections-0.24.1 app/commands/decidim/elections/voter/update_vote_status.rb
decidim-elections-0.24.0 app/commands/decidim/elections/voter/update_vote_status.rb
decidim-elections-0.24.0.rc2 app/commands/decidim/elections/voter/update_vote_status.rb
decidim-elections-0.24.0.rc1 app/commands/decidim/elections/voter/update_vote_status.rb