Sha256: 256a02255baa88d1c3823502d28915bfe20bd984086ce408c8c4caa58aababcc

Contents?: true

Size: 1.4 KB

Versions: 12

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to join a user group.
  class JoinUserGroup < Decidim::Command
    # Public: Initializes the command.
    #
    # user - the user that wants to join the group
    # user_group - The user group to join
    def initialize(user, user_group)
      @user = user
      @user_group = user_group
    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 already_member?

      transaction do
        join_user_group
        send_notification
      end

      broadcast(:ok, @user_group)
    end

    private

    attr_reader :user, :user_group

    def join_user_group
      Decidim::UserGroupMembership.create!(user: user, user_group: user_group, role: :requested)
    end

    def already_member?
      Decidim::UserGroupMembership.where(user: user, user_group: user_group).any?
    end

    def send_notification
      Decidim::EventsManager.publish(
        event: "decidim.events.groups.join_request_created",
        event_class: JoinRequestCreatedEvent,
        resource: user_group,
        affected_users: user_group.managers,
        extra: {
          user_group_name: user_group.name,
          user_group_nickname: 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/join_user_group.rb
decidim-core-0.27.8 app/commands/decidim/join_user_group.rb
decidim-core-0.27.7 app/commands/decidim/join_user_group.rb
decidim-core-0.27.6 app/commands/decidim/join_user_group.rb
decidim-core-0.27.5 app/commands/decidim/join_user_group.rb
decidim-core-0.27.4 app/commands/decidim/join_user_group.rb
decidim-core-0.27.3 app/commands/decidim/join_user_group.rb
decidim-core-0.27.2 app/commands/decidim/join_user_group.rb
decidim-core-0.27.1 app/commands/decidim/join_user_group.rb
decidim-core-0.27.0 app/commands/decidim/join_user_group.rb
decidim-core-0.27.0.rc2 app/commands/decidim/join_user_group.rb
decidim-core-0.27.0.rc1 app/commands/decidim/join_user_group.rb