2019-02-26 18:01:15 +08:00
|
|
|
# frozen_string_literal: true
|
2019-02-13 23:19:18 +08:00
|
|
|
|
2019-02-26 18:01:15 +08:00
|
|
|
class GlobalActivitiesController < ApplicationController
|
2019-05-08 23:38:24 +08:00
|
|
|
include InputSanitizeHelper
|
|
|
|
|
2021-05-31 19:32:31 +08:00
|
|
|
before_action :check_create_activity_filter_permissions, only: :save_activity_filter
|
|
|
|
|
2019-02-13 23:19:18 +08:00
|
|
|
def index
|
2019-04-02 17:14:36 +08:00
|
|
|
# Preload filter format
|
|
|
|
# {
|
|
|
|
# from_date: "YYYY-MM-DD",
|
|
|
|
# to_date: "YYYY-MM-DD",
|
|
|
|
# teams: [*team_ids],
|
|
|
|
# types: [*activity_type_ids],
|
|
|
|
# users: [*user_ids],
|
|
|
|
# subjects: {
|
|
|
|
# *Object_name: [*object_ids],
|
|
|
|
# ...
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
|
|
|
|
# Example
|
|
|
|
# {
|
|
|
|
# from_date: "2019-03-29",
|
|
|
|
# to_date: "2019-03-29",
|
|
|
|
# teams: [1,2],
|
|
|
|
# types: [32,33,34],
|
|
|
|
# users: [1,2,3],
|
|
|
|
# subjects: {
|
|
|
|
# Project: [1]
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# To prepare URL search query use - Activity.url_search_query(filter)
|
|
|
|
@filters = activity_filters
|
|
|
|
@filters[:subject_labels] = params[:subject_labels]
|
2019-03-20 17:33:22 +08:00
|
|
|
@teams = current_user.teams
|
2019-04-12 21:37:45 +08:00
|
|
|
selected_teams = if activity_filters[:teams].present?
|
2019-03-27 20:49:55 +08:00
|
|
|
@teams.where(id: activity_filters[:teams]).order(name: :asc)
|
2019-03-26 21:54:34 +08:00
|
|
|
else
|
2019-03-27 20:49:55 +08:00
|
|
|
@teams.order(name: :asc)
|
2019-03-26 21:54:34 +08:00
|
|
|
end
|
2019-03-06 17:34:04 +08:00
|
|
|
@activity_types = Activity.activity_types_list
|
2019-03-18 22:38:19 +08:00
|
|
|
@user_list = User.where(id: UserTeam.where(team: current_user.teams).select(:user_id))
|
|
|
|
.distinct
|
2019-03-27 20:49:55 +08:00
|
|
|
.order(full_name: :asc)
|
2019-03-18 22:38:19 +08:00
|
|
|
.pluck(:full_name, :id)
|
2019-04-12 21:37:45 +08:00
|
|
|
|
|
|
|
activities = ActivitiesService.load_activities(current_user, selected_teams, activity_filters)
|
|
|
|
|
|
|
|
@grouped_activities = activities.group_by do |activity|
|
|
|
|
Time.zone.at(activity.created_at).to_date.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
@next_page = activities.next_page
|
2019-04-15 16:51:59 +08:00
|
|
|
@starting_timestamp = activities.first&.created_at.to_i
|
2019-04-12 21:37:45 +08:00
|
|
|
|
2023-07-05 18:43:23 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
activities_html: render_to_string(partial: 'activity_list'),
|
|
|
|
next_page: @next_page,
|
|
|
|
starting_timestamp: @starting_timestamp
|
|
|
|
}
|
|
|
|
end
|
|
|
|
format.html do
|
|
|
|
end
|
|
|
|
end
|
2019-02-26 18:01:15 +08:00
|
|
|
end
|
|
|
|
|
2019-04-09 15:08:27 +08:00
|
|
|
def team_filter
|
2020-01-30 20:42:02 +08:00
|
|
|
render json: current_user.teams.ordered.global_activity_filter(activity_filters, params[:query])
|
2019-04-09 15:08:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_filter
|
|
|
|
filter = activity_filters
|
|
|
|
filter = { subjects: { MyModule: [params[:my_module_id].to_i] } } if params[:my_module_id]
|
|
|
|
render json: current_user.global_activity_filter(filter, params[:query])
|
|
|
|
end
|
|
|
|
|
2019-08-08 21:51:08 +08:00
|
|
|
def project_filter
|
|
|
|
render json: get_objects(Project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def experiment_filter
|
|
|
|
render json: get_objects(Experiment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def my_module_filter
|
|
|
|
render json: get_objects(MyModule)
|
|
|
|
end
|
|
|
|
|
|
|
|
def inventory_filter
|
|
|
|
render json: get_objects(Repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
def inventory_item_filter
|
|
|
|
render json: get_objects(RepositoryRow)
|
|
|
|
end
|
|
|
|
|
|
|
|
def protocol_filter
|
|
|
|
render json: get_objects(Protocol)
|
|
|
|
end
|
|
|
|
|
|
|
|
def report_filter
|
|
|
|
render json: get_objects(Report)
|
|
|
|
end
|
|
|
|
|
2021-05-31 19:32:31 +08:00
|
|
|
def save_activity_filter
|
|
|
|
activity_filter = ActivityFilter.new(activity_filter_params)
|
|
|
|
if activity_filter.save
|
|
|
|
render json: { message: t('global_activities.index.activity_filter_saved') }
|
|
|
|
else
|
|
|
|
render json: { errors: activity_filter.errors.full_messages }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-08 21:51:08 +08:00
|
|
|
private
|
|
|
|
|
2021-05-31 19:32:31 +08:00
|
|
|
def check_create_activity_filter_permissions
|
|
|
|
render_403 && return unless can_create_acitivity_filters?
|
|
|
|
end
|
|
|
|
|
2019-08-08 21:51:08 +08:00
|
|
|
def get_objects(subject)
|
2019-02-26 18:01:15 +08:00
|
|
|
query = subject_search_params[:query]
|
|
|
|
teams =
|
|
|
|
if subject_search_params[:teams].present?
|
|
|
|
current_user.teams.where(id: subject_search_params[:teams])
|
|
|
|
else
|
|
|
|
current_user.teams
|
|
|
|
end
|
2019-04-09 15:08:27 +08:00
|
|
|
filter_teams =
|
|
|
|
if subject_search_params[:users].present?
|
2022-05-13 21:45:24 +08:00
|
|
|
User.where(id: subject_search_params[:users]).joins(:teams).group(:team_id).pluck(:team_id)
|
2019-04-09 15:08:27 +08:00
|
|
|
elsif subject_search_params[:teams].present?
|
|
|
|
subject_search_params[:teams]
|
|
|
|
else
|
2019-04-15 17:20:43 +08:00
|
|
|
teams
|
2019-04-09 15:08:27 +08:00
|
|
|
end
|
2019-08-08 21:51:08 +08:00
|
|
|
matched = subject.search_by_name(current_user, teams, query, whole_phrase: true)
|
|
|
|
.where.not(name: nil).where.not(name: '')
|
|
|
|
.filter_by_teams(filter_teams)
|
2019-08-09 20:31:50 +08:00
|
|
|
.order(name: :asc)
|
2019-02-13 23:19:18 +08:00
|
|
|
|
2019-08-08 21:51:08 +08:00
|
|
|
selected_subject = subject_search_params[:subjects]
|
2022-09-13 18:38:26 +08:00
|
|
|
matched = matched.where(my_module: nil) if subject == Protocol
|
2019-08-09 20:31:50 +08:00
|
|
|
matched = matched.where(project_id: selected_subject['Project']) if subject == Experiment
|
|
|
|
matched = matched.where(experiment_id: selected_subject['Experiment']) if subject == MyModule
|
2020-07-20 21:01:00 +08:00
|
|
|
matched = matched.where(repository_id: selected_subject['RepositoryBase']) if subject == RepositoryRow
|
2021-10-05 19:45:09 +08:00
|
|
|
matched = matched.limit(Constants::SEARCH_LIMIT)
|
2019-08-08 21:51:08 +08:00
|
|
|
|
2021-10-05 19:45:09 +08:00
|
|
|
matched.map { |pr| { value: pr.id, label: escape_input(pr.name) } }
|
2019-08-08 21:51:08 +08:00
|
|
|
end
|
2019-02-13 23:19:18 +08:00
|
|
|
|
2021-05-31 19:32:31 +08:00
|
|
|
def activity_filter_params
|
|
|
|
params.permit(:name, filter: {})
|
|
|
|
end
|
|
|
|
|
2019-02-26 18:01:15 +08:00
|
|
|
def activity_filters
|
|
|
|
params.permit(
|
2019-04-12 21:37:45 +08:00
|
|
|
:page, :starting_timestamp, :from_date, :to_date, types: [], subjects: {}, users: [], teams: []
|
2019-02-26 18:01:15 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def subject_search_params
|
2019-08-08 21:51:08 +08:00
|
|
|
params.permit(:query, teams: [], subject_types: [], users: [], subjects: {})
|
2019-02-13 23:19:18 +08:00
|
|
|
end
|
|
|
|
end
|