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-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-02-28 18:51:08 +08:00
|
|
|
tasks = if @experiment
|
|
|
|
@experiment.my_modules.active
|
|
|
|
elsif @project
|
|
|
|
MyModule.active.joins(:experiment).where('experiments.project_id': @project.id)
|
2020-02-26 18:07:17 +08:00
|
|
|
else
|
|
|
|
MyModule.active.viewable_by_user(current_user, current_team)
|
|
|
|
end
|
|
|
|
if task_filters[:mode] == 'assigned'
|
2020-02-21 17:41:42 +08:00
|
|
|
tasks = tasks.left_outer_joins(:user_my_modules).where('user_my_modules.user_id': current_user.id)
|
|
|
|
end
|
2020-02-26 18:07:17 +08:00
|
|
|
tasks = tasks.where('my_modules.state': task_filters[:view])
|
2020-02-21 17:41:42 +08:00
|
|
|
|
2020-02-26 18:07:17 +08:00
|
|
|
case task_filters[:sort]
|
2020-02-21 17:41:42 +08:00
|
|
|
when 'date_desc'
|
|
|
|
tasks = tasks.order('my_modules.due_date': :desc)
|
|
|
|
when 'date_asc'
|
|
|
|
tasks = tasks.order('my_modules.due_date': :asc)
|
|
|
|
when 'atoz'
|
|
|
|
tasks = tasks.order('my_modules.name': :asc)
|
|
|
|
when 'ztoa'
|
|
|
|
tasks = tasks.order('my_modules.name': :desc)
|
|
|
|
else
|
|
|
|
tasks
|
|
|
|
end
|
|
|
|
|
2020-03-02 23:11:14 +08:00
|
|
|
page = (params[:page] || 1).to_i
|
|
|
|
tasks_per_page = tasks.page(page).per(Constants::INFINITE_SCROLL_LIMIT)
|
2020-02-28 23:17:32 +08:00
|
|
|
|
2020-03-02 23:11:14 +08:00
|
|
|
tasks_per_page = tasks_per_page.with_step_statistics.preload(experiment: :project)
|
|
|
|
tasks_list = tasks_per_page.map do |task|
|
|
|
|
{ id: task.id,
|
|
|
|
link: protocols_my_module_path(task.id),
|
|
|
|
experiment: escape_input(task.experiment.name),
|
|
|
|
project: escape_input(task.experiment.project.name),
|
|
|
|
name: escape_input(task.name),
|
|
|
|
due_date: task.due_date.present? ? I18n.l(task.due_date, format: :full_with_comma) : nil,
|
|
|
|
overdue: task.is_overdue?,
|
|
|
|
state: task.state,
|
|
|
|
steps_state: { completed_steps: task.steps_completed,
|
|
|
|
all_steps: task.steps_total,
|
|
|
|
percentage: task.steps_completed_percentage } }
|
2020-02-21 17:41:42 +08:00
|
|
|
end
|
2020-03-02 23:11:14 +08:00
|
|
|
|
|
|
|
render json: { data: tasks_list, next_page: tasks_per_page.next_page }
|
2020-02-21 17:41:42 +08:00
|
|
|
end
|
2020-02-17 17:47:10 +08:00
|
|
|
|
|
|
|
def project_filter
|
2020-02-17 22:32:43 +08:00
|
|
|
projects = current_team.projects.search(current_user, false, params[:query], 1, current_team).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-02-28 18:51:08 +08:00
|
|
|
experiments = @project.experiments.search(current_user, false, params[:query], 1, current_team).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
|
|
|
|
|
|
|
|
def task_filters
|
2020-03-02 23:11:14 +08:00
|
|
|
params.permit(:project_id, :experiment_id, :mode, :view, :sort, :page)
|
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
|
|
|
|
2020-02-28 18:51:08 +08:00
|
|
|
def check_task_view_permissions
|
|
|
|
render_403 if @project && !can_read_project?(@project)
|
|
|
|
render_403 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
|