Class: AsyncTransaction::Vet360::Base
- Inherits:
-
Base
- Object
- ActiveRecord::Base
- ApplicationRecord
- Base
- AsyncTransaction::Vet360::Base
- Defined in:
- app/models/async_transaction/vet360/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 vet360 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.
-
.refresh_transaction_status(user, service, tx_id = nil) ⇒ AsyncTransaction::Vet360::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::Vet360::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”.
- #initialize_person? ⇒ Boolean private
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 vet360 for an app transaction
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/async_transaction/vet360/base.rb', line 58 def self.fetch_transaction(transaction_record, service) case transaction_record when AsyncTransaction::Vet360::AddressTransaction service.get_address_transaction_status(transaction_record.transaction_id) when AsyncTransaction::Vet360::EmailTransaction service.get_email_transaction_status(transaction_record.transaction_id) when AsyncTransaction::Vet360::TelephoneTransaction service.get_telephone_transaction_status(transaction_record.transaction_id) when AsyncTransaction::Vet360::PermissionTransaction service.(transaction_record.transaction_id) when AsyncTransaction::Vet360::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
80 81 82 |
# File 'app/models/async_transaction/vet360/base.rb', line 80 def self.find_transaction!(user_uuid, transaction_id) 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
117 118 119 120 121 122 123 124 |
# File 'app/models/async_transaction/vet360/base.rb', line 117 def self.last_ongoing_transactions_for_user(user) ongoing_transactions = [] ongoing_transactions += AddressTransaction.last_requested.for_user(user) ongoing_transactions += EmailTransaction.last_requested.for_user(user) ongoing_transactions += TelephoneTransaction.last_requested.for_user(user) ongoing_transactions += PermissionTransaction.last_requested.for_user(user) ongoing_transactions end |
.refresh_transaction_status(user, service, tx_id = nil) ⇒ AsyncTransaction::Vet360::Base
Updates the status and transaction_status with fresh API data
45 46 47 48 49 50 51 |
# File 'app/models/async_transaction/vet360/base.rb', line 45 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
104 105 106 107 108 109 110 111 112 |
# File 'app/models/async_transaction/vet360/base.rb', line 104 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::Vet360::Base
Creates an initial AsyncTransaction record for ongoing tracking
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/async_transaction/vet360/base.rb', line 27 def self.start(user, response) create( user_uuid: user.uuid, user_account: user.user_account, source_id: user.vet360_id, source: 'vet360', 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
84 85 86 87 88 89 90 |
# File 'app/models/async_transaction/vet360/base.rb', line 84 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”
95 96 97 |
# File 'app/models/async_transaction/vet360/base.rb', line 95 def finished? FINAL_STATUSES.include?(transaction_status) || status == COMPLETED end |
#initialize_person? ⇒ Boolean (private)
128 129 130 |
# File 'app/models/async_transaction/vet360/base.rb', line 128 def initialize_person? type&.constantize == AsyncTransaction::Vet360::InitializePersonTransaction end |