Sha256: f303e5b03bc8e2d809803a438140f420e130a97c058f7d254ae8acc7ef119aca

Contents?: true

Size: 1.9 KB

Versions: 12

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic to publish many answers at once.
      class PublishAnswers < Decidim::Command
        # Public: Initializes the command.
        #
        # component - The component that contains the answers.
        # user - the Decidim::User that is publishing the answers.
        # proposal_ids - the identifiers of the proposals with the answers to be published.
        def initialize(component, user, proposal_ids)
          @component = component
          @user = user
          @proposal_ids = proposal_ids
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid.
        # - :invalid if there are not proposals to publish.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) unless proposals.any?

          proposals.each do |proposal|
            transaction do
              mark_proposal_as_answered(proposal)
              notify_proposal_answer(proposal)
            end
          end

          broadcast(:ok)
        end

        private

        attr_reader :component, :user, :proposal_ids

        def proposals
          @proposals ||= Decidim::Proposals::Proposal
                         .published
                         .answered
                         .state_not_published
                         .where(component: component)
                         .where(id: proposal_ids)
        end

        def mark_proposal_as_answered(proposal)
          Decidim.traceability.perform_action!(
            "publish_answer",
            proposal,
            user
          ) do
            proposal.update!(state_published_at: Time.current)
          end
        end

        def notify_proposal_answer(proposal)
          NotifyProposalAnswer.call(proposal, nil)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-proposals-0.27.9 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.8 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.7 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.6 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.5 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.4 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.3 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.2 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.1 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.0 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.0.rc2 app/commands/decidim/proposals/admin/publish_answers.rb
decidim-proposals-0.27.0.rc1 app/commands/decidim/proposals/admin/publish_answers.rb