Sha256: 0e6838de7d7d6f500af41a02af7b8079e41dc50cf2b2c825be50b21e9eb4c2da

Contents?: true

Size: 1.99 KB

Versions: 12

Compression:

Stored size: 1.99 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 < Decidim::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

12 entries across 12 versions & 1 rubygems

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