Sha256: 368339074c0f7cb67972e7e308146768d79bf03258a8171cb360594538ee37e5

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

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

  request_method :post
  request_path { |r| "/users/#{r.user_id}/identities.json" }
  request_body { |r| { 'identity' => r.user_identity_params } }

  def self.accepted_attributes
    %w(primary verified type value user_id)
  end

  def user_identity_params
    Cistern::Hash.slice(params.fetch('user_identity'), *self.class.accepted_attributes)
  end

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

  def mock
    identity = cistern.serial_id

    record = {
      'id'         => identity,
      'url'        => url_for("/user/#{user_id}/identities/#{identity}.json"),
      'created_at' => timestamp,
      'updated_at' => timestamp,
      'verified'   => false,
      'primary'    => false,
      'user_id'    => user_id,
    }.merge(user_identity_params)

    record.merge('primary' => true) if cistern.data[:identities].values.find { |ui| ui['user_id'] == user_id }.nil?

    if cistern.data[:identities].values.find { |i| i['value'] == record['value'] }
      error!(:invalid, details: {
               'value' => [{ 'description' => "Value: #{record['value']} is already being used by another user" }],
             })
    end

    cistern.data[:identities][identity] = record

    mock_response('identity' => record)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zendesk2-1.15.0 lib/zendesk2/create_user_identity.rb
zendesk2-1.13.1 lib/zendesk2/create_user_identity.rb
zendesk2-1.13.0 lib/zendesk2/create_user_identity.rb
zendesk2-1.12.0 lib/zendesk2/create_user_identity.rb