Skip to content

Commit

Permalink
Merge pull request #1364 from scott/dev/2.6/add-user-status
Browse files Browse the repository at this point in the history
Add agent availability toggle
  • Loading branch information
scott committed Jul 22, 2019
2 parents f195019 + a06137e commit e90a184
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ def get_tickets_by_status
def fetch_counts
if current_user.is_restricted? && teams?
topics = Topic.tagged_with(current_user.team_list, :any => true)
@admins = User.agents #can_receive_ticket.tagged_with(current_user.team_list, :any => true)
@admins = User.agents.available #can_receive_ticket.tagged_with(current_user.team_list, :any => true)
else
topics = Topic.all
@admins = User.agents.includes(:topics)
@admins = User.agents.available.includes(:topics)
end
@new = topics.unread.size
@unread = topics.unread.size
Expand Down
25 changes: 23 additions & 2 deletions app/controllers/api/v1/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Users < Grape::API
optional :active, type: Boolean, desc: "User active or deactivated", default: true
optional :priority, type: String, desc: "Users Priority", values: ['low', 'normal', 'high', 'vip'], default: 'normal'
optional :notes, type: String, desc: "Notes about the user"
optional :status, type: String, desc: "User/Agent status"
end
post "", root: :users do
user = User.create!(
Expand All @@ -107,7 +108,8 @@ class Users < Grape::API
language: permitted_params[:language],
active: permitted_params[:active],
priority: permitted_params[:priority],
notes: permitted_params[:notes]
notes: permitted_params[:notes],
status: permitted_params[:status]
)
present user, with: Entity::User
end
Expand Down Expand Up @@ -143,6 +145,7 @@ class Users < Grape::API
optional :active, type: Boolean, desc: "User active or deactivated"
optional :priority, type: String, desc: "Users Priority- low, normal, high or vip", default: 'normal'
optional :notes, type: String, desc: "Notes about the user"
optional :status, type: String, desc: "User/Agent status"
end
patch ":id", root: :users do
user = User.where(id: permitted_params[:id]).first
Expand Down Expand Up @@ -170,7 +173,25 @@ class Users < Grape::API
language: permitted_params[:language],
active: permitted_params[:active],
priority: permitted_params[:priority],
notes: permitted_params[:notes]
notes: permitted_params[:notes],
status: permitted_params[:status]
)
present user, with: Entity::User
end

# UPDATE AGENT STATUS
desc "Update agents status", {
entity: Entity::User,
notes: "Update a user"
}
params do
requires :id, type: Integer, desc: "User ID"
requires :status, type: String, desc: "User/Agent status"
end
patch "status/:id", root: :users do
user = User.where(id: permitted_params[:id]).first
user.update!(
status: permitted_params[:status]
)
present user, with: Entity::User
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Doc < ActiveRecord::Base
validates :body, presence: true
validates :category_id, presence: true

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

Expand Down
1 change: 1 addition & 0 deletions app/models/entity/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ class User < Base
expose :account_number
expose :priority
expose :notes
expose :status
end
end
2 changes: 1 addition & 1 deletion app/models/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Topic < ActiveRecord::Base

paginates_per 15

include PgSearch
include PgSearch::Model
multisearchable :against => [:id, :name, :post_cache],
:if => :public?

Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class User < ActiveRecord::Base
include Gravtastic
mount_uploader :profile_image, ProfileImageUploader

include PgSearch
include PgSearch::Model
pg_search_scope :user_search,
against: [:name, :login, :email, :company, :account_number, :home_phone, :work_phone, :cell_phone]

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

def set_role_on_invitation_accept
self.role = self.role.presence || "agent"
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190716202013_add_status_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToUser < ActiveRecord::Migration
def change
add_column :users, :status, :string, default: 'available'
end
end
13 changes: 7 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20190513145733) do
ActiveRecord::Schema.define(version: 20190716202013) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -307,14 +307,14 @@
t.integer "assigned_ticket_count", default: 0
t.integer "topics_count", default: 0
t.boolean "active", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
Expand All @@ -338,6 +338,7 @@
t.string "account_number"
t.string "priority", default: "normal"
t.text "notes"
t.string "status", default: "available"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/api/v1/users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,12 @@ def app
assert_equal "anon", object['login']
assert_nil object['city']
end

test "API users should be able to set status of users" do
user = User.find(6)
patch "/api/v1/users/status/#{user.id}.json?status=vacation", @default_params

object = JSON.parse(last_response.body)
assert_equal "vacation", object['status']
end
end

0 comments on commit e90a184

Please sign in to comment.