Refactoring current tasks backend

This commit is contained in:
Mojca Lorber 2020-02-28 11:51:08 +01:00
parent 415721112a
commit 3c09923d96
3 changed files with 39 additions and 36 deletions

View file

@ -33,7 +33,7 @@ var DasboardCurrentTasksWidget = (function() {
$currentTasksList.find('.current-task-item').remove();
$.each(data.tasks_list, (i, task) => {
var currentTaskItem;
var stepsPercentage = (task.steps_state === 0) ? '' : task.steps_state.percentage + '%';
var stepsPercentage = task.steps_state.percentage + '%';
var stateText;
var dueDate = (task.due_date !== null) ? '<i class="fas fa-calendar-day"></i>'
+ I18n.t('dashboard.current_tasks.due_date', { date: task.due_date }) : '';
@ -43,7 +43,7 @@ var DasboardCurrentTasksWidget = (function() {
} else {
stateText = I18n.t('dashboard.current_tasks.progress_bar.in_progress');
if (task.overdue) { stateText = I18n.t('dashboard.current_tasks.progress_bar.overdue'); }
if (task.steps_state !== 0) {
if (task.steps_state.all_steps !== 0) {
stateText += I18n.t('dashboard.current_tasks.progress_bar.completed_steps',
{ steps: task.steps_state.completed_steps, total_steps: task.steps_state.all_steps });
}

View file

@ -4,16 +4,15 @@ module Dashboard
class CurrentTasksController < ApplicationController
include InputSanitizeHelper
before_action :check_view_permissions, only: :show
before_action :load_project, only: %i(show experiment_filter)
before_action :load_experiment, only: :show
before_action :check_task_view_permissions, only: :show
def show
project = current_team.projects.find_by(id: task_filters[:project_id]) if task_filters[:project_id]
experiment = project.experiments.find_by(id: task_filters[:experiment_id]) if task_filters[:experiment_id]
tasks = if experiment
experiment.my_modules.active
elsif project
MyModule.active.joins(:experiment).where('experiments.project_id': project.id)
tasks = if @experiment
@experiment.my_modules.active
elsif @project
MyModule.active.joins(:experiment).where('experiments.project_id': @project.id)
else
MyModule.active.viewable_by_user(current_user, current_team)
end
@ -66,12 +65,11 @@ module Dashboard
end
def experiment_filter
project = current_team.projects.find_by(id: params[:project_id])
unless project
unless @project
render json: []
return false
end
experiments = project.experiments.search(current_user, false, params[:query], 1, current_team).select(:id, :name)
experiments = @project.experiments.search(current_user, false, params[:query], 1, current_team).select(:id, :name)
unless params[:mode] == 'team'
experiments = experiments.where(id: current_user.my_modules
.group(:experiment_id).select(:experiment_id).pluck(:experiment_id))
@ -87,12 +85,17 @@ module Dashboard
)
end
def check_view_permissions
experiment = Experiment.find_by_id(params[:experiment_id])
project = Project.find_by_id(params[:project_id])
def load_project
@project = current_team.projects.find_by(id: params[:project_id])
end
render_403 if project && !can_read_project?(project)
render_403 if experiment && !can_read_experiment?(experiment)
def load_experiment
@experiment = @project.experiments.find_by(id: params[:experiment_id]) if @project
end
def check_task_view_permissions
render_403 if @project && !can_read_project?(@project)
render_403 if @experiment && !can_read_experiment?(@experiment)
end
end
end

View file

@ -493,27 +493,27 @@ class MyModule < ApplicationRecord
end
def completed_steps_percentage
if protocol && protocol.steps.any?
steps_percentage_sql = <<-SQL
SELECT
COUNT(steps.id) AS all,
SUM (CASE WHEN steps.completed = TRUE THEN 1 ELSE 0 END) AS completed,
SUM (CASE WHEN steps.completed = TRUE THEN 1 ELSE 0 END)* 100 / (COUNT(steps.id)) AS percentage
FROM my_modules
INNER JOIN protocols ON protocols.my_module_id = my_modules.id
INNER JOIN steps ON steps.protocol_id = protocols.id
WHERE my_modules.id = #{id}
GROUP BY my_modules.id
ORDER BY my_modules.id
SQL
steps_percentage = ActiveRecord::Base.connection.execute(steps_percentage_sql)
if protocol.steps.any?
steps_percentage =
MyModule.joins(protocols: :steps)
.where(id: id)
.group(:id)
.select('my_modules.*')
.select('COUNT(steps.id) AS all')
.select('COUNT(steps.id) FILTER (where steps.completed = true) AS completed')
.select('((COUNT(steps.id) FILTER (where steps.completed = true)) * 100 / COUNT(steps.id)) AS percentage')
.take
{
completed_steps: steps_percentage[0]['completed'],
all_steps: steps_percentage[0]['all'],
percentage: steps_percentage[0]['percentage']
completed_steps: steps_percentage.completed,
all_steps: steps_percentage.all,
percentage: steps_percentage.percentage
}
else
0
{
completed_steps: 0,
all_steps: 0,
percentage: 0
}
end
end