Class: Api::V1::PartiesController

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

Instance Method Summary collapse

Instance Method Details

#create ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
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
# File 'app/controllers/api/v1/parties_controller.rb', line 64

def create
  begin
    ActiveRecord::Base.transaction do
      role_type_iids = params[:role_types].present? ? params[:role_types].split(',') : []
      business_party_klass = params[:business_party]

      business_party = business_party_klass.constantize.new
      if business_party_klass == 'Organization'
        business_party.description = params[:description].strip
      else
        business_party.current_first_name = params[:first_name].strip
        business_party.current_last_name = params[:last_name].strip
      end

      business_party.save!

      dba_organization = current_user.party.dba_organization
      role_type_iids.each do |role_type_iid|
        role_type = RoleType.iid(role_type_iid)

        PartyRole.create(party: business_party.party, role_type: role_type)

        # associate to dba_org
        relationship_type = RelationshipType.find_or_create(RoleType.iid('dba_org'), role_type)

        business_party.party.create_relationship(relationship_type.description,
                                                 dba_organization.id,
                                                 relationship_type)
      end

      business_party.party.created_by_party = current_user.party
      business_party.party.save!

      render :json => {success: true, party: business_party.party.to_data_hash}
    end
  rescue ActiveRecord::RecordInvalid => invalid

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

    # email error
    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    render :json => {success: false, message: 'Application Error'}
  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
# File 'app/controllers/api/v1/parties_controller.rb', line 5

def index
  query = params[:query]
  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
  role_types = params[:role_types]

  parties = Party

  unless query.blank?
    parties_tbl = Party.arel_table

    where_clause = nil
    # if the query has commas split on the commas and treat them as separate search terms
    query.split(',').each do |query_part|
      if where_clause.nil?
        where_clause = parties_tbl[:description].matches(query_part.strip + '%')
      else
        where_clause = where_clause.or(parties_tbl[:description].matches(query_part.strip + '%'))
      end
    end

    parties = parties.where(where_clause)
  end

  unless params[:id].blank?
    parties = parties.where(id: params[:id].split(','))
  end

  unless role_types.blank?
    if params[:include_child_roles]
      role_types = RoleType.find_child_role_types(role_types.split(',')).collect{|role_type| role_type.internal_identifier}
    else
      role_types = role_types.split(',')
    end

    parties = parties.joins(party_roles: :role_type).where('role_types.internal_identifier' => role_types)
  end

  # scope by dba organization
  if params[:include_descendants].present? and params[:include_descendants].to_bool
    dba_organization = [current_user.party.dba_organization]
    dba_organization.concat(current_user.party.dba_organization.child_dba_organizations)

    parties = parties.scope_by_dba_organization(dba_organization)
  else
    parties = parties.scope_by_dba_organization(current_user.party.dba_organization)
  end

  parties = parties.uniq.order("#{sort} #{dir}")

  total_count = parties.count
  parties = parties.offset(start).limit(limit)

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

#show ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/api/v1/parties_controller.rb', line 113

def show
  party = Party.find(params[:id])

  data = party.to_data_hash

  if params[:include_email]
    if params[:email_purposes].present?
      contact_purposes = params[:email_purposes].split(',')
      data[:email_addresses] = party.email_addresses_to_hash(contact_purposes)
    else
      data[:email_addresses] = party.email_addresses_to_hash
    end
  end

  if params[:include_phone_number]
    if params[:phone_number_purposes].present?
      contact_purposes = params[:phone_number_purposes].split(',')
      data[:phone_numbers] = party.phone_numbers_to_hash(contact_purposes)
    else
      data[:phone_numbers] = party.phone_numbers_to_hash
    end
  end

  if params[:include_postal_address]
    if params[:postal_address_purposes].present?
      contact_purposes = params[:postal_address_purposes].split(',')
      data[:postal_addresses] = party.postal_addresses_to_hash(contact_purposes)
    else
      data[:postal_addresses] = party.postal_addresses_to_hash
    end
  end

  data[:custom_fields] = party.custom_fields

  render :json => {success: true, party: data}
end

#update_roles ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/controllers/api/v1/parties_controller.rb', line 150

def update_roles
  ActiveRecord::Base.transaction do
    begin
      party = Party.find(params[:id])
      role_type_iids = params[:role_type_iids].split(',')

      # remove current party roles
      party.party_roles.destroy_all

      # assign new roles
      role_type_iids.each do |role_type_iid|
        role_type = RoleType.iid(role_type_iid)

        PartyRole.create(party: party, role_type: role_type)
      end

      party.updated_by_party = current_user.party
      party.save!

      render :json => {success: true}

    rescue Exception => ex
      Rails.logger.error(e.message)
      Rails.logger.error(e.backtrace.join("\n"))

      render :json => {success: false}
    end # begin
  end # transaction
end