Sha256: 82a261033a97fc2c87e780c15a1a68083742ac088ae326af1d7151087c55438e

Contents?: true

Size: 1.27 KB

Versions: 12

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to accept a join request to a user
  # group.
  class AcceptUserGroupJoinRequest < Decidim::Command
    # Public: Initializes the command.
    #
    # membership - the UserGroupMembership to be accepted.
    def initialize(membership)
      @membership = membership
    end

    # Executes the command. Broadcasts these events:
    #
    # - :ok when everything is valid.
    # - :invalid if we couldn't proceed.
    #
    # Returns nothing.
    def call
      return broadcast(:invalid) if membership.role.to_s != "requested"

      transaction do
        accept_membership
        send_notification
      end

      broadcast(:ok, @user_group)
    end

    private

    attr_reader :membership

    def accept_membership
      membership.role = :member
      membership.save!
    end

    def send_notification
      Decidim::EventsManager.publish(
        event: "decidim.events.groups.join_request_accepted",
        event_class: JoinRequestAcceptedEvent,
        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

12 entries across 12 versions & 1 rubygems

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