Verified Commit 08f76667 authored by Tam Le's avatar Tam Le
Browse files

Filter backend

Showing with 114 additions and 10 deletions
+114 -10
class AssignsController < ApplicationController
#layout 'admin'
before_action :find_project_by_project_id
include SPAssignmentManager
......@@ -74,10 +74,6 @@ class AssignsController < ApplicationController
end
end
def select_to_transfer
return head 403 unless @can_assign = User.current.allowed_to?(:assign_service_packs, @project)
end
def report
return head 403 unless User.current.allowed_to?(:see_assigned_service_packs, @project)
if assignment = assigned?(@project)
......
......@@ -23,6 +23,7 @@ class ServicePacksController < ApplicationController
# controller chooses not to get the thresholds.
# assume the service pack exists.
# TODO: make a separate action JSON only.
binding.pry
respond_to do |format|
format.json {
# the function already converted this to json
......
class SpReportController < ApplicationController
before_action :find_project_by_project_id
include ServicePacksReportHelper
def report
# json & html endpoint
# params[:service_pack, :start_date, :end_date]
spid = params[:service_pack_id]
if spid.present?
@fault = true and render_404 unless sp = ServicePack.find_by(id: spid)
end
sql = query(service_pack: sp, project: @project,
start_date: params[:start_date]&.to_date,
end_date: params[:end_date]&.to_date)
respond_to do |format|
format.html {
# change this to debug
render plain: sp_available
}
format.json {
render json: @entries
}
format.csv {
# todo
}
end
end
def proj_available
get_projects_available.pluck(:id, :name)
end
def sp_available
Assign.active.joins(:service_pack)
.where(project_id: get_projects_available.pluck(:id))
.pluck(-'service_packs.id', 'service_packs.name')
end
private
def get_projects_available
@projects ||= Project.allowed_to(User.current, :see_assigned_service_packs)
end
end
\ No newline at end of file
......@@ -9,13 +9,13 @@ class ServicePackReport
end
end
def query(project = nil)
def query(project: nil, date_start: nil, date_end: nil)
sql = <<-SQL
SELECT t2.spent_on, concat(t4.firstname, ' ', t4.lastname) AS user_name, t3.name AS activity_name,
t5.id AS work_package_id, t7.name AS type_name, t5.subject AS subject, t2.comments AS comment,
t1.units AS units, t2.hours AS hours, #{project.nil? ? 't6.name' : "'#{project.name}'"} AS project_name
FROM service_pack_entries t1
INNER JOIN #{TimeEntry.table_name} t2
LEFT JOIN #{TimeEntry.table_name} t2
ON t1.time_entry_id = t2.id
INNER JOIN #{TimeEntryActivity.table_name} t3
ON t2.activity_id = t3.id
......@@ -26,7 +26,7 @@ class ServicePackReport
ON t2.work_package_id = t5.id
LEFT JOIN types t7
ON t5.type_id = t7.id
WHERE service_pack_id = #{@service_pack.id}
WHERE t1.service_pack_id = #{@service_pack.id}
#{project.nil? ? '' : "AND t2.project_id = #{project.id}"}
ORDER BY spent_on DESC
SQL
......
module ServicePacksReportHelper
def query(service_pack: nil, project: nil, start_date: nil, end_date: nil)
# binding.pry
proj_clause = <<-SQL
SELECT t2.spent_on, concat(t4.firstname, ' ', t4.lastname) AS user_name,
t3.name AS activity_name, t5.id AS work_package_id, t5.subject AS subject,
t2.comments AS comment, t1.units AS units, t2.hours AS hours, t6.name AS type_name
SQL
query_to_sanitize = [proj_clause]
from_clause = <<-SQL
FROM service_pack_entries t1
LEFT JOIN #{TimeEntry.table_name} t2
ON t1.time_entry_id = t2.id
INNER JOIN #{TimeEntryActivity.table_name} t3
ON t2.activity_id = t3.id
INNER JOIN users t4
ON t2.user_id = t4.id
LEFT JOIN #{WorkPackage.table_name} t5
ON t2.work_package_id = t5.id
LEFT JOIN types t6
ON t5.type_id = t6.id
SQL
where_clause = 'WHERE 1 = 1'
dts = start_date&.to_s
ets = (end_date&.next_day)&.to_s
where_clause << " AND t1.created_at >= '#{dts}' " if dts
where_clause << " AND t1.created_at < '#{ets}' " if ets
if project&.id # project given?
# specific project
where_clause << " AND t2.project_id = #{project.id} "
proj_clause << -', ? AS project_name '
query_to_sanitize << project.name
else
# all project
from_clause << -' INNER JOIN projects t7 ON t2.project_id = t7.id '
proj_clause << -', t7.name AS project_name '
end
if service_pack&.id # SP given?
# specific SP
where_clause << " AND t1.service_pack_id = #{service_pack.id} "
proj_clause << -', ? AS sp_name '
query_to_sanitize << service_pack.name
else
# all SP
from_clause << -' INNER JOIN service_packs t8 ON t1.service_pack_id = t8.id '
proj_clause << -', t8.name AS sp_name'
end
proj_clause << from_clause << where_clause
proj_clause << -' ORDER BY spent_on DESC '
sql = ActiveRecord::Base.send(:sanitize_sql_array, query_to_sanitize)
@entries = ActiveRecord::Base.connection.exec_query(sql)
sql
end
end
\ No newline at end of file
# freeze_literal_string: true
OpenProject::Application.routes.draw do
resources :service_packs do
get '/statistics', to: 'service_packs#statistics', constraints: lambda { |req| req.format == :json }
end
scope "/projects/:project_id" do
scope '/projects/:project_id' do
get '/assigns', to: 'assigns#show'
get '/assigns/report', to: 'assigns#report'
post '/assigns/assign', to: 'assigns#assign'
post '/assigns/unassign', to: 'assigns#unassign'
get '/assigns/statistics', to: 'assigns#statistics', constraints: lambda { |req| req.format == :json }
get '/assigns/:assignment_id/transfer', to: 'assigns#select_to_transfer', as: 'sp_transfer'
get '/sp_report', to: 'sp_report#report', as: 'sp_report'
end
end
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment