Class: Hws::PaymentOperationsDemo::TransactionalValueStore
- Inherits:
-
Object
- Object
- Hws::PaymentOperationsDemo::TransactionalValueStore
- Defined in:
- lib/hws/payment_operations_demo/transactional_value_store.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#store_id ⇒ Object
Returns the value of attribute store_id.
-
#transaction_group_id ⇒ Object
Returns the value of attribute transaction_group_id.
Class Method Summary collapse
- .create(name:, description: nil, store_tags: {}, txn_tags: []) ⇒ Object
- .get_instrument_for_entry(entry_id) ⇒ Object
- .load(store_id) ⇒ Object
- .update_txn_status(entry_id, status, tags = {}) ⇒ Object
Instance Method Summary collapse
- #balance ⇒ Object
- #deposit(amount:, tags:, txn_time: Time.now) ⇒ Object
-
#initialize(store_id:, transaction_group_id:) ⇒ TransactionalValueStore
constructor
A new instance of TransactionalValueStore.
- #store ⇒ Object
- #update_txn_status(entry_id, status, tags = {}) ⇒ Object
- #withdraw(amount:, tags:, txn_time: Time.now) ⇒ Object
Constructor Details
#initialize(store_id:, transaction_group_id:) ⇒ TransactionalValueStore
Returns a new instance of TransactionalValueStore.
13 14 15 16 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 13 def initialize(store_id:, transaction_group_id:) @store_id = store_id @transaction_group_id = transaction_group_id end |
Instance Attribute Details
#store_id ⇒ Object
Returns the value of attribute store_id.
6 7 8 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 6 def store_id @store_id end |
#transaction_group_id ⇒ Object
Returns the value of attribute transaction_group_id.
6 7 8 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 6 def transaction_group_id @transaction_group_id end |
Class Method Details
.create(name:, description: nil, store_tags: {}, txn_tags: []) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 57 def create(name:, description: nil, store_tags: {}, txn_tags: []) ActiveRecord::Base.transaction do store_id = Hws::Stores.create_store( { name: name, description: description, data: 0, schema: { type: :number, multipleOf: 0.01 }.with_indifferent_access, tags: } ) transaction_group = Hws::Transactions.create_group( "store_ledger_#{store_id}", "value_store_#{description}", %I[status bank_ref_id].concat(), %I[store_id instrument_id beneficiary remitter txn_time note pymt_type] ) Hws::Stores.update_store(store_id: store_id, tags: { ledger_id: transaction_group.id }) TransactionalValueStore.new(store_id: store_id, transaction_group_id: transaction_group.id) end end |
.get_instrument_for_entry(entry_id) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 95 def get_instrument_for_entry(entry_id) entry = Hws::Transactions.get_entry(entry_id) instrument_id = (entry).try(:[], 'instrument_id') return if instrument_id.nil? Hws::Instruments::Models::Instrument.find_by(id: instrument_id) end |
.load(store_id) ⇒ Object
51 52 53 54 55 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 51 def load(store_id) store = Hws::Stores.get_store(store_id) tg_id = store[:tags]['ledger_id'] TransactionalValueStore.new(store_id: store_id, transaction_group_id: tg_id) end |
.update_txn_status(entry_id, status, tags = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 77 def update_txn_status(entry_id, status, = {}) entry = Hws::Transactions.get_entry(entry_id) return if status == entry['status'] entry = ActiveRecord::Base.transaction do Hws::Transactions.update_entry(entry[:transaction_group_id], entry_id, { status: status }.merge()) if status == 'FAILED' store_id = (entry).try(:[], 'store_id') raise 'couldnot find store corresponding to txn entry' if store_id.nil? Hws::Stores.increment(store_id, (entry[:value] * -1)) end Hws::Transactions.get_entry(entry_id) end end |
Instance Method Details
#balance ⇒ Object
32 33 34 35 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 32 def balance store = Hws::Stores.get_store(@store_id) store[:data] end |
#deposit(amount:, tags:, txn_time: Time.now) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 18 def deposit(amount:, tags:, txn_time: Time.now) entry = ActiveRecord::Base.transaction do Hws::Stores.increment(@store_id, amount) .key?(:immutable_tags) ? [:immutable_tags][:store_id] = store_id : [:immutable_tags] = { store_id: store_id } Hws::Transactions.add_entry(@transaction_group_id, amount, txn_time, ) end entry.id end |
#store ⇒ Object
8 9 10 11 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 8 def store @store ||= Hws::Stores.get_store(@store_id) @store end |
#update_txn_status(entry_id, status, tags = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 37 def update_txn_status(entry_id, status, = {}) entry = Hws::Transactions.get_entry(entry_id) return if status == entry['status'] entry = ActiveRecord::Base.transaction do Hws::Transactions.update_entry(@transaction_group_id, entry_id, { status: status }.merge()) Hws::Stores.increment(@store_id, (entry[:value] * -1)) if entry['status'] != 'FAILED' && status == 'FAILED' Hws::Transactions.get_entry(entry_id) end end |
#withdraw(amount:, tags:, txn_time: Time.now) ⇒ Object
28 29 30 |
# File 'lib/hws/payment_operations_demo/transactional_value_store.rb', line 28 def withdraw(amount:, tags:, txn_time: Time.now) self.deposit(amount: amount * -1, tags: , txn_time: txn_time) end |