Sha256: 0a4758a2a08347d32ab687aa63a46ffa991e47bfd74c838be038b10774ce9d11

Contents?: true

Size: 1.98 KB

Versions: 34

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

module Decidim
  module Proposals
    module Admin
      # A command with all the business logic when an admin creates a private note proposal.
      class CreateProposalNote < Rectify::Command
        # Public: Initializes the command.
        #
        # form         - A form object with the params.
        # proposal - the proposal to relate.
        def initialize(form, proposal)
          @form = form
          @proposal = proposal
        end

        # Executes the command. Broadcasts these events:
        #
        # - :ok when everything is valid, together with the note proposal.
        # - :invalid if the form wasn't valid and we couldn't proceed.
        #
        # Returns nothing.
        def call
          return broadcast(:invalid) if form.invalid?

          create_proposal_note
          notify_admins_and_valuators

          broadcast(:ok, proposal_note)
        end

        private

        attr_reader :form, :proposal_note, :proposal

        def create_proposal_note
          @proposal_note = Decidim.traceability.create!(
            ProposalNote,
            form.current_user,
            {
              body: form.body,
              proposal: proposal,
              author: form.current_user
            },
            resource: {
              title: proposal.title
            }
          )
        end

        def notify_admins_and_valuators
          affected_users = Decidim::User.org_admins_except_me(form.current_user).all
          affected_users += Decidim::Proposals::ValuationAssignment.includes(valuator_role: :user).where.not(id: form.current_user.id).where(proposal: proposal).map(&:valuator)

          data = {
            event: "decidim.events.proposals.admin.proposal_note_created",
            event_class: Decidim::Proposals::Admin::ProposalNoteCreatedEvent,
            resource: proposal,
            affected_users: affected_users
          }

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

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
decidim-proposals-0.26.10 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.9 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.8 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.7 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.5 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.4 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.3 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.2 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.1 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.0 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.0.rc2 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.26.0.rc1 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.25.2 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.25.1 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.25.0 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.25.0.rc4 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.25.0.rc3 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.25.0.rc2 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.25.0.rc1 app/commands/decidim/proposals/admin/create_proposal_note.rb
decidim-proposals-0.24.3 app/commands/decidim/proposals/admin/create_proposal_note.rb