Class: Api::V1::BizTxnAcctRootsController

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

Instance Method Summary collapse

Instance Method Details

#create ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/api/v1/biz_txn_acct_roots_controller.rb', line 76

def create
  description = params[:description].strip
  external_identifier = params[:external_identifier].strip

  begin
    ActiveRecord::Base.transaction do
      biz_txn_acct_root = BizTxnAcctRoot.create(description: description,
                                                internal_identifier: description.to_iid,
                                                external_identifier: external_identifier)

      if !params[:parent].blank? and params[:parent] != 'No Parent'
        parent = BizTxnAcctRoot.iid(params[:parent])
        biz_txn_acct_root.move_to_child_of(parent)
      elsif !params[:default_parent].blank?
        parent = BizTxnAcctRoot.iid(params[:default_parent])
        biz_txn_acct_root.move_to_child_of(parent)
      end

      if params[:biz_txn_acct_type_iid]
        biz_txn_acct_root.biz_txn_acct_type = BizTxnAcctType.iid(params[:biz_txn_acct_type_iid])
      end

      BizTxnAcctPartyRole.create(biz_txn_acct_root: biz_txn_acct_root,
                                 party: current_user.party.dba_organization,
                                 biz_txn_acct_pty_rtype: BizTxnAcctPtyRtype.find_or_create('dba_org', 'DBA Organization'))

      biz_txn_acct_root.created_by_party = current_user.party

      biz_txn_acct_root.save!

      render :json => {success: true, biz_txn_type: biz_txn_acct_root.to_data_hash}
    end
  rescue ActiveRecord::RecordInvalid => invalid
    Rails.logger.error invalid.record.errors.full_messages

    render json: {:success => false, :message => invalid.record.errors.full_messages.join('</br>')}
  rescue => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render json: {:success => false, :message => "Error creating record"}
  end
end

#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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/api/v1/biz_txn_acct_roots_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
  biz_txn_acct_roots = BizTxnAcctRoot.apply_filters(query_filter)

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

  respond_to do |format|
    format.json do

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

      total_count = biz_txn_acct_roots.count

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

      render :json => {success: true,
                       total_count: total_count,
                       biz_txn_acct_roots: biz_txn_acct_roots.collect { |item| item.to_data_hash }}
    end
    format.tree do
      if params[:parent_id]
        render :json => {success: true,
                         biz_txn_acct_roots: BizTxnAcctRoot.find(params[:parent_id]).children_to_tree_hash}
      else
        nodes = [].tap do |nodes|
          biz_txn_acct_roots.roots.each do |root|
            nodes.push(root.to_tree_hash)
          end
        end

        render :json => {success: true,
                         biz_txn_acct_roots: nodes}
      end

    end
    format.all_representation do
      if params[:parent_id].present?
        render :json => {success: true,
                         biz_txn_acct_roots: BizTxnAcctRoot.to_all_representation(BizTxnAcctRoot.find(params[:parent_id]))}
      else


        render :json => {success: true,
                         biz_txn_acct_roots: BizTxnAcctRoot.to_all_representation(nil, [], 0, biz_txn_acct_roots.roots)}
      end
    end
  end
end