Class: Hws::PaymentOperationsDemo::TransactionalValueStore

Inherits:
Object
  • Object
show all
Defined in:
lib/hws/payment_operations_demo/transactional_value_store.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_idObject

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_idObject

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: store_tags
      }
    )

    transaction_group = Hws::Transactions.create_group(
      "store_ledger_#{store_id}", "value_store_#{description}", %I[status bank_ref_id].concat(txn_tags),
      %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 = get_immutable_tags_from_entry(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, tags = {})
  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(tags))
    if status == 'FAILED'
      store_id = get_immutable_tags_from_entry(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

#balanceObject



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)
    tags.key?(:immutable_tags) ? tags[:immutable_tags][:store_id] = store_id : tags[:immutable_tags] = { store_id: store_id }
    Hws::Transactions.add_entry(@transaction_group_id, amount, txn_time, tags)
  end

  entry.id
end

#storeObject



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, tags = {})
  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(tags))
    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: tags, txn_time: txn_time)
end