Sha256: e6e039ab5cce1c94d2ca9645b9ea9bb5832cdccac19d0b9effc1b4073c1408cb

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

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

  request_method :put
  request_path { |r| "/organizations/#{r.organization_id}.json" }
  request_body { |r| { 'organization' => r.organization_params } }

  def organization_params
    @_organization_params ||= Cistern::Hash.slice(organization, *Zendesk2::CreateOrganization.accepted_attributes)
  end

  def organization
    params.fetch('organization')
  end

  def organization_id
    organization.fetch('id').to_i
  end

  def mock
    record = find!(:organizations, organization_id)

    other_organizations = cistern.data[:organizations].dup
    other_organizations.delete(organization_id)

    other_named_organization = other_organizations.values.find do |o|
      o['name'].casecmp(organization['name'].to_s.downcase).zero?
    end

    if organization['name'] && other_named_organization
      error!(:invalid, details: { 'name' => [{ 'description' => 'Name: has already been taken' }] })
    end

    other_external_organization = other_organizations.values.find do |o|
      o['external_id'].to_s.casecmp(organization['external_id'].to_s.downcase).zero?
    end

    if organization['external_id'] && other_external_organization
      error!(:invalid, details: { 'name' => [{ 'description' => 'External has already been taken' }] })
    end

    record.merge!(organization_params)

    mock_response('organization' => record)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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