Class: Zendesk2::CreateUser

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/zendesk2/create_user.rb

Instance Attribute Summary

Attributes included from Request

#params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#call, cistern_included, #data, #delete!, #error!, #find!, #html_url_for, included, #mock_response, #page, #page_params!, #page_params?, #pluralize, #real, #real_request, #request_body, #request_params, #request_path, #resources, #response, #timestamp, #url_for

Class Method Details

.accepted_attributesObject



9
10
11
12
# File 'lib/zendesk2/create_user.rb', line 9

def self.accepted_attributes
  %w(name email organization_id external_id alias verified locate_id time_zone phone signature details notes role
     custom_role_id moderator ticket_restriction only_private_comments user_fields)
end

Instance Method Details

#mockObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zendesk2/create_user.rb', line 18

def mock
  user_id = cistern.serial_id

  user = params.fetch('user')
  organization_id = user['organization_id']
  organization_id && find!(:organizations, organization_id)

  record = {
    'id'         => user_id,
    'url'        => url_for("/users/#{user_id}.json"),
    'created_at' => timestamp,
    'updated_at' => timestamp,
    'role'       => 'end-user',
    'active'     => true,
  }.merge(user_params)

  external_id = record['external_id']
  matching_external_id = external_id && data[:users].values.find do |o|
    o['external_id'].to_s.casecmp(external_id.to_s.downcase).zero?
  end

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

  email = record['email']
  matching_identity = email && data[:identities].values.find do |i|
    i['type'] == 'email' && i['value'].to_s.casecmp(email.downcase).zero?
  end

  if matching_identity
    error!(:invalid, details: {
             'email' => [{
               'description' => "Email: #{email} is already being used by another user",
             },],
           })
  end

  user_identity_id = cistern.serial_id

  user_identity = {
    'id'         => user_identity_id,
    'url'        => url_for("/users/#{user_id}/identities/#{user_identity_id}.json"),
    'created_at' => timestamp,
    'updated_at' => timestamp,
    'type'       => 'email',
    'value'      => record['email'],
    'verified'   => false,
    'primary'    => true,
    'user_id'    => user_id,
  }

  data[:identities][user_identity_id] = user_identity
  data[:users][user_id] = record.reject { |k, _v| k == 'email' }

  if organization_id
    cistern.create_membership(
      'membership' => { 'user_id' => user_id, 'organization_id' => organization_id, 'default' => true }
    )
  end

  mock_response({ 'user' => record }, { status: 201 })
end

#user_paramsObject



14
15
16
# File 'lib/zendesk2/create_user.rb', line 14

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