2020-03-06 20:51:18 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Dashboard
|
|
|
|
class RecentWorkService
|
|
|
|
include InputSanitizeHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
|
|
|
def initialize(user, team, mode)
|
|
|
|
@user = user
|
|
|
|
@team = team
|
|
|
|
@mode = mode
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
visible_projects = Project.viewable_by_user(@user, @team)
|
2020-03-09 22:10:51 +08:00
|
|
|
|
2020-03-11 21:30:41 +08:00
|
|
|
activities = Activity.where(owner_id: @user.id)
|
2020-03-11 18:34:59 +08:00
|
|
|
.where('((project_id IS NULL AND team_id = ?) OR project_id IN (?))',
|
|
|
|
@team.id,
|
|
|
|
visible_projects.pluck(:id))
|
2020-07-23 20:22:58 +08:00
|
|
|
.where.not(type_of: Extends::DASHBOARD_BLACKLIST_ACTIVITY_TYPES)
|
2020-03-11 18:34:59 +08:00
|
|
|
.select('MAX(created_at) AS last_change',
|
|
|
|
:subject_id,
|
|
|
|
:subject_type)
|
|
|
|
.group(:subject_id, :subject_type)
|
|
|
|
.order(last_change: :desc)
|
|
|
|
|
|
|
|
query = Activity.from("(#{activities.to_sql}) AS activities")
|
2020-03-11 21:30:41 +08:00
|
|
|
.results_joins
|
|
|
|
.protocols_joins
|
|
|
|
.my_modules_joins(:from_results, :from_protocols)
|
|
|
|
.experiments_joins(:from_my_modules)
|
|
|
|
.projects_joins(:from_experiments)
|
|
|
|
.repositories_joins
|
|
|
|
.reports_joins
|
2020-06-22 20:57:35 +08:00
|
|
|
.where('repositories.archived IS NOT TRUE')
|
2020-03-11 18:34:59 +08:00
|
|
|
.where('projects.archived IS NOT TRUE')
|
|
|
|
.where('experiments.archived IS NOT TRUE')
|
|
|
|
.where('my_modules.archived IS NOT TRUE')
|
|
|
|
.where('protocols.protocol_type != ? OR protocols.protocol_type IS NULL',
|
|
|
|
Protocol.protocol_types[:in_repository_archived])
|
|
|
|
.select('
|
2020-03-09 22:10:51 +08:00
|
|
|
CASE
|
|
|
|
WHEN my_modules.id IS NOT NULL THEN
|
|
|
|
CONCAT(\'tsk\', my_modules.id)
|
|
|
|
WHEN experiments.id IS NOT NULL THEN
|
|
|
|
CONCAT(\'exp\', experiments.id)
|
|
|
|
WHEN projects.id IS NOT NULL THEN
|
|
|
|
CONCAT(\'pro\', projects.id)
|
|
|
|
WHEN protocols.id IS NOT NULL THEN
|
|
|
|
CONCAT(\'prt\', protocols.id)
|
|
|
|
WHEN repositories.id IS NOT NULL THEN
|
|
|
|
CONCAT(\'inv\', repositories.id)
|
|
|
|
WHEN reports.id IS NOT NULL THEN
|
|
|
|
CONCAT(\'rpt\', reports.id)
|
2020-03-11 18:34:59 +08:00
|
|
|
END AS group_id,
|
|
|
|
|
|
|
|
COALESCE (
|
|
|
|
my_modules.name,
|
|
|
|
experiments.name,
|
|
|
|
projects.name,
|
|
|
|
protocols.name,
|
|
|
|
repositories.name,
|
2020-03-09 22:10:51 +08:00
|
|
|
reports.name
|
2020-03-11 18:34:59 +08:00
|
|
|
) AS name,
|
2020-03-09 22:10:51 +08:00
|
|
|
|
2020-03-11 18:34:59 +08:00
|
|
|
reports.project_id AS report_project_id,
|
2020-03-09 22:10:51 +08:00
|
|
|
subject_id,
|
|
|
|
subject_type,
|
|
|
|
last_change
|
2020-03-06 20:51:18 +08:00
|
|
|
')
|
|
|
|
|
2020-03-11 18:34:59 +08:00
|
|
|
ordered_query = Activity.from("(#{query.to_sql}) AS activities").where.not(group_id: nil)
|
|
|
|
.select(:group_id,
|
|
|
|
:name,
|
|
|
|
'MAX(last_change) AS last_change',
|
|
|
|
'MAX(report_project_id) AS report_project_id')
|
|
|
|
.group(:group_id, :name)
|
|
|
|
.order('MAX(last_change) DESC').limit(Constants::SEARCH_LIMIT)
|
|
|
|
|
|
|
|
query_filter = "(group_id LIKE 'tsk%' OR group_id LIKE 'exp%' OR group_id LIKE 'pro%')" if @mode == 'projects'
|
|
|
|
query_filter = "group_id LIKE 'prt%'" if @mode == 'protocols'
|
|
|
|
query_filter = "group_id LIKE 'inv%'" if @mode == 'repositories'
|
|
|
|
query_filter = "group_id LIKE 'rpt%'" if @mode == 'reports'
|
|
|
|
ordered_query = ordered_query.where(query_filter) unless @mode == 'all'
|
|
|
|
|
|
|
|
recent_objects = ordered_query.as_json.map do |recent_object|
|
|
|
|
recent_object.deep_symbolize_keys!
|
|
|
|
recent_object.delete_if { |_k, v| v.nil? }
|
|
|
|
|
|
|
|
recent_object[:last_change] = I18n.l(
|
|
|
|
DateTime.parse(recent_object[:last_change]).in_time_zone(@user.settings[:time_zone] || 'UTC'),
|
2020-03-06 22:24:57 +08:00
|
|
|
format: :full_with_comma
|
|
|
|
)
|
2020-03-11 18:34:59 +08:00
|
|
|
recent_object[:subject_type] = override_subject_type(recent_object)
|
|
|
|
recent_object[:name] = escape_input(recent_object[:name])
|
|
|
|
recent_object[:url] = generate_url(recent_object)
|
|
|
|
recent_object
|
2020-03-09 22:10:51 +08:00
|
|
|
end
|
2020-03-06 20:51:18 +08:00
|
|
|
|
2020-03-11 18:34:59 +08:00
|
|
|
recent_objects
|
2020-03-06 20:51:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-11 18:34:59 +08:00
|
|
|
def generate_url(recent_object)
|
|
|
|
object_id = recent_object[:group_id].gsub(/[^0-9]/, '')
|
|
|
|
|
|
|
|
case recent_object[:subject_type]
|
2020-03-06 20:51:18 +08:00
|
|
|
when 'MyModule'
|
2020-03-11 18:34:59 +08:00
|
|
|
protocols_my_module_path(object_id)
|
2020-03-06 20:51:18 +08:00
|
|
|
when 'Experiment'
|
2020-03-11 18:34:59 +08:00
|
|
|
canvas_experiment_path(object_id)
|
2020-03-06 20:51:18 +08:00
|
|
|
when 'Project'
|
2020-03-11 18:34:59 +08:00
|
|
|
project_path(object_id)
|
2020-03-06 20:51:18 +08:00
|
|
|
when 'Protocol'
|
2020-03-11 18:34:59 +08:00
|
|
|
edit_protocol_path(object_id)
|
2020-04-09 18:33:04 +08:00
|
|
|
when 'RepositoryBase'
|
2020-03-11 18:34:59 +08:00
|
|
|
repository_path(object_id)
|
2020-03-06 20:51:18 +08:00
|
|
|
when 'Report'
|
2020-03-11 18:34:59 +08:00
|
|
|
edit_project_report_path(recent_object[:report_project_id], object_id) if recent_object[:report_project_id]
|
2020-03-06 20:51:18 +08:00
|
|
|
end
|
|
|
|
end
|
2020-03-06 22:12:40 +08:00
|
|
|
|
2020-03-11 18:34:59 +08:00
|
|
|
def override_subject_type(recent_object)
|
|
|
|
if recent_object[:group_id].include?('pro')
|
2020-03-09 22:10:51 +08:00
|
|
|
'Project'
|
2020-03-11 18:34:59 +08:00
|
|
|
elsif recent_object[:group_id].include?('exp')
|
2020-03-09 22:10:51 +08:00
|
|
|
'Experiment'
|
2020-03-11 18:34:59 +08:00
|
|
|
elsif recent_object[:group_id].include?('tsk')
|
2020-03-09 22:10:51 +08:00
|
|
|
'MyModule'
|
2020-03-11 18:34:59 +08:00
|
|
|
elsif recent_object[:group_id].include?('prt')
|
2020-03-09 22:10:51 +08:00
|
|
|
'Protocol'
|
2020-03-11 18:34:59 +08:00
|
|
|
elsif recent_object[:group_id].include?('inv')
|
2020-04-09 18:33:04 +08:00
|
|
|
'RepositoryBase'
|
2020-03-11 18:34:59 +08:00
|
|
|
elsif recent_object[:group_id].include?('rpt')
|
2020-03-09 22:10:51 +08:00
|
|
|
'Report'
|
|
|
|
end
|
|
|
|
end
|
2020-03-06 20:51:18 +08:00
|
|
|
end
|
|
|
|
end
|