Module: ClientSuccess::Contact

Extended by:
Contact
Included in:
Contact
Defined in:
lib/client_success/contact.rb

Defined Under Namespace

Classes: Error, NotFound

Instance Method Summary collapse

Instance Method Details

#create(client_id:, attributes:, connection:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/client_success/contact.rb', line 42

def create(client_id:, attributes:, connection:)
  body = Schema::Contact::Create[attributes]
    .transform_keys { |k| k.to_s.camelize(:lower) }
    .to_json

  response = connection.post(
    "/v1/clients/#{client_id}/contacts", body)

  payload = response.body

  DomainModel::Contact.new(
    payload.deep_transform_keys(&:underscore))
end

#delete(id:, client_id:, connection:) ⇒ Object



69
70
71
# File 'lib/client_success/contact.rb', line 69

def delete(id:, client_id:, connection:)
  connection.delete("/v1/clients/#{client_id}/contacts/#{id}")
end

#get_details(client_id:, contact_id:, connection:) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/client_success/contact.rb', line 32

def get_details(client_id:, contact_id:, connection:)
  response = connection.get(
    "/v1/clients/#{client_id}/contacts/#{contact_id}/details")

  payload = response.body

  DomainModel::Contact.new(
    payload.deep_transform_keys(&:underscore))
end

#get_details_by_client_external_id_and_email(client_external_id:, email:, connection:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/client_success/contact.rb', line 79

def get_details_by_client_external_id_and_email(client_external_id:, email:, connection:)
  params = {
    "clientExternalId" => client_external_id,
    "email" => email
  }

  response = connection.get(
    "/v1/contacts?#{params.compact.to_query}")

  # for some reason the ClientSuccess API does not return a 404
  # but instead a body containing the string "null" if the contact is not found
  # this is probably a security restriction on their end, but we will instead raise an error

  if response.body.blank?
    raise NotFound, "contact with email '#{email}' not found on client '#{client_external_id}'"
  else
    payload = response.body
    DomainModel::Contact.new(payload.deep_transform_keys(&:underscore))
  end
end

#get_summary(client_id:, contact_id:, connection:) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/client_success/contact.rb', line 22

def get_summary(client_id:, contact_id:, connection:)
  response = connection.get(
    "/v1/clients/#{client_id}/contacts/#{contact_id}")

  payload = response.body

  DomainModel::Contact.new(
    payload.deep_transform_keys(&:underscore))
end

#list_all(client_id:, connection:) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/client_success/contact.rb', line 12

def list_all(client_id:, connection:)
  response = connection.get(
    "/v1/clients/#{client_id}/contacts")

  response.body.map do |payload|
    DomainModel::Contact.new(
      payload.deep_transform_keys(&:underscore))
  end
end

#search(term:, connection:) ⇒ Object



100
101
102
# File 'lib/client_success/contact.rb', line 100

def search(term:, connection:)
  connection.get("/v1/contacts/search?term='#{term}'").body
end

#search_by_email(email:, connection:) ⇒ Object



73
74
75
76
77
# File 'lib/client_success/contact.rb', line 73

def search_by_email(email:, connection:)
  search(term: email, connection: connection)
    .reject { |contact| contact["email"].nil? }
    .select { |contact| contact["email"].downcase == email.downcase }
end

#update(id:, client_id:, attributes:, connection:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/client_success/contact.rb', line 56

def update(id:, client_id:, attributes:, connection:)
  body = Schema::Contact::Update[attributes]
    .transform_keys { |k| k.to_s.camelize(:lower) }
    .to_json

  response = connection.put("/v1/clients/#{client_id}/contacts/#{id}/details", body)

  payload = response.body

  DomainModel::Contact.new(
    payload.deep_transform_keys(&:underscore))
end