Skip to content

Commit e90a184

Browse files
authored
Merge pull request #1364 from scott/dev/2.6/add-user-status
Add agent availability toggle
2 parents f195019 + a06137e commit e90a184

File tree

9 files changed

+50
-13
lines changed

9 files changed

+50
-13
lines changed

app/controllers/admin/base_controller.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ def get_tickets_by_status
111111
def fetch_counts
112112
if current_user.is_restricted? && teams?
113113
topics = Topic.tagged_with(current_user.team_list, :any => true)
114-
@admins = User.agents #can_receive_ticket.tagged_with(current_user.team_list, :any => true)
114+
@admins = User.agents.available #can_receive_ticket.tagged_with(current_user.team_list, :any => true)
115115
else
116116
topics = Topic.all
117-
@admins = User.agents.includes(:topics)
117+
@admins = User.agents.available.includes(:topics)
118118
end
119119
@new = topics.unread.size
120120
@unread = topics.unread.size

app/controllers/api/v1/users.rb

+23-2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class Users < Grape::API
8181
optional :active, type: Boolean, desc: "User active or deactivated", default: true
8282
optional :priority, type: String, desc: "Users Priority", values: ['low', 'normal', 'high', 'vip'], default: 'normal'
8383
optional :notes, type: String, desc: "Notes about the user"
84+
optional :status, type: String, desc: "User/Agent status"
8485
end
8586
post "", root: :users do
8687
user = User.create!(
@@ -107,7 +108,8 @@ class Users < Grape::API
107108
language: permitted_params[:language],
108109
active: permitted_params[:active],
109110
priority: permitted_params[:priority],
110-
notes: permitted_params[:notes]
111+
notes: permitted_params[:notes],
112+
status: permitted_params[:status]
111113
)
112114
present user, with: Entity::User
113115
end
@@ -143,6 +145,7 @@ class Users < Grape::API
143145
optional :active, type: Boolean, desc: "User active or deactivated"
144146
optional :priority, type: String, desc: "Users Priority- low, normal, high or vip", default: 'normal'
145147
optional :notes, type: String, desc: "Notes about the user"
148+
optional :status, type: String, desc: "User/Agent status"
146149
end
147150
patch ":id", root: :users do
148151
user = User.where(id: permitted_params[:id]).first
@@ -170,7 +173,25 @@ class Users < Grape::API
170173
language: permitted_params[:language],
171174
active: permitted_params[:active],
172175
priority: permitted_params[:priority],
173-
notes: permitted_params[:notes]
176+
notes: permitted_params[:notes],
177+
status: permitted_params[:status]
178+
)
179+
present user, with: Entity::User
180+
end
181+
182+
# UPDATE AGENT STATUS
183+
desc "Update agents status", {
184+
entity: Entity::User,
185+
notes: "Update a user"
186+
}
187+
params do
188+
requires :id, type: Integer, desc: "User ID"
189+
requires :status, type: String, desc: "User/Agent status"
190+
end
191+
patch "status/:id", root: :users do
192+
user = User.where(id: permitted_params[:id]).first
193+
user.update!(
194+
status: permitted_params[:status]
174195
)
175196
present user, with: Entity::User
176197
end

app/models/doc.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Doc < ActiveRecord::Base
3939
validates :body, presence: true
4040
validates :category_id, presence: true
4141

42-
include PgSearch
42+
include PgSearch::Model
4343
multisearchable against: [:title, :body, :keywords],
4444
:if => lambda { |record| record.category.present? && record.category.publicly_viewable? && record.active && record.category.active? }
4545

app/models/entity/user.rb

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ class User < Base
3333
expose :account_number
3434
expose :priority
3535
expose :notes
36+
expose :status
3637
end
3738
end

app/models/topic.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Topic < ActiveRecord::Base
4545

4646
paginates_per 15
4747

48-
include PgSearch
48+
include PgSearch::Model
4949
multisearchable :against => [:id, :name, :post_cache],
5050
:if => :public?
5151

app/models/user.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class User < ActiveRecord::Base
8383
include Gravtastic
8484
mount_uploader :profile_image, ProfileImageUploader
8585

86-
include PgSearch
86+
include PgSearch::Model
8787
pg_search_scope :user_search,
8888
against: [:name, :login, :email, :company, :account_number, :home_phone, :work_phone, :cell_phone]
8989

@@ -117,6 +117,7 @@ class User < ActiveRecord::Base
117117
scope :by_role, -> (role) { where(role: role) }
118118
scope :active_first, -> { order('updated_at desc') }
119119
scope :alpha, -> { order('name asc') }
120+
scope :available, -> { where(status: 'available') }
120121

121122
def set_role_on_invitation_accept
122123
self.role = self.role.presence || "agent"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddStatusToUser < ActiveRecord::Migration
2+
def change
3+
add_column :users, :status, :string, default: 'available'
4+
end
5+
end

db/schema.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20190513145733) do
14+
ActiveRecord::Schema.define(version: 20190716202013) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -307,14 +307,14 @@
307307
t.integer "assigned_ticket_count", default: 0
308308
t.integer "topics_count", default: 0
309309
t.boolean "active", default: true
310-
t.datetime "created_at", null: false
311-
t.datetime "updated_at", null: false
312-
t.string "email", default: "", null: false
313-
t.string "encrypted_password", default: "", null: false
310+
t.datetime "created_at", null: false
311+
t.datetime "updated_at", null: false
312+
t.string "email", default: "", null: false
313+
t.string "encrypted_password", default: "", null: false
314314
t.string "reset_password_token"
315315
t.datetime "reset_password_sent_at"
316316
t.datetime "remember_created_at"
317-
t.integer "sign_in_count", default: 0, null: false
317+
t.integer "sign_in_count", default: 0, null: false
318318
t.datetime "current_sign_in_at"
319319
t.datetime "last_sign_in_at"
320320
t.inet "current_sign_in_ip"
@@ -338,6 +338,7 @@
338338
t.string "account_number"
339339
t.string "priority", default: "normal"
340340
t.text "notes"
341+
t.string "status", default: "available"
341342
end
342343

343344
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree

test/controllers/api/v1/users_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,12 @@ def app
212212
assert_equal "anon", object['login']
213213
assert_nil object['city']
214214
end
215+
216+
test "API users should be able to set status of users" do
217+
user = User.find(6)
218+
patch "/api/v1/users/status/#{user.id}.json?status=vacation", @default_params
219+
220+
object = JSON.parse(last_response.body)
221+
assert_equal "vacation", object['status']
222+
end
215223
end

0 commit comments

Comments
 (0)