2023-11-10 20:34:36 +08:00
|
|
|
module Lists
|
|
|
|
class ProjectsService < BaseService
|
|
|
|
def initialize(team, user, folder, params)
|
|
|
|
@team = team
|
|
|
|
@user = user
|
|
|
|
@current_folder = folder
|
|
|
|
@params = params
|
2023-11-24 18:08:28 +08:00
|
|
|
@filters = params[:filters] || {}
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = []
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
projects = fetch_projects
|
|
|
|
folders = fetch_project_folders
|
|
|
|
|
|
|
|
projects = filter_project_records(projects)
|
|
|
|
folders = filter_project_folder_records(folders)
|
|
|
|
|
2023-11-24 18:08:28 +08:00
|
|
|
if @filters[:folder_search].present? && @filters[:folder_search] == 'true'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = projects
|
2023-11-24 18:08:28 +08:00
|
|
|
else
|
|
|
|
projects = projects.where(project_folder: @current_folder)
|
|
|
|
folders = folders.where(parent_folder: @current_folder)
|
2023-11-10 20:34:36 +08:00
|
|
|
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = projects + folders
|
2023-11-24 18:08:28 +08:00
|
|
|
end
|
2023-11-10 20:34:36 +08:00
|
|
|
|
2023-12-12 19:17:38 +08:00
|
|
|
sort_records
|
|
|
|
paginate_records
|
|
|
|
@records
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fetch_projects
|
|
|
|
@team.projects
|
2023-11-28 16:41:10 +08:00
|
|
|
.includes(:team, user_assignments: %i(user user_role))
|
|
|
|
.includes(:project_comments, experiments: { my_modules: { my_module_status: :my_module_status_implications } })
|
|
|
|
.visible_to(@user, @team)
|
|
|
|
.left_outer_joins(:project_comments)
|
|
|
|
.select('projects.*')
|
|
|
|
.select('COUNT(DISTINCT comments.id) AS comment_count')
|
|
|
|
.group('projects.id')
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_project_folders
|
|
|
|
project_folders = @team.project_folders
|
2023-11-28 16:41:10 +08:00
|
|
|
.includes(:team)
|
|
|
|
.joins('LEFT OUTER JOIN project_folders child_folders
|
2023-11-10 20:34:36 +08:00
|
|
|
ON child_folders.parent_folder_id = project_folders.id')
|
2023-11-28 16:41:10 +08:00
|
|
|
.left_outer_joins(:projects)
|
2023-11-10 20:34:36 +08:00
|
|
|
project_folders.select('project_folders.*')
|
2023-11-28 16:41:10 +08:00
|
|
|
.select('COUNT(DISTINCT projects.id) AS projects_count')
|
|
|
|
.select('COUNT(DISTINCT child_folders.id) AS folders_count')
|
|
|
|
.group('project_folders.id')
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_project_records(records)
|
2023-11-24 18:08:28 +08:00
|
|
|
records = records.archived if @params[:view_mode] == 'archived'
|
|
|
|
records = records.active if @params[:view_mode] == 'active'
|
2023-11-10 20:34:36 +08:00
|
|
|
|
2024-01-26 16:29:39 +08:00
|
|
|
if @params[:search].present?
|
|
|
|
records = records.where_attributes_like(['projects.name', Project::PREFIXED_ID_SQL], @params[:search])
|
|
|
|
end
|
|
|
|
|
2023-11-24 18:08:28 +08:00
|
|
|
if @filters[:query].present?
|
|
|
|
records = records.where_attributes_like(['projects.name', Project::PREFIXED_ID_SQL], @filters[:query])
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
2023-11-24 18:08:28 +08:00
|
|
|
|
|
|
|
if @filters[:members].present?
|
2024-01-09 01:33:28 +08:00
|
|
|
records = records.joins(:user_assignments).where(user_assignments: { user_id: @filters[:members].values })
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
2024-01-09 01:33:28 +08:00
|
|
|
if @filters[:created_at_from].present?
|
2023-11-28 16:41:10 +08:00
|
|
|
records = records.where('projects.created_at > ?',
|
2024-01-09 01:33:28 +08:00
|
|
|
@filters[:created_at_from])
|
2023-11-28 16:41:10 +08:00
|
|
|
end
|
2024-01-09 01:33:28 +08:00
|
|
|
records = records.where('projects.created_at < ?', @filters[:created_at_to]) if @filters[:created_at_to].present?
|
2023-11-28 16:41:10 +08:00
|
|
|
if @filters[:archived_on_to].present?
|
|
|
|
records = records.where('projects.archived_on < ?',
|
|
|
|
@filters[:archived_on_to])
|
|
|
|
end
|
2023-11-24 18:08:28 +08:00
|
|
|
if @filters[:archived_on_from].present?
|
|
|
|
records = records.where('projects.archived_on > ?', @filters[:archived_on_from])
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
|
|
|
records
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_project_folder_records(records)
|
2023-12-01 07:01:08 +08:00
|
|
|
records = records.archived if @params[:view_mode] == 'archived'
|
|
|
|
records = records.active if @params[:view_mode] == 'active'
|
2023-11-24 18:08:28 +08:00
|
|
|
records = records.where_attributes_like('project_folders.name', @filters[:query]) if @filters[:query].present?
|
2024-01-26 16:29:39 +08:00
|
|
|
records = records.where_attributes_like('project_folders.name', @params[:search]) if @params[:search].present?
|
2023-11-10 20:34:36 +08:00
|
|
|
records
|
|
|
|
end
|
|
|
|
|
2023-12-12 19:17:38 +08:00
|
|
|
def sort_records
|
|
|
|
return unless @params[:order]
|
2023-11-10 20:34:36 +08:00
|
|
|
|
|
|
|
sort = "#{order_params[:column]}_#{sort_direction(order_params)}"
|
|
|
|
|
|
|
|
case sort
|
|
|
|
when 'created_at_ASC'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.sort_by(&:created_at).reverse!
|
2023-11-10 20:34:36 +08:00
|
|
|
when 'created_at_DESC'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.sort_by(&:created_at)
|
2023-11-10 20:34:36 +08:00
|
|
|
when 'name_ASC'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.sort_by { |c| c.name.downcase }
|
2023-11-10 20:34:36 +08:00
|
|
|
when 'name_DESC'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.sort_by { |c| c.name.downcase }.reverse!
|
2023-11-10 20:34:36 +08:00
|
|
|
when 'code_ASC'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.sort_by(&:id)
|
2023-11-10 20:34:36 +08:00
|
|
|
when 'code_DESC'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.sort_by(&:id).reverse!
|
2023-11-10 20:34:36 +08:00
|
|
|
when 'archived_on_ASC'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.sort_by(&:archived_on)
|
2023-11-10 20:34:36 +08:00
|
|
|
when 'archived_on_DESC'
|
2023-12-12 19:17:38 +08:00
|
|
|
@records = @records.sort_by(&:archived_on).reverse!
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'users_ASC'
|
2024-03-04 17:56:46 +08:00
|
|
|
@records = @records.sort_by { |object| project_users_count(object) }
|
2024-02-27 20:34:55 +08:00
|
|
|
when 'users_DESC'
|
2024-03-04 17:56:46 +08:00
|
|
|
@records = @records.sort_by { |object| project_users_count(object) }.reverse!
|
2024-02-28 20:31:42 +08:00
|
|
|
when 'updated_at_ASC'
|
|
|
|
@records = @records.sort_by(&:updated_at).reverse!
|
|
|
|
when 'updated_at_DESC'
|
|
|
|
@records = @records.sort_by(&:updated_at)
|
2024-03-04 17:56:46 +08:00
|
|
|
when 'comments_ASC'
|
|
|
|
@records = @records.sort_by { |object| project_comments_count(object) }
|
|
|
|
when 'comments_DESC'
|
|
|
|
@records = @records.sort_by { |object| project_comments_count(object) }.reverse!
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-12-12 19:17:38 +08:00
|
|
|
def paginate_records
|
|
|
|
@records = Kaminari.paginate_array(@records).page(@params[:page]).per(@params[:per_page])
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
2024-03-04 17:56:46 +08:00
|
|
|
|
|
|
|
def project_comments_count(object)
|
|
|
|
project?(object) ? object.comments.count : -1
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_users_count(object)
|
|
|
|
project?(object) ? object.users.count : 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def project?(object)
|
|
|
|
object.instance_of?(Project)
|
|
|
|
end
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
|
|
|
end
|