Class: AsyncTransaction::VAProfile::Base
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- ApplicationRecord
- Base
- AsyncTransaction::VAProfile::Base
- Defined in:
- app/models/async_transaction/va_profile/base.rb
Direct Known Subclasses
AddressTransaction, EmailTransaction, InitializePersonTransaction, PermissionTransaction, TelephoneTransaction
Constant Summary collapse
- FINAL_STATUSES =
%w[ REJECTED COMPLETED_SUCCESS COMPLETED_NO_CHANGES_DETECTED COMPLETED_FAILURE ].freeze
- REQUESTED =
'requested'
- COMPLETED =
'completed'
Constants inherited from Base
Class Method Summary collapse
-
.fetch_transaction(transaction_record, service) ⇒ VAProfile::Models::Transaction
Requests a transaction from VAProfile for an app transaction.
-
.find_transaction!(user_uuid, transaction_id) ⇒ AddressTransaction, ...
Finds a transaction by transaction_id for a user.
-
.last_ongoing_transactions_for_user(user) ⇒ Array
Find the most recent address, email, or telelphone transactions for a user.
-
.last_requested_for_user(user) ⇒ AsyncTransaction::VAProfile::Base
Get most recent requested transaction for a user.
-
.refresh_transaction_status(user, service, tx_id = nil) ⇒ AsyncTransaction::VAProfile::Base
Updates the status and transaction_status with fresh API data.
-
.refresh_transaction_statuses(user, service) ⇒ Array
Wrapper for .refresh_transaction_status which finds any outstanding transactions for a user and refreshes them.
-
.start(user, response) ⇒ AsyncTransaction::VAProfile::Base
Creates an initial AsyncTransaction record for ongoing tracking.
- .update_transaction_from_api(transaction_record, api_response) ⇒ Object
Instance Method Summary collapse
-
#finished? ⇒ Boolean
Returns true or false if a transaction is “over”.
Methods inherited from Base
#parsed_metadata, #serialize_metadata
Methods inherited from ApplicationRecord
descendants_using_encryption, lockbox_options, #timestamp_attributes_for_update_in_model, #valid?
Class Method Details
.fetch_transaction(transaction_record, service) ⇒ VAProfile::Models::Transaction
Requests a transaction from VAProfile for an app transaction
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/async_transaction/va_profile/base.rb', line 75 def self.fetch_transaction(transaction_record, service) case transaction_record when AsyncTransaction::Vet360::AddressTransaction, AsyncTransaction::VAProfile::AddressTransaction service.get_address_transaction_status(transaction_record.transaction_id) when AsyncTransaction::Vet360::EmailTransaction, AsyncTransaction::VAProfile::EmailTransaction service.get_email_transaction_status(transaction_record.transaction_id) when AsyncTransaction::Vet360::TelephoneTransaction, AsyncTransaction::VAProfile::TelephoneTransaction service.get_telephone_transaction_status(transaction_record.transaction_id) when AsyncTransaction::Vet360::PermissionTransaction, AsyncTransaction::VAProfile::PermissionTransaction service.(transaction_record.transaction_id) when AsyncTransaction::Vet360::InitializePersonTransaction, AsyncTransaction::VAProfile::InitializePersonTransaction service.get_person_transaction_status(transaction_record.transaction_id) else # Unexpected transaction type means something went sideways raise end end |
.find_transaction!(user_uuid, transaction_id) ⇒ AddressTransaction, ...
Finds a transaction by transaction_id for a user
98 99 100 101 |
# File 'app/models/async_transaction/va_profile/base.rb', line 98 def self.find_transaction!(user_uuid, transaction_id) Base.find_by(user_uuid:, transaction_id:) || AsyncTransaction::Vet360::Base.find_by!(user_uuid:, transaction_id:) end |
.last_ongoing_transactions_for_user(user) ⇒ Array
Find the most recent address, email, or telelphone transactions for a user
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/models/async_transaction/va_profile/base.rb', line 136 def self.last_ongoing_transactions_for_user(user) ongoing_transactions = [] %w[ Address Email Telephone Permission ].each do |transaction_type| ongoing_transactions += "AsyncTransaction::VAProfile::#{transaction_type}Transaction" .constantize .last_requested_for_user(user) end ongoing_transactions end |
.last_requested_for_user(user) ⇒ AsyncTransaction::VAProfile::Base
Get most recent requested transaction for a user
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/models/async_transaction/va_profile/base.rb', line 23 def self.last_requested_for_user(user) transactions = where( status: Base::REQUESTED, user_uuid: user.uuid ).order( created_at: :desc ).limit(1) return to_s.gsub('VAProfile', 'Vet360').constantize.last_requested.for_user(user) if transactions.blank? transactions end |
.refresh_transaction_status(user, service, tx_id = nil) ⇒ AsyncTransaction::VAProfile::Base
Updates the status and transaction_status with fresh API data
62 63 64 65 66 67 68 |
# File 'app/models/async_transaction/va_profile/base.rb', line 62 def self.refresh_transaction_status(user, service, tx_id = nil) transaction_record = find_transaction!(user.uuid, tx_id) return transaction_record if transaction_record.finished? api_response = Base.fetch_transaction(transaction_record, service) update_transaction_from_api(transaction_record, api_response) end |
.refresh_transaction_statuses(user, service) ⇒ Array
Wrapper for .refresh_transaction_status which finds any outstanding transactions
for a user and refreshes them
123 124 125 126 127 128 129 130 131 |
# File 'app/models/async_transaction/va_profile/base.rb', line 123 def self.refresh_transaction_statuses(user, service) last_ongoing_transactions_for_user(user).each_with_object([]) do |transaction, array| array << refresh_transaction_status( user, service, transaction.transaction_id ) end end |
.start(user, response) ⇒ AsyncTransaction::VAProfile::Base
Creates an initial AsyncTransaction record for ongoing tracking
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'app/models/async_transaction/va_profile/base.rb', line 44 def self.start(user, response) create( user_uuid: user.uuid, user_account: user.user_account, source_id: user.vet360_id, source: 'va_profile', status: REQUESTED, transaction_id: response.transaction.id, transaction_status: response.transaction.status, metadata: response.transaction. ) end |
.update_transaction_from_api(transaction_record, api_response) ⇒ Object
103 104 105 106 107 108 109 |
# File 'app/models/async_transaction/va_profile/base.rb', line 103 def self.update_transaction_from_api(transaction_record, api_response) transaction_record.status = COMPLETED if FINAL_STATUSES.include? api_response.transaction.status transaction_record.transaction_status = api_response.transaction.status transaction_record. = api_response.transaction. transaction_record.save! transaction_record end |
Instance Method Details
#finished? ⇒ Boolean
this checks transaction_status status fields, which should be redundant
Returns true or false if a transaction is “over”
114 115 116 |
# File 'app/models/async_transaction/va_profile/base.rb', line 114 def finished? FINAL_STATUSES.include?(transaction_status) || status == COMPLETED end |