Class: Api::V1::FinancialTxnsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/v1/financial_txns_controller.rb

Instance Method Summary collapse

Instance Method Details

#index ⇒ Object



5
6
7
8
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
# File 'app/controllers/api/v1/financial_txns_controller.rb', line 5

def index
  sort = nil
  dir = nil
  limit = nil
  start = nil

  unless params[:sort].blank?
    sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first)
    sort = sort_hash[:property] || 'description'
    dir = sort_hash[:direction] || 'ASC'
    limit = params[:limit] || 25
    start = params[:start] || 0
  end

  query_filter = params[:query_filter].blank? ? {} : JSON.parse(params[:query_filter]).symbolize_keys

  # hook method to apply any scopes passed via parameters to this api
  financial_txns = FinancialTxn.apply_filters(query_filter)

  # scope by dba_organizations if there are no parties passed as filters
  unless query_filter[:parties]
    dba_organizations = [current_user.party.dba_organization]
    dba_organizations = dba_organizations.concat(current_user.party.dba_organization.child_dba_organizations)
    financial_txns = financial_txns.scope_by_dba_organization(dba_organizations)
  end

  if sort and dir
    financial_txns = financial_txns.order("#{sort} #{dir}")
  end

  total_count = financial_txns.count

  if start and limit
    financial_txns = financial_txns.offset(start).limit(limit)
  end

  render :json => {success: true, total_count: total_count, financial_txns: financial_txns.collect(&:to_data_hash)}
end

#show ⇒ Object



44
45
46
47
48
# File 'app/controllers/api/v1/financial_txns_controller.rb', line 44

def show
  financial_txn = FinancialTxn.find(params[:id])

  render :json => {financial_txn: financial_txn.to_data_hash}
end