Class: VAProfile::ContactInformation::Service

Inherits:
Service show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/va_profile/contact_information/service.rb

Constant Summary collapse

CONTACT_INFO_CHANGE_TEMPLATE =
Settings.vanotify.services.va_gov.template_id.contact_info_change
EMAIL_PERSONALISATIONS =
{
  address: 'Address',
  residence_address: 'Home address',
  correspondence_address: 'Mailing address',
  email: 'Email address',
  phone: 'Phone number',
  home_phone: 'Home phone number',
  mobile_phone: 'Mobile phone number',
  work_phone: 'Work phone number'
}.freeze

Constants inherited from Service

Service::STATSD_KEY_PREFIX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Service

breakers_service, #final_failure?, #handle_error, #initialize, #log_dates, #parse_messages, #perform, #person_transaction?, #person_transaction_failure?, #raise_backend_exception, #raise_invalid_body, #report_stats_on, #save_error_details

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

This class inherits a constructor from VAProfile::Service

Class Method Details

.get_person(vet360_id) ⇒ Object



58
59
60
61
# File 'lib/va_profile/contact_information/service.rb', line 58

def self.get_person(vet360_id)
  stub_user = OpenStruct.new(vet360_id:)
  new(stub_user).get_person
end

Instance Method Details

#get_address_transaction_status(transaction_id) ⇒ VAProfile::ContactInformation::EmailTransactionResponse

GET’s the status of an address transaction from the VAProfile api

Parameters:

  • transaction_id (int)

    the transaction_id to check

Returns:



120
121
122
123
124
125
126
127
128
# File 'lib/va_profile/contact_information/service.rb', line 120

def get_address_transaction_status(transaction_id)
  route = "#{@user.vet360_id}/addresses/status/#{transaction_id}"
  transaction_status = get_transaction_status(route, AddressTransactionResponse)

  changes = transaction_status.changed_field
  send_contact_change_notification(transaction_status, changes)

  transaction_status
end

#get_email_personalisation(type) ⇒ Object (private)



246
247
248
# File 'lib/va_profile/contact_information/service.rb', line 246

def get_email_personalisation(type)
  { 'contact_info' => EMAIL_PERSONALISATIONS[type] }
end

#get_email_transaction_status(transaction_id) ⇒ VAProfile::ContactInformation::EmailTransactionResponse

GET’s the status of an email transaction from the VAProfile api

Parameters:

  • transaction_id (int)

    the transaction_id to check

Returns:



159
160
161
162
163
164
165
166
# File 'lib/va_profile/contact_information/service.rb', line 159

def get_email_transaction_status(transaction_id)
  route = "#{@user.vet360_id}/emails/status/#{transaction_id}"
  transaction_status = get_transaction_status(route, EmailTransactionResponse)

  send_email_change_notification(transaction_status)

  transaction_status
end

#get_permission_transaction_status(transaction_id) ⇒ VAProfile::ContactInformation::PermissionTransactionResponse

GET’s the status of a permission transaction from the VAProfile api

Parameters:

  • transaction_id (int)

    the transaction_id to check

Returns:



214
215
216
217
# File 'lib/va_profile/contact_information/service.rb', line 214

def get_permission_transaction_status(transaction_id)
  route = "#{@user.vet360_id}/permissions/status/#{transaction_id}"
  get_transaction_status(route, PermissionTransactionResponse)
end

#get_personVAProfile::ContactInformation::PersonResponse

GET’s a Person bio from the VAProfile API If a user is not found in VAProfile, an empty PersonResponse with a 404 status will be returned

Returns:



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
# File 'lib/va_profile/contact_information/service.rb', line 32

def get_person
  with_monitoring do
    vet360_id_present!
    raw_response = perform(:get, @user.vet360_id)

    PersonResponse.from(raw_response)
  end
rescue Common::Client::Errors::ClientError => e
  if e.status == 404
    log_exception_to_sentry(
      e,
      { vet360_id: @user.vet360_id },
      { va_profile: :person_not_found },
      :warning
    )

    return PersonResponse.new(404, person: nil)
  elsif e.status >= 400 && e.status < 500
    return PersonResponse.new(e.status, person: nil)
  end

  handle_error(e)
rescue => e
  handle_error(e)
end

#get_person_transaction_status(transaction_id) ⇒ VAProfile::ContactInformation::PersonTransactionResponse

GET’s the status of a person transaction from the VAProfile api. Does not validate the presence of a vet360_id before making the service call, as POSTing a person initializes a vet360_id.

Parameters:

  • transaction_id (String)

    the transaction_id to check

Returns:



225
226
227
228
229
230
231
232
233
234
# File 'lib/va_profile/contact_information/service.rb', line 225

def get_person_transaction_status(transaction_id)
  with_monitoring do
    raw_response = perform(:get, "status/#{transaction_id}")
    VAProfile::Stats.increment_transaction_results(raw_response, 'init_vet360_id')

    VAProfile::ContactInformation::PersonTransactionResponse.from(raw_response, @user)
  end
rescue => e
  handle_error(e)
end

#get_telephone_transaction_status(transaction_id) ⇒ VAProfile::ContactInformation::TelephoneTransactionResponse

GET’s the status of a telephone transaction from the VAProfile api

Parameters:

  • transaction_id (int)

    the transaction_id to check

Returns:



186
187
188
189
190
191
192
193
194
# File 'lib/va_profile/contact_information/service.rb', line 186

def get_telephone_transaction_status(transaction_id)
  route = "#{@user.vet360_id}/telephones/status/#{transaction_id}"
  transaction_status = get_transaction_status(route, TelephoneTransactionResponse)

  changes = transaction_status.changed_field
  send_contact_change_notification(transaction_status, changes)

  transaction_status
end

#get_transaction_status(path, response_class) ⇒ Object (private)



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/va_profile/contact_information/service.rb', line 311

def get_transaction_status(path, response_class)
  with_monitoring do
    vet360_id_present!
    raw_response = perform(:get, path)
    VAProfile::Stats.increment_transaction_results(raw_response)

    response_class.from(raw_response)
  end
rescue => e
  handle_error(e)
end

#post_address(address) ⇒ VAProfile::ContactInformation::AddressTransactionResponse

POSTs a new address to the VAProfile API

Parameters:

Returns:



106
107
108
# File 'lib/va_profile/contact_information/service.rb', line 106

def post_address(address)
  post_or_put_data(:post, address, 'addresses', AddressTransactionResponse)
end

#post_email(email) ⇒ VAProfile::ContactInformation::EmailTransactionResponse

POSTs a new address to the VAProfile API

Parameters:

Returns:



133
134
135
# File 'lib/va_profile/contact_information/service.rb', line 133

def post_email(email)
  post_or_put_data(:post, email, 'emails', EmailTransactionResponse)
end

#post_or_put_data(method, model, path, response_class) ⇒ Object (private)



300
301
302
303
304
305
306
307
308
309
# File 'lib/va_profile/contact_information/service.rb', line 300

def post_or_put_data(method, model, path, response_class)
  with_monitoring do
    vet360_id_present!
    raw_response = perform(method, path, model.in_json)

    response_class.from(raw_response)
  end
rescue => e
  handle_error(e)
end

#post_permission(permission) ⇒ VAProfile::ContactInformation::PermissionUpdateResponse

POSTs a new permission to the VAProfile API

Parameters:

Returns:

  • (VAProfile::ContactInformation::PermissionUpdateResponse)

    response wrapper around a transaction object



199
200
201
# File 'lib/va_profile/contact_information/service.rb', line 199

def post_permission(permission)
  post_or_put_data(:post, permission, 'permissions', PermissionTransactionResponse)
end

#post_telephone(telephone) ⇒ VAProfile::ContactInformation::TelephoneUpdateResponse

POSTs a new telephone to the VAProfile API

Parameters:

Returns:

  • (VAProfile::ContactInformation::TelephoneUpdateResponse)

    response wrapper around a transaction object



171
172
173
# File 'lib/va_profile/contact_information/service.rb', line 171

def post_telephone(telephone)
  post_or_put_data(:post, telephone, 'telephones', TelephoneTransactionResponse)
end

#put_address(address) ⇒ VAProfile::ContactInformation::AddressTransactionResponse

PUTs an updated address to the VAProfile API

Parameters:

Returns:



113
114
115
# File 'lib/va_profile/contact_information/service.rb', line 113

def put_address(address)
  post_or_put_data(:put, address, 'addresses', AddressTransactionResponse)
end

#put_email(email) ⇒ VAProfile::ContactInformation::EmailTransactionResponse

PUTs an updated address to the VAProfile API

Parameters:

Returns:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/va_profile/contact_information/service.rb', line 140

def put_email(email)
  old_email =
    begin
      @user.va_profile_email
    rescue
      nil
    end

  response = post_or_put_data(:put, email, 'emails', EmailTransactionResponse)

  transaction = response.transaction
  OldEmail.create(transaction_id: transaction.id, email: old_email) if transaction.received? && old_email.present?

  response
end

#put_permission(permission) ⇒ VAProfile::ContactInformation::PermissionUpdateResponse

PUTs an updated permission to the VAProfile API

Parameters:

Returns:

  • (VAProfile::ContactInformation::PermissionUpdateResponse)

    response wrapper around a transaction object



206
207
208
# File 'lib/va_profile/contact_information/service.rb', line 206

def put_permission(permission)
  post_or_put_data(:put, permission, 'permissions', PermissionTransactionResponse)
end

#put_telephone(telephone) ⇒ VAProfile::ContactInformation::TelephoneUpdateResponse

PUTs an updated telephone to the VAProfile API

Parameters:

Returns:

  • (VAProfile::ContactInformation::TelephoneUpdateResponse)

    response wrapper around a transaction object



178
179
180
# File 'lib/va_profile/contact_information/service.rb', line 178

def put_telephone(telephone)
  post_or_put_data(:put, telephone, 'telephones', TelephoneTransactionResponse)
end

#send_contact_change_notification(transaction_status, personalisation) ⇒ Object (private)



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/va_profile/contact_information/service.rb', line 250

def send_contact_change_notification(transaction_status, personalisation)
  return unless Flipper.enabled?(:contact_info_change_email, @user)

  transaction = transaction_status.transaction

  if transaction.completed_success?
    transaction_id = transaction.id
    return if TransactionNotification.find(transaction_id).present?

    email = @user.va_profile_email
    return if email.blank?

    VANotifyEmailJob.perform_async(
      email,
      CONTACT_INFO_CHANGE_TEMPLATE,
      get_email_personalisation(personalisation)
    )

    TransactionNotification.create(transaction_id:)
  end
end

#send_email_change_notification(transaction_status) ⇒ Object (private)



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/va_profile/contact_information/service.rb', line 272

def send_email_change_notification(transaction_status)
  return unless Flipper.enabled?(:contact_info_change_email, @user)

  transaction = transaction_status.transaction

  if transaction.completed_success?
    old_email = OldEmail.find(transaction.id)
    return if old_email.nil?

    personalisation = get_email_personalisation(:email)

    VANotifyEmailJob.perform_async(old_email.email, CONTACT_INFO_CHANGE_TEMPLATE, personalisation)
    if transaction_status.new_email.present?
      VANotifyEmailJob.perform_async(
        transaction_status.new_email,
        CONTACT_INFO_CHANGE_TEMPLATE,
        personalisation
      )
    end

    old_email.destroy
  end
end

#update_address(address) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/va_profile/contact_information/service.rb', line 63

def update_address(address)
  address_type =
    if address.address_pou == VAProfile::Models::BaseAddress::RESIDENCE
      'residential'
    else
      'mailing'
    end

  update_model(address, "#{address_type}_address", 'address')
end

#update_email(email) ⇒ Object



74
75
76
# File 'lib/va_profile/contact_information/service.rb', line 74

def update_email(email)
  update_model(email, 'email', 'email')
end

#update_model(model, attr, method_name) ⇒ Object (private)



238
239
240
241
242
243
244
# File 'lib/va_profile/contact_information/service.rb', line 238

def update_model(model, attr, method_name)
  contact_info = VAProfileRedis::ContactInformation.for_user(@user)
  model.id = contact_info.public_send(attr)&.id
  verb = model.id.present? ? 'put' : 'post'

  public_send("#{verb}_#{method_name}", model)
end

#update_permission(permission) ⇒ Object



78
79
80
# File 'lib/va_profile/contact_information/service.rb', line 78

def update_permission(permission)
  update_model(permission, 'text_permission', 'permission')
end

#update_telephone(telephone) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/va_profile/contact_information/service.rb', line 82

def update_telephone(telephone)
  phone_type =
    case telephone.phone_type
    when VAProfile::Models::Telephone::MOBILE
      'mobile_phone'
    when VAProfile::Models::Telephone::HOME
      'home_phone'
    when VAProfile::Models::Telephone::WORK
      'work_phone'
    when VAProfile::Models::Telephone::FAX
      'fax_number'
    when VAProfile::Models::Telephone::TEMPORARY
      'temporary_phone'
    else
      raise 'invalid phone type'
    end

  update_model(telephone, phone_type, 'telephone')
end

#vet360_id_present!Object (private)



296
297
298
# File 'lib/va_profile/contact_information/service.rb', line 296

def vet360_id_present!
  raise 'User does not have a vet360_id' if @user&.vet360_id.blank?
end