I have a user table and a reporting events table.
reporting_event.rb
class ReportingEvent < ApplicationRecord
belongs_to :user
belongs_to :conversation, optional: true
EVENT_TYPES = {
ASSIGN_AGENT: "assign_agent",
FIRST_RESPONSE: "first_response",
AVERAGE_RESOLUTION_TIME: "avg_resolution_time",
POST_CHAT_RESPONSE: "post_chat_response",
AVERAGE_RESPONSE_TIME: "avg_response_time",
AVERAGE_FIRST_RESPONSE_TIME: "avg_first_response_time",
CLOSE_CONVERSATION: "close_conversation"
}
scope :post_chat, -> { where(name: EVENT_TYPES[:POST_CHAT_RESPONSE]) }
scope :assignments, -> { where(name: EVENT_TYPES[:ASSIGN_AGENT]) }
scope :resolutions, -> { where(name: EVENT_TYPES[:CLOSE_CONVERSATION]) }
scope :avg_resolution_times, -> { where(name: EVENT_TYPES[:AVERAGE_RESOLUTION_TIME]) }
scope :first_response_times, -> { where(name: EVENT_TYPES[:FIRST_RESPONSE]) }
scope :avg_response_times, -> { where(name: EVENT_TYPES[:AVERAGE_RESPONSE_TIME]) }
scope :avg_first_response_times, -> { where(name: EVENT_TYPES[:AVERAGE_FIRST_RESPONSE_TIME]) }
scope :between_dates, -> (start_date, end_date) { where(created_at: [start_date..end_date]) }
validates :name, inclusion: EVENT_TYPES.values
end
Reporting Event belongs to user.
I have to sort user based on reporting event assignments, resultions, avg_resolution_time etc values. For this I can make a scope in user.rb?
scope :sort_by_average_resolution_time, ->(dir: "ASC") { joins(:reporting_events).order("reporting_events.avg_resolution_times #{dir}") }
scope :sort_by_average_resolution_time_asc, -> { sort_by_average_resolution_time }
scope :sort_by_approval_rate_desc, -> { sort_by_average_resolution_time dir: "DESC" }
In the controller, I want to sort the users.
def index
filter_service = Reports::FilterService.new(@organization, reports_filter_params)
users = filter_service.process
@user_reports = users.sort_by_average_resolution_time_asc.map { |agent| ReportsCarrier.new(agent, @start_date, @end_date) }
@total_count = filter_service.total_count
end
How do I sort by reporting_events values which belongs to user?
Aucun commentaire:
Enregistrer un commentaire