Class: ErpApp::Organizer::Crm::UsersController

Inherits:
BaseController show all
Defined in:
app/controllers/erp_app/organizer/crm/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/controllers/erp_app/organizer/crm/users_controller.rb', line 133

def create
  result = {}

  user_data = params[:data].present? ? params[:data] : params
  party_id = params[:party_id] || user_data[:party_id]

  party = party_id.blank? ? nil : Party.find(party_id)

  begin
    ActiveRecord::Base.transaction do
      user = User.new(
          :email => user_data['email'].strip,
          :username => user_data['username'].strip,
      )

      if user_data[:password].present? && user_data[:password_confirmation].present?
        user.password = user_data[:password].strip
        user.password_confirmation = user_data[:password_confirmation].strip
        user.add_instance_attribute(:temp_password, user_data[:password])
      else
        temp_password = 'AB' + SecureRandom.uuid[0..5] + 'CD'
        user.password = temp_password
        user.password_confirmation = temp_password
        user.add_instance_attribute(:temp_password, temp_password)
      end

      #set this to tell activation where to redirect_to for login and temp password
      user.add_instance_attribute(:login_url, '/erp_app/login')

      # if a party was passed create this user with the party if not then create a individual now
      if party
        user.party = party
      else
        individual = Individual.create(current_first_name: params[:first_name].strip,
                                       current_last_name: params[:last_name].strip)

        user.party = individual.party

        # add a party roles if passed
        if params[:party_roles].present?
          params[:party_roles].split(',').each do |party_role|
            PartyRole.create(party: individual.party, role_type: RoleType.iid(party_role))
          end
        end
      end

      if params[:skip_activation_email].present? and params[:skip_activation_email] === 'true'
        user.skip_activation_email = true
      end

      # add security roles if present
      if params[:security_roles].present?
        params[:security_roles].split(',').each do |security_role|
          user.party.add_role(security_role)
        end
      end

      if user.save
        if params[:skip_activation_email].present? and params[:skip_activation_email] === 'true'
          if user_data[:activation_state].present? and user_data[:activation_state] != 'pending'
            user.activation_state = user_data[:activation_state].strip
            user.save!
          end
        end

        result = {:success => user.save, :users => user.to_hash(:only =>
                                                                    [:id, :username,
                                                                     :email,
                                                                     :last_login_at,
                                                                     :created_at,
                                                                     :updated_at,
                                                                     :activation_state])}
      else
        result = {:success => false, :message => user.errors.full_messages.to_sentence}
      end
    end
  rescue => ex
    Rails.logger.error ex.message
    Rails.logger.error ex.backtrace.join("\n")

    ExceptionNotifier.notify_exception(ex) if defined? ExceptionNotifier

    result = {:success => false, :message => "Error adding user."}
  end

  render :json => result
end

#destroyObject



263
264
265
266
267
# File 'app/controllers/erp_app/organizer/crm/users_controller.rb', line 263

def destroy
  user = User.find(params[:id])

  render :json => {:success => user.destroy}
end

#indexObject



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
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
# File 'app/controllers/erp_app/organizer/crm/users_controller.rb', line 6

def index
  party_roles = params[:party_roles]
  to_role = params[:to_role]
  to_party_id = params[:to_party_id]
  offset = params[:start] || 0
  limit = params[:limit] || 25
  included_party_to_relationships = ActiveSupport::JSON.decode params[:included_party_to_relationships]
  query_filter = params[:query_filter] || nil

  # determine sorting
  sort_hash = params[:sort].present? ? ActiveSupport::JSON.decode(params[:sort]).first : {}

  #
  # Convert sort parameters that are mapped Extjs fields
  #
  if sort_hash['property'] == 'createdAt'
    sort_hash['property'] = 'created_at'
  elsif sort_hash['property'] == 'updatedAt'
    sort_hash['property'] = 'updated_at'
  elsif sort_hash['property'] == 'status'
    sort_hash['property'] = 'activation_state'
  elsif sort_hash['property'] == 'lastLoginAt'
    sort_hash['property'] = 'last_login_at'
  elsif sort_hash['property'] == 'lastLogoutAt'
    sort_hash['property'] = 'last_logout_at'
  elsif sort_hash['property'] == 'lastActivityAt'
    sort_hash['property'] = 'last_activity_at'
  end

  order_by = sort_hash['property'] || 'created_at'
  direction = sort_hash['direction'] || 'desc'

  user_table = User.arel_table

  if party_roles.present?
    statement = User.joins(:party => :party_roles).where(:party_roles => {:role_type_id => RoleType.where(:internal_identifier => party_roles).all})
  end

  unless to_role.blank?
    to_role_type = RoleType.iid(to_role)

    if to_party_id.blank?
      to_party = current_user.party
      to_party_rln = to_party.from_relationships.where('role_type_id_to = ?', to_role_type).first

      unless to_party_rln.nil?
        statement = statement.joins("join party_relationships on party_relationships.party_id_from = parties.id")
        .where('party_relationships.party_id_to = ?', to_party_rln.party_id_to)
        .where('party_relationships.role_type_id_to' => to_role_type)
      end
    else
      to_party = Party.find(to_party_id)

      statement = statement.joins("join party_relationships on party_relationships.party_id_from = parties.id")
      .where('party_relationships.party_id_to = ?', to_party.id)
      .where('party_relationships.role_type_id_to' => to_role_type)
    end
  end

  if query_filter
    statement = statement.where(user_table[:username].matches("%#{query_filter}%")
                                .or(user_table[:email].matches("%#{query_filter}%")))
  end

  total = statement.uniq.count
  users = statement.uniq.order("#{order_by} #{direction}").limit(limit).offset(offset).all

  data = {
      :success => true,
      :users => users.collect do |user|
        user_data = user.to_hash(:only =>
                                     [:id, :username,
                                      :email,
                                      :last_login_at,
                                      :last_logout_at,
                                      :last_activity_at,
                                      :created_at,
                                      :updated_at,
                                      :activation_state],
                                 :party_description => (user.party.description))

        # add relationships
        included_party_to_relationships.each do |included_party_to_relationship|
          included_party_to_relationship.symbolize_keys!

          relationship = user.party.relationships.where('relationship_type_id = ?', RelationshipType.find_by_internal_identifier(included_party_to_relationship[:relationshipType])).first
          if relationship
            user_data[included_party_to_relationship[:toRoleType]] = relationship.to_party.description
          end
        end

        if user.party.business_party.class == Individual
          user_data[:first_name] = user.party.business_party.current_first_name
          user_data[:last_name] = user.party.business_party.current_last_name
        end

        user_data
      end,
      :total => total
  }

  render :json => data
end

#showObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/erp_app/organizer/crm/users_controller.rb', line 110

def show
  result = {:success => true}

  user = User.where('id = ?', params[:id]).first

  if user
    individual = user.party.business_party

    result[:user] = user.to_hash(:only =>
                                     [:id,
                                      :username,
                                      :email,
                                      :last_login_at,
                                      :created_at,
                                      :updated_at,
                                      :activation_state],
                                 :first_name => individual.current_first_name,
                                 :last_name => individual.current_last_name)
  end

  render :json => result
end

#updateObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'app/controllers/erp_app/organizer/crm/users_controller.rb', line 221

def update
  user_data = params[:data].present? ? params[:data] : params

  user = User.find(params[:id])

  user.username = user_data[:username].strip
  user.email = user_data[:email].strip

  # update first name and last of individual if passed
  if params[:first_name].present? and params[:last_name].present?
    individual = user.party.business_party

    individual.current_first_name = params[:first_name].strip
    individual.current_last_name = params[:last_name].strip

    individual.save
  end

  if user_data[:activation_state].present?
    user.activation_state = user_data[:activation_state].strip
  end

  if user_data[:password].present? && user_data[:password_confirmation].present?
    user.password = user_data[:password].strip
    user.password_confirmation = user_data[:password_confirmation].strip
  end

  if user.save
    result = {:success => user.save, :users => user.to_hash(:only =>
                                                                [:id, :username,
                                                                 :email,
                                                                 :last_login_at,
                                                                 :created_at,
                                                                 :updated_at,
                                                                 :activation_state])}
  else
    result = {:success => false, :message => user.errors.full_messages.to_sentence}
  end

  render :json => result
end