Class: AsyncTransaction::Vet360::Base

Inherits:
Base show all
Defined in:
app/models/async_transaction/vet360/base.rb

Constant Summary collapse

FINAL_STATUSES =
%w[
  REJECTED
  COMPLETED_SUCCESS
  COMPLETED_NO_CHANGES_DETECTED
  COMPLETED_FAILURE
].freeze
REQUESTED =
'requested'
COMPLETED =
'completed'

Constants inherited from Base

Base::DELETE_COMPLETED_AFTER

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

Returns:



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.get_permission_transaction_status(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

Parameters:

  • user_uuid (String)

    the user’s UUID

  • transaction_id (String)

    the transaction UUID

Returns:



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

Returns:

  • (Array)

    an array of any outstanding transactions



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

Parameters:

Returns:



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

Parameters:

Returns:

  • (Array)

    An array with any outstanding transactions refreshed. Empty if none.



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

Parameters:

Returns:



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.,
    source_id: user.vet360_id,
    source: 'vet360',
    status: REQUESTED,
    transaction_id: response.transaction.id,
    transaction_status: response.transaction.status,
    metadata: response.transaction.messages
  )
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.messages
  transaction_record.save!
  transaction_record
end

Instance Method Details

#finished?Boolean

Note:

this checks transaction_status status fields, which should be redundant

Returns true or false if a transaction is “over”

Returns:

  • (Boolean)

    true if status 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)

Returns:

  • (Boolean)


128
129
130
# File 'app/models/async_transaction/vet360/base.rb', line 128

def initialize_person?
  type&.constantize == AsyncTransaction::Vet360::InitializePersonTransaction
end