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-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'
|
|
|
|
records = projects
|
|
|
|
else
|
|
|
|
projects = projects.where(project_folder: @current_folder)
|
|
|
|
folders = folders.where(parent_folder: @current_folder)
|
2023-11-10 20:34:36 +08:00
|
|
|
|
2023-11-24 18:08:28 +08:00
|
|
|
records = projects + folders
|
|
|
|
end
|
2023-11-10 20:34:36 +08:00
|
|
|
|
|
|
|
records = sort_records(records)
|
|
|
|
paginate_records(records)
|
|
|
|
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
|
|
|
|
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?
|
|
|
|
records = records.joins(:user_assignments).where(user_assignments: { user_id: @filters[:members] })
|
2023-11-10 20:34:36 +08:00
|
|
|
end
|
2023-11-28 16:41:10 +08:00
|
|
|
if @filters[:created_on_from].present?
|
|
|
|
records = records.where('projects.created_at > ?',
|
|
|
|
@filters[:created_on_from])
|
|
|
|
end
|
2023-11-24 18:08:28 +08:00
|
|
|
records = records.where('projects.created_at < ?', @filters[:created_on_to]) if @filters[:created_on_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?
|
2023-11-10 20:34:36 +08:00
|
|
|
records
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort_records(records)
|
|
|
|
return records unless @params[:order]
|
|
|
|
|
|
|
|
sort = "#{order_params[:column]}_#{sort_direction(order_params)}"
|
|
|
|
|
|
|
|
case sort
|
|
|
|
when 'created_at_ASC'
|
|
|
|
records.sort_by(&:created_at).reverse!
|
|
|
|
when 'created_at_DESC'
|
|
|
|
records.sort_by(&:created_at)
|
|
|
|
when 'name_ASC'
|
|
|
|
records.sort_by { |c| c.name.downcase }
|
|
|
|
when 'name_DESC'
|
|
|
|
records.sort_by { |c| c.name.downcase }.reverse!
|
|
|
|
when 'code_ASC'
|
|
|
|
records.sort_by(&:id)
|
|
|
|
when 'code_DESC'
|
|
|
|
records.sort_by(&:id).reverse!
|
|
|
|
when 'archived_on_ASC'
|
|
|
|
records.sort_by(&:archived_on)
|
|
|
|
when 'archived_on_DESC'
|
|
|
|
records.sort_by(&:archived_on).reverse!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def paginate_records(records)
|
|
|
|
Kaminari.paginate_array(records).page(@params[:page]).per(@params[:per_page])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|