Class: Skala::AlephAdapter::GetUserTransactions

Inherits:
Skala::Adapter::GetUserTransactions show all
Includes:
parentparent::ResolveUser
Defined in:
lib/skala/aleph_adapter/get_user_transactions.rb

Instance Attribute Summary

Attributes inherited from Skala::Adapter::Operation

#adapter

Instance Method Summary collapse

Methods inherited from Skala::Adapter::Operation

#initialize

Constructor Details

This class inherits a constructor from Skala::Adapter::Operation

Instance Method Details

#call(username, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/skala/aleph_adapter/get_user_transactions.rb', line 9

def call(username, options = {})
  resolved_user_id = resolve_user(username)

  patron_cash_list = @adapter.restful_api.patron(resolved_user_id).circulationActions.cash.get(view: :full)
  patron_loan_list = @adapter.restful_api.patron(resolved_user_id).circulationActions.loans.get(view: :full)

  if [patron_cash_list, patron_loan_list].any? { |_response| _response.include?("<error>") }
    return nil
  else
    transactions = []

    # regular credits/debits
    Nokogiri::XML(patron_cash_list).xpath("//cash").each do |_cash|
      credit_or_debit = {
        id: id(_cash),
        creation_date: creation_date(_cash),
        record: {
          id: record_id(_cash)
        },
        reason: reason(_cash),
        type: type(_cash),
        value: value(_cash),
        description: description(_cash)
      }

      transactions << credit_or_debit
    end

    # unentered debits from loans
    Nokogiri::XML(patron_loan_list).xpath("//loan").each do |_loan|
      if fine = _loan.at_xpath("./fine").try(:content).presence
        transactions << {
          id: nil,
          record: {
            id: record_id(_loan)
          },
          reason: :overdue_fine,
          type: :debit,
          value: fine.to_f
        }
      end
    end

    self.class::Result.new(
      transactions: transactions,
      source: [patron_cash_list, patron_loan_list]
    )
  end
end