Sha256: ce0626430cf1aa37d4b633fa79a540fb1a40f0a9907d72a61a8411bd90305244

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Decidim
  # A command with all the business logic to update a user group profile.
  class UpdateUserGroup < Rectify::Command
    # Public: Initializes the command.
    #
    # form - A form object with the params.
    # user_group - The user group to update
    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?

      was_verified = user_group.verified?
      update_user_group
      notify_admins if was_verified

      broadcast(:ok, user_group)
    end

    private

    attr_reader :form, :user_group

    def update_user_group
      user_group_attributes = attributes
      user_group_attributes[:avatar] = form.avatar if form.avatar.present?
      user_group.update(user_group_attributes)
    end

    def attributes
      {
        email: form.email,
        name: form.name,
        nickname: form.nickname,
        about: form.about,
        avatar: form.avatar,
        extended_data: {
          phone: form.phone,
          document_number: form.document_number
        }
      }
    end

    def notify_admins
      data = {
        event: "decidim.events.groups.user_group_updated",
        event_class: Decidim::UserGroupUpdatedEvent,
        resource: @user_group,
        affected_users: Decidim::User.org_admins_except_me(form.current_user)
      }

      Decidim::EventsManager.publish(data)
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
decidim-core-0.23.6 app/commands/decidim/update_user_group.rb
decidim-core-0.23.5 app/commands/decidim/update_user_group.rb
decidim-core-0.23.4 app/commands/decidim/update_user_group.rb
decidim-core-0.23.3 app/commands/decidim/update_user_group.rb
decidim-core-0.23.2 app/commands/decidim/update_user_group.rb
decidim-core-0.23.1 app/commands/decidim/update_user_group.rb
decidim-core-0.23.1.rc1 app/commands/decidim/update_user_group.rb
decidim-core-0.23.0 app/commands/decidim/update_user_group.rb
decidim-core-0.22.0 app/commands/decidim/update_user_group.rb