Sha256: 64022b881b6eaaccc58320d7586c88c05d6a276db3756bc83bdd4f5483467b64

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true
class Zendesk2::CreateMembership
  include Zendesk2::Request

  request_method :post
  request_path { |r| "/users/#{r.user_id}/organization_memberships.json" }
  request_body { |r| { 'organization_membership' => r.membership_params } }

  def self.accepted_params
    %w(user_id organization_id default)
  end

  def membership_params
    @_membership_params ||= Cistern::Hash.slice(params.fetch('membership'), *self.class.accepted_params)
  end

  def user_id
    params.fetch('membership').fetch('user_id').to_i
  end

  def organization_id
    params.fetch('membership').fetch('organization_id').to_i
  end

  def mock
    user = find!(:users, user_id)
    find!(:organizations, organization_id,
          error: :invalid,
          details: {
            'organization' => [{ 'description' => 'Organization cannot be blank' }],
          })

    if data[:memberships].values.find { |m| m['user_id'] == user_id && m['organization_id'] == organization_id }
      error!(:invalid, description: { 'user_id' => [{ 'description' => 'User has already been taken' }] })
    end

    resource_id = cistern.serial_id

    default_membership = !data[:memberships].values.find { |m| m['user_id'] == user_id && m['default'] }

    resource = {
      'id'              => resource_id,
      'user_id'         => user_id,
      'organization_id' => organization_id,
      'default'         => default_membership,
    }

    data[:memberships][resource_id] = resource

    primary_organization = data[:memberships].values.find { |m| m['user_id'] == user_id && m['default'] }

    if primary_organization
      user['organization_id'] = primary_organization['organization_id']
    end

    mock_response('organization_membership' => resource)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
zendesk2-1.15.0 lib/zendesk2/create_membership.rb
zendesk2-1.13.1 lib/zendesk2/create_membership.rb
zendesk2-1.13.0 lib/zendesk2/create_membership.rb
zendesk2-1.12.0 lib/zendesk2/create_membership.rb
zendesk2-1.11.0 lib/zendesk2/create_membership.rb
zendesk2-1.10.0 lib/zendesk2/create_membership.rb