scinote-web/app/services/dashboard/recent_work_service.rb

142 lines
5.6 KiB
Ruby
Raw Normal View History

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)
.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)
.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
.where('repositories.archived IS NOT TRUE')
.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)
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
) AS name,
2020-03-09 22:10:51 +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
')
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
)
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
recent_objects
2020-03-06 20:51:18 +08:00
end
private
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'
protocols_my_module_path(object_id)
2020-03-06 20:51:18 +08:00
when 'Experiment'
canvas_experiment_path(object_id)
2020-03-06 20:51:18 +08:00
when 'Project'
project_path(object_id)
2020-03-06 20:51:18 +08:00
when 'Protocol'
edit_protocol_path(object_id)
when 'RepositoryBase'
repository_path(object_id)
2020-03-06 20:51:18 +08:00
when 'Report'
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
def override_subject_type(recent_object)
if recent_object[:group_id].include?('pro')
2020-03-09 22:10:51 +08:00
'Project'
elsif recent_object[:group_id].include?('exp')
2020-03-09 22:10:51 +08:00
'Experiment'
elsif recent_object[:group_id].include?('tsk')
2020-03-09 22:10:51 +08:00
'MyModule'
elsif recent_object[:group_id].include?('prt')
2020-03-09 22:10:51 +08:00
'Protocol'
elsif recent_object[:group_id].include?('inv')
'RepositoryBase'
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