Class: Agent

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/agent.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_total_agent_from_current_location(latitude, longtitude) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'app/models/agent.rb', line 252

def self.get_total_agent_from_current_location(latitude, longtitude)
  radius = DEFAULT_RADIUS
   total_agents = Agent.joins(:user).where("users.is_online = true").near([latitude, longtitude], radius, :units => :mi)
   #total_agents = Agent.joins(:user).near([latitude, longtitude], radius, :units => :mi)
   while total_agents.size.to_i <= MAX_AGENTS_TOTAL  && radius < MAX_RADIUS
      p total_agents.size.to_i
      radius += DEFAULT_RADIUS
      total_agents = Agent.joins(:user).where("users.is_online = true").near([latitude, longtitude], radius, :units => :mi)
      #total_agents = Agent.joins(:user).near([latitude, longtitude], radius, :units => :mi)

   end

   total_agents.size.to_s
end

Instance Method Details

#as_json(request) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/agent.rb', line 63

def as_json(request)
  avatar = ""
  avatar = request.protocol + request.host_with_port + self.avatar.url if self.avatar.present?
  { 
    email:                self.user.email,
    first_name:           self.first_name,
    last_name:            self.last_name,
    company_name:         self.company_name,
    phone_number:         self.phone_number,
    about:                self.about,
    license_number:       self.license_number,
    license_state_issued: self.license_state_issued,
    license_issued_date:  self.license_issued_date,
    license_url:          self.try(:state).try(:main_page_url),
    status:               self.status,
    website_url:          self.website_url,
    listing_url:          self.listing_url,
    video_url:            self.video_url,
    avatar:               avatar,
    edit_profile_state:   self.edit_profile_state,
    edit_company_name_state: self.edit_company_name_state
   }
end

#as_json2(request) ⇒ Object



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
# File 'app/models/agent.rb', line 87

def as_json2(request)
  avatar = ""
  avatar = request.protocol + request.host_with_port + self.avatar.url if self.avatar.present?
  agent_change_info = self.user.agent_change_info.where(action: CHANGE_PROFILE, status: [PENDING, APPROVED]).last
  agent_change_company_name = self.user.agent_change_info.where(action: CHANGE_COMPANY_NAME , status: [PENDING, APPROVED]).last
  license = self.licenses.where(status: ["Pending", "Actived"]).last

  { 
    email:                self.user.email,
    first_name:           agent_change_info ? agent_change_info.first_name_new : self.first_name,
    last_name:            agent_change_info ? agent_change_info.last_name_new : self.last_name,
    company_name:         agent_change_company_name ? agent_change_company_name.company_name_new : self.company_name,
    phone_number:         self.phone_number,
    about:                self.about,
    license_number:       license ? license.license_number : self.license_number,
    license_state_issued: license ? license.license_state_issued : self.license_state_issued,
    license_issued_date:  license ? license.license_issued_date : self.license_issued_date,
    license_url:          self.try(:state).try(:main_page_url),
    status:               self.status,
    website_url:          self.website_url,
    listing_url:          self.listing_url,
    video_url:            self.video_url,
    avatar:               avatar,
    edit_profile_state:   self.edit_profile_state,
    edit_company_name_state: self.edit_company_name_state
   }
end

#get_agent_info_historyObject



127
128
129
130
131
132
133
134
# File 'app/models/agent.rb', line 127

def get_agent_info_history
  agent_change_info = AgentChangeInfo.where(user_id: self.user_id,status: APPROVED,action: CHANGE_COMPANY_NAME).last
  if agent_change_info.blank? || agent_change_info.updated_at < Time.now - 1.months
    self.update_attributes({:edit_company_name_state => PENDING})
    return agent_change_info
  end
  return agent_change_info
end

#get_rating_informationObject



115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/agent.rb', line 115

def get_rating_information
  ratings = Rating.where(agent_id: self.id)
  numCustomerRates = ratings.distinct.count(:customer_id)
  avgAll = ratings.average('rate').to_f
  avgQuestion = ratings.group('rating_question_id').average('rate').map{|question, rate| {question: question, rate: rate}}
  ratings = ratings.joins(:rating_question).group('category')      
  avgCategory = ratings.average('rate').map{|category, rate| {category: category, rate: rate}}
  numRateCategory = ratings.distinct.count("customer_id").map{|category, num| {category: category, numCustomerRates: num}}
  avgCategory_temp = ratings.average('rate').map{|category, rate| {category: category, numCustomerRates: rate}}
  {numCustomerRates: numCustomerRates, avgAllRates: avgAll, numRateCategory: numRateCategory,avgCategory_temp: avgCategory_temp, avgCategory: avgCategory, avgQuestion: avgQuestion}
end

#is_holdon?Boolean

Returns:

  • (Boolean)


267
268
269
# File 'app/models/agent.rb', line 267

def is_holdon?
  self.edit_profile_state == PENDING or self.edit_company_name_state == PENDING
end

#nameObject

end



57
58
59
60
61
# File 'app/models/agent.rb', line 57

def name
  if first_name.present? && last_name.present?
    "#{first_name} #{last_name}"
  end
end

#push_notification_apply_change(message, change_info) ⇒ Object



209
210
211
212
213
214
215
216
217
# File 'app/models/agent.rb', line 209

def push_notification_apply_change(message,change_info)
   customer_devices = self.user.customer_devices
   unless customer_devices.blank?
      customer_devices.each do |customer_device|
        customer_device.push_notification_change_first_last_name(message,change_info)
      end 
      
   end
end

#push_notification_apply_change_company(message, change_info) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'app/models/agent.rb', line 229

def push_notification_apply_change_company(message,change_info)
   customer_devices = self.user.customer_devices
   unless customer_devices.blank?
      customer_devices.each do |customer_device|
        customer_device.push_notification_change_company_name(message,change_info)
      end 
      
   end
end

#push_notification_change_license(status) ⇒ Object



219
220
221
222
223
224
225
226
227
# File 'app/models/agent.rb', line 219

def push_notification_change_license(status)
  customer_devices = self.user.customer_devices
  unless customer_devices.blank?
      customer_devices.each do |customer_device|
        customer_device.push_notification_change_license(status,self)
      end 
      
   end
end

#push_notification_onlineObject



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/models/agent.rb', line 196

def push_notification_online
    customer_location_alerts = CustomerLocationAlert.where("time_expired > (?) and is_alert IS TRUE",Time.now).within(DEFAULT_RADIUS,:units => :miles, origin: [self.latitude, self.longitude])
    user_ids = customer_location_alerts.select("user_id")
    customer_devices = CustomerDevice.where(user_id: user_ids)
    unless customer_location_alerts.blank? || customer_devices.blank?
    sns = Aws::SNS::Client.new
   p "======================================"
   p customer_devices
    customer_devices.each do |customer_device|
      customer_device.push_notification_agent_online(self)
    end
  end
end

#push_notification_to_agent_background(type, customer, sharing) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
# File 'app/models/agent.rb', line 240

def push_notification_to_agent_background(type,customer,sharing)
  customer_devices = self.user.customer_devices
   unless customer_devices.blank?
      customer_devices.each do |customer_device|
        customer_device.push_notification_background(type,self,customer,sharing)
      end 
      
   end

  
end

#save_company_name(company_name_new) ⇒ Object



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
# File 'app/models/agent.rb', line 135

def save_company_name(company_name_new)
    
    agent_change_info = AgentChangeInfo.find_by(user_id: self.user_id,status: PENDING,action: CHANGE_COMPANY_NAME)
    if agent_change_info.blank?
      agent_change_info = AgentChangeInfo.new( user_id: self.user_id,company_name_old: self.company_name,
                                      company_name_new: company_name_new, status: PENDING, 
                                      action: CHANGE_COMPANY_NAME)
      first_times = true
    else
      first_times = false
      gt_month = agent_change_info.updated_at.utc < Time.now.utc - 1.minute
      agent_change_info.company_name_new = company_name_new
    end
    if agent_change_info.save
      begin
        if first_times
          self.update!(edit_company_name_state: UNKNOWN)
          message = I18n.t 'saveCompanyName_success_gt_30'
        else
          if gt_month
            self.update!(edit_company_name_state: UNKNOWN)
            message = I18n.t 'saveCompanyName_success_gt_30'
          else
            self.update!(edit_company_name_state: PENDING)
            message = I18n.t 'saveCompanyName_success_ltq_30'
          end
        end
        return true, message
      rescue Exception => e
        puts "============errors"
        p e
        return false, ""
      end
    end
    puts "=========error"
    puts agent_change_info.errors.inspect
    return false
end

#save_fullname(first_name_new, last_name_new) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/models/agent.rb', line 174

def save_fullname(first_name_new, last_name_new)
    agent_change_info = AgentChangeInfo.find_by(user_id: self.user_id,status: PENDING,action: CHANGE_PROFILE)
    if agent_change_info.blank?
      agent_change_info = AgentChangeInfo.new(first_name_new: first_name_new, first_name_old: self.first_name, last_name_new: last_name_new, 
                                      last_name_old: self.last_name, user_id: self.user_id,
                                      status: PENDING,action: CHANGE_PROFILE)
    else
      agent_change_info.first_name_new = first_name_new
      agent_change_info.last_name_new = last_name_new
    end
    if agent_change_info.save
      begin
        self.update!(edit_profile_state: PENDING)
        return true  
      rescue Exception => e
        p e
        return false
      end
    end
    return false
end