Sha256: 2a0ca265378e25b3313b515588945e7b629a365e75bea13ae6e8c8a6029d769b

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to promote a user to group admin.
  class PromoteMembership < Decidim::Command
    # Public: Initializes the command.
    #
    # membership - the UserGroupMembership to be promoted.
    def initialize(membership, user_group)
      @membership = membership
      @user_group = user_group
    end

    # Executes the command. Broadcasts these events:
    #
    # - :ok when everything is valid.
    # - :invalid if we could not proceed.
    #
    # Returns nothing.
    def call
      return broadcast(:invalid) if membership.blank?
      return broadcast(:invalid) if membership.role != "member"
      return broadcast(:invalid) if membership.user_group != user_group

      transaction do
        promote_membership
        send_notification
      end

      broadcast(:ok)
    end

    private

    attr_reader :membership, :user_group

    def promote_membership
      membership.role = :admin
      membership.save!
    end

    def send_notification
      Decidim::EventsManager.publish(
        event: "decidim.events.groups.promoted_to_admin",
        event_class: PromotedToAdminEvent,
        resource: membership.user_group,
        affected_users: [membership.user],
        extra: {
          user_group_name: membership.user_group.name,
          user_group_nickname: membership.user_group.nickname
        }
      )
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/commands/decidim/promote_membership.rb
decidim-core-0.28.4 app/commands/decidim/promote_membership.rb
decidim-core-0.29.0 app/commands/decidim/promote_membership.rb
decidim-core-0.28.3 app/commands/decidim/promote_membership.rb
decidim-core-0.29.0.rc4 app/commands/decidim/promote_membership.rb
decidim-core-0.29.0.rc3 app/commands/decidim/promote_membership.rb
decidim-core-0.29.0.rc2 app/commands/decidim/promote_membership.rb
decidim-core-0.29.0.rc1 app/commands/decidim/promote_membership.rb
decidim-core-0.28.2 app/commands/decidim/promote_membership.rb
decidim-core-0.28.1 app/commands/decidim/promote_membership.rb
decidim-core-0.28.0 app/commands/decidim/promote_membership.rb
decidim-core-0.28.0.rc5 app/commands/decidim/promote_membership.rb
decidim-core-0.28.0.rc4 app/commands/decidim/promote_membership.rb