Class: VAProfile::V2::ContactInformation::Service
- Inherits:
-
Service
- Object
- Common::Client::Base
- Service
- VAProfile::V2::ContactInformation::Service
- Includes:
- Common::Client::Concerns::Monitoring
- Defined in:
- lib/va_profile/v2/contact_information/service.rb
Constant Summary collapse
- CONTACT_INFO_CHANGE_TEMPLATE =
Settings.vanotify.services.va_gov.template_id.contact_info_change
- VA_PROFILE_ID_POSTFIX =
'^PI^200VETS^USDVA'
- 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
Class Method Summary collapse
Instance Method Summary collapse
-
#get_address_transaction_status(transaction_id) ⇒ VAProfile::V2::ContactInformation::EmailTransactionResponse
GET’s the status of an address transaction from the VAProfile api.
- #get_email_personalisation(type) ⇒ Object private
-
#get_email_transaction_status(transaction_id) ⇒ VAProfile::V2::ContactInformation::EmailTransactionResponse
GET’s the status of an email transaction from the VAProfile api.
-
#get_person ⇒ VAProfile::V2::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.
-
#get_person_transaction_status(transaction_id) ⇒ VAProfile::V2::ContactInformation::PersonTransactionResponse
GET’s the status of a person transaction from the VAProfile api.
-
#get_telephone_transaction_status(transaction_id) ⇒ VAProfile::V2::ContactInformation::TelephoneTransactionResponse
GET’s the status of a telephone transaction from the VAProfile api.
- #get_transaction_status(path, response_class) ⇒ Object private
- #icn_with_aaid ⇒ Object private
-
#post_address(address) ⇒ VAProfile::V2::ContactInformation::AddressTransactionResponse
POSTs a new address to the VAProfile API.
-
#post_email(email) ⇒ VAProfile::V2::ContactInformation::EmailTransactionResponse
POSTs a new address to the VAProfile API.
- #post_or_put_data(method, model, path, response_class) ⇒ Object private
-
#post_telephone(telephone) ⇒ VAProfile::V2::ContactInformation::TelephoneUpdateResponse
POSTs a new telephone to the VAProfile API.
-
#put_address(address) ⇒ VAProfile::V2::ContactInformation::AddressTransactionResponse
PUTs an updated address to the VAProfile API.
-
#put_email(email) ⇒ VAProfile::V2::ContactInformation::EmailTransactionResponse
PUTs an updated address to the VAProfile API.
-
#put_telephone(telephone) ⇒ VAProfile::V2::ContactInformation::TelephoneUpdateResponse
PUTs an updated telephone to the VAProfile API.
- #send_contact_change_notification(transaction_status, personalisation) ⇒ Object private
- #send_email_change_notification(transaction_status) ⇒ Object private
- #update_address(address) ⇒ Object
- #update_email(email) ⇒ Object
- #update_model(model, attr, method_name) ⇒ Object private
- #update_telephone(telephone) ⇒ Object
- #vet360_id ⇒ Object private
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
59 60 61 62 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 59 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::V2::ContactInformation::EmailTransactionResponse
GET’s the status of an address transaction from the VAProfile api
117 118 119 120 121 122 123 124 125 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 117 def get_address_transaction_status(transaction_id) route = "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)
231 232 233 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 231 def get_email_personalisation(type) { 'contact_info' => EMAIL_PERSONALISATIONS[type] } end |
#get_email_transaction_status(transaction_id) ⇒ VAProfile::V2::ContactInformation::EmailTransactionResponse
GET’s the status of an email transaction from the VAProfile api
159 160 161 162 163 164 165 166 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 159 def get_email_transaction_status(transaction_id) route = "emails/status/#{transaction_id}" transaction_status = get_transaction_status(route, EmailTransactionResponse) send_email_change_notification(transaction_status) transaction_status end |
#get_person ⇒ VAProfile::V2::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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 35 def get_person with_monitoring do raw_response = perform(:get, "#{MPI::Constants::VA_ROOT_OID}/#{ERB::Util.url_encode(icn_with_aaid)}") PersonResponse.from(raw_response) end rescue Common::Client::Errors::ClientError => e if e.status == 404 log_exception_to_sentry( e, { 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::V2::ContactInformation::PersonTransactionResponse
GET’s the status of a person transaction from the VAProfile api. Does not validate the presence of user’s icn before making the service call, as POSTing a person initializes a icn.
202 203 204 205 206 207 208 209 210 211 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 202 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_va_profile') VAProfile::V2::ContactInformation::PersonTransactionResponse.from(raw_response, @user) end rescue => e handle_error(e) end |
#get_telephone_transaction_status(transaction_id) ⇒ VAProfile::V2::ContactInformation::TelephoneTransactionResponse
GET’s the status of a telephone transaction from the VAProfile api
186 187 188 189 190 191 192 193 194 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 186 def get_telephone_transaction_status(transaction_id) route = "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)
288 289 290 291 292 293 294 295 296 297 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 288 def get_transaction_status(path, response_class) with_monitoring do raw_response = perform(:get, path) VAProfile::Stats.increment_transaction_results(raw_response) response_class.from(raw_response) end rescue => e handle_error(e) end |
#icn_with_aaid ⇒ Object (private)
215 216 217 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 215 def icn_with_aaid "#{@user.icn}^NI^200M^USVHA" end |
#post_address(address) ⇒ VAProfile::V2::ContactInformation::AddressTransactionResponse
POSTs a new address to the VAProfile API
103 104 105 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 103 def post_address(address) post_or_put_data(:post, address, 'addresses', AddressTransactionResponse) end |
#post_email(email) ⇒ VAProfile::V2::ContactInformation::EmailTransactionResponse
POSTs a new address to the VAProfile API
130 131 132 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 130 def post_email(email) post_or_put_data(:post, email, 'emails', EmailTransactionResponse) end |
#post_or_put_data(method, model, path, response_class) ⇒ Object (private)
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 277 def post_or_put_data(method, model, path, response_class) with_monitoring do request_path = "#{MPI::Constants::VA_ROOT_OID}/#{ERB::Util.url_encode(icn_with_aaid)}" + "/#{path}" # in_json method should replace in_json_v2 after Contact Information V1 has depreciated raw_response = perform(method, request_path, model.in_json_v2) response_class.from(raw_response) end rescue => e handle_error(e) end |
#post_telephone(telephone) ⇒ VAProfile::V2::ContactInformation::TelephoneUpdateResponse
POSTs a new telephone to the VAProfile API
171 172 173 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 171 def post_telephone(telephone) post_or_put_data(:post, telephone, 'telephones', TelephoneTransactionResponse) end |
#put_address(address) ⇒ VAProfile::V2::ContactInformation::AddressTransactionResponse
PUTs an updated address to the VAProfile API
110 111 112 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 110 def put_address(address) post_or_put_data(:put, address, 'addresses', AddressTransactionResponse) end |
#put_email(email) ⇒ VAProfile::V2::ContactInformation::EmailTransactionResponse
PUTs an updated address to the VAProfile API
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 137 def put_email(email) old_email = begin @user.va_profile_v2_email rescue nil end response = post_or_put_data(:put, email, 'emails', EmailTransactionResponse) transaction = response.transaction if transaction.received? && old_email.present? OldEmail.create(transaction_id: transaction.id, email: old_email) end response end |
#put_telephone(telephone) ⇒ VAProfile::V2::ContactInformation::TelephoneUpdateResponse
PUTs an updated telephone to the VAProfile API
178 179 180 |
# File 'lib/va_profile/v2/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)
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 235 def send_contact_change_notification(transaction_status, personalisation) transaction = transaction_status.transaction if transaction.completed_success? transaction_id = transaction.id return if TransactionNotification.find(transaction_id).present? email = @user.va_profile_v2_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)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 255 def send_email_change_notification(transaction_status) 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
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 64 def update_address(address) address_type = if address.address_pou == VAProfile::Models::V3::BaseAddress::RESIDENCE 'residential' else 'mailing' end update_model(address, "#{address_type}_address", 'address') end |
#update_email(email) ⇒ Object
75 76 77 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 75 def update_email(email) update_model(email, 'email', 'email') end |
#update_model(model, attr, method_name) ⇒ Object (private)
223 224 225 226 227 228 229 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 223 def update_model(model, attr, method_name) contact_info = VAProfileRedis::V2::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_telephone(telephone) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 79 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 ⇒ Object (private)
219 220 221 |
# File 'lib/va_profile/v2/contact_information/service.rb', line 219 def vet360_id @user.vet360_id end |