2020-02-14 19:07:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Dashboard
|
|
|
|
class CurrentTasksController < ApplicationController
|
2020-02-17 17:47:10 +08:00
|
|
|
include InputSanitizeHelper
|
2020-10-26 18:03:43 +08:00
|
|
|
helper_method :current_task_date
|
2020-02-17 17:47:10 +08:00
|
|
|
|
2020-02-28 18:51:08 +08:00
|
|
|
before_action :load_project, only: %i(show experiment_filter)
|
|
|
|
before_action :load_experiment, only: :show
|
|
|
|
before_action :check_task_view_permissions, only: :show
|
2020-02-26 18:07:17 +08:00
|
|
|
|
2020-02-21 17:41:42 +08:00
|
|
|
def show
|
2020-03-02 23:11:14 +08:00
|
|
|
page = (params[:page] || 1).to_i
|
2023-02-16 20:57:17 +08:00
|
|
|
tasks = load_tasks.page(page).per(Constants::INFINITE_SCROLL_LIMIT).without_count
|
2020-02-28 23:17:32 +08:00
|
|
|
|
2020-03-03 23:17:29 +08:00
|
|
|
tasks_list = tasks.map do |task|
|
2020-10-22 14:02:15 +08:00
|
|
|
render_to_string(partial: 'dashboards/current_tasks/task', locals: { task: task })
|
2020-02-21 17:41:42 +08:00
|
|
|
end
|
2020-03-02 23:11:14 +08:00
|
|
|
|
2020-03-03 23:17:29 +08:00
|
|
|
render json: { data: tasks_list, next_page: tasks.next_page }
|
2020-02-21 17:41:42 +08:00
|
|
|
end
|
2020-02-17 17:47:10 +08:00
|
|
|
|
|
|
|
def project_filter
|
2020-03-03 20:13:46 +08:00
|
|
|
projects = current_team.projects
|
|
|
|
.where(archived: false)
|
|
|
|
.viewable_by_user(current_user, current_team)
|
|
|
|
.search_by_name(current_user, current_team, params[:query]).select(:id, :name)
|
|
|
|
|
2020-02-17 17:47:10 +08:00
|
|
|
unless params[:mode] == 'team'
|
2020-02-17 22:32:43 +08:00
|
|
|
projects = projects.where(id: current_user.my_modules.joins(:experiment)
|
2020-02-17 17:47:10 +08:00
|
|
|
.group(:project_id).select(:project_id).pluck(:project_id))
|
|
|
|
end
|
2020-02-17 22:32:43 +08:00
|
|
|
render json: projects.map { |i| { value: i.id, label: escape_input(i.name) } }, status: :ok
|
2020-02-17 17:47:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def experiment_filter
|
2020-02-28 18:51:08 +08:00
|
|
|
unless @project
|
2020-02-17 17:47:10 +08:00
|
|
|
render json: []
|
|
|
|
return false
|
|
|
|
end
|
2020-03-03 20:13:46 +08:00
|
|
|
experiments = @project.experiments
|
|
|
|
.where(archived: false)
|
|
|
|
.viewable_by_user(current_user, current_team)
|
|
|
|
.search_by_name(current_user, current_team, params[:query]).select(:id, :name)
|
|
|
|
|
2020-02-17 17:47:10 +08:00
|
|
|
unless params[:mode] == 'team'
|
2020-02-17 22:32:43 +08:00
|
|
|
experiments = experiments.where(id: current_user.my_modules
|
2020-02-17 17:47:10 +08:00
|
|
|
.group(:experiment_id).select(:experiment_id).pluck(:experiment_id))
|
|
|
|
end
|
2020-02-17 22:32:43 +08:00
|
|
|
render json: experiments.map { |i| { value: i.id, label: escape_input(i.name) } }, status: :ok
|
2020-02-14 19:07:40 +08:00
|
|
|
end
|
2020-02-26 18:07:17 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-10-26 18:03:43 +08:00
|
|
|
def current_task_date(task)
|
|
|
|
span_class, translation_key, date = nil
|
|
|
|
|
2020-09-02 22:49:59 +08:00
|
|
|
if task.completed?
|
2020-10-26 18:03:43 +08:00
|
|
|
translation_key = 'completed_on_html'
|
|
|
|
date = task.completed_on
|
|
|
|
elsif task.due_date.present?
|
|
|
|
date = task.due_date
|
|
|
|
|
2020-03-04 20:21:52 +08:00
|
|
|
if task.is_overdue?
|
2020-10-26 18:03:43 +08:00
|
|
|
span_class = 'overdue'
|
|
|
|
translation_key = 'due_date_overdue_html'
|
2020-08-10 15:10:56 +08:00
|
|
|
elsif task.is_one_day_prior?
|
2020-10-26 18:03:43 +08:00
|
|
|
span_class = 'day-prior'
|
|
|
|
translation_key = 'due_date_html'
|
|
|
|
else
|
|
|
|
span_class = ''
|
|
|
|
translation_key = 'due_date_html'
|
2020-03-04 20:21:52 +08:00
|
|
|
end
|
2020-08-10 15:10:56 +08:00
|
|
|
end
|
2020-10-26 18:03:43 +08:00
|
|
|
{ span_class: span_class, translation_key: translation_key, date: date }
|
2020-03-04 20:21:52 +08:00
|
|
|
end
|
|
|
|
|
2020-02-26 18:07:17 +08:00
|
|
|
def task_filters
|
2020-08-27 18:13:13 +08:00
|
|
|
params.permit(:project_id, :experiment_id, :mode, :sort, :query, :page, statuses: [])
|
2020-02-26 18:07:17 +08:00
|
|
|
end
|
|
|
|
|
2020-02-28 18:51:08 +08:00
|
|
|
def load_project
|
|
|
|
@project = current_team.projects.find_by(id: params[:project_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_experiment
|
|
|
|
@experiment = @project.experiments.find_by(id: params[:experiment_id]) if @project
|
|
|
|
end
|
2020-02-26 18:07:17 +08:00
|
|
|
|
2023-02-16 20:57:17 +08:00
|
|
|
def load_tasks
|
|
|
|
tasks = if @experiment
|
|
|
|
@experiment.my_modules.active
|
|
|
|
elsif @project
|
|
|
|
MyModule.active.where(projects: { id: @project.id })
|
|
|
|
else
|
|
|
|
MyModule.active
|
|
|
|
end
|
|
|
|
|
|
|
|
tasks = tasks.viewable_by_user(current_user, current_team)
|
|
|
|
|
|
|
|
tasks = tasks.joins(experiment: :project)
|
|
|
|
.where(experiments: { archived: false })
|
|
|
|
.where(projects: { archived: false })
|
|
|
|
|
|
|
|
if task_filters[:mode] == 'assigned'
|
|
|
|
assigned_tasks = tasks.joins(:user_my_modules).where(user_my_modules: { user_id: current_user.id }).select(:id)
|
|
|
|
tasks = tasks.where(id: assigned_tasks)
|
|
|
|
end
|
|
|
|
|
|
|
|
tasks = tasks.where(my_module_status_id: task_filters[:statuses]) if task_filters[:statuses].present?
|
|
|
|
|
|
|
|
case task_filters[:sort]
|
|
|
|
when 'start_date'
|
|
|
|
tasks = tasks.order('my_modules.started_on': :asc).order('my_modules.name': :asc)
|
|
|
|
when 'due_date'
|
|
|
|
tasks = tasks.order('my_modules.due_date': :asc).order('my_modules.name': :asc)
|
|
|
|
when 'atoz'
|
|
|
|
tasks = tasks.order('my_modules.name': :asc)
|
|
|
|
when 'ztoa'
|
|
|
|
tasks = tasks.order('my_modules.name': :desc)
|
|
|
|
end
|
|
|
|
|
|
|
|
tasks = tasks.search_by_name(current_user, current_team, task_filters[:query]) if task_filters[:query].present?
|
|
|
|
tasks.joins(:my_module_status)
|
|
|
|
.select(
|
|
|
|
'my_modules.*',
|
|
|
|
'my_module_statuses.name AS status_name',
|
|
|
|
'my_module_statuses.color AS status_color',
|
|
|
|
'projects.name AS project_name',
|
|
|
|
'experiments.name AS experiment_name'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-02-28 18:51:08 +08:00
|
|
|
def check_task_view_permissions
|
2020-11-06 17:39:04 +08:00
|
|
|
render_403 && return if @project && !can_read_project?(@project)
|
|
|
|
render_403 && return if @experiment && !can_read_experiment?(@experiment)
|
2020-02-26 18:07:17 +08:00
|
|
|
end
|
2020-02-14 19:07:40 +08:00
|
|
|
end
|
|
|
|
end
|