Sha256: 4aa970207d441205b3f7871baa428293cab68aab4c24380f6c8fb1b5fdd004d2

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to invite a user to a group.
  class InviteUserToGroup < Rectify::Command
    # Public: Initializes the command.
    #
    # form - A form object with the params.
    # user_group - The user group that invites the user
    def initialize(form, user_group)
      @form = form
      @user_group = user_group
    end

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

      transaction do
        invite_user
        send_notification
      end

      broadcast(:ok)
    end

    private

    attr_reader :form, :user_group

    def invite_user
      Decidim::UserGroupMembership.create!(
        user: form.user,
        user_group: user_group,
        role: :invited
      )
    end

    def send_notification
      Decidim::EventsManager.publish(
        event: "decidim.events.groups.invited_to_group",
        event_class: InvitedToGroupEvent,
        resource: user_group,
        recipient_ids: [form.user.id],
        extra: {
          user_group_name: user_group.name,
          user_group_nickname: user_group.nickname
        }
      )
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-core-0.15.2 app/commands/decidim/invite_user_to_group.rb
decidim-core-0.15.1 app/commands/decidim/invite_user_to_group.rb
decidim-core-0.15.0 app/commands/decidim/invite_user_to_group.rb