lib/zendesk2/update_organization.rb in zendesk2-1.9.0 vs lib/zendesk2/update_organization.rb in zendesk2-1.10.0
- old
+ new
@@ -1,38 +1,47 @@
+# 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 } }
+ 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")
+ params.fetch('organization')
end
def organization_id
- organization.fetch("id").to_i
+ organization.fetch('id').to_i
end
def mock
- record = self.find!(:organizations, organization_id)
+ record = find!(:organizations, organization_id)
other_organizations = cistern.data[:organizations].dup
other_organizations.delete(organization_id)
- if organization["name"] && other_organizations.values.find { |o| o["name"].downcase == organization["name"].downcase }
- error!(:invalid, details: {"name" => [ { "description" => "Name: has already been taken" } ]})
+ other_named_organization = other_organizations.values.find do |o|
+ o['name'].casecmp(organization['name'].to_s.downcase).zero?
end
- if organization["external_id"] && other_organizations.values.find { |o| o["external_id"].to_s.downcase == organization["external_id"].to_s.downcase }
- error!(:invalid, details: {"name" => [ { "description" => "External has already been taken" } ]})
+ 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)
+ mock_response('organization' => record)
end
end