diff --git a/app/controllers/api/v0/themes_controller.rb b/app/controllers/api/v0/themes_controller.rb index 79b5e6b..241806f 100644 --- a/app/controllers/api/v0/themes_controller.rb +++ b/app/controllers/api/v0/themes_controller.rb @@ -2,22 +2,39 @@ module V0 class ThemesController < Api::V0::BaseController before_action :authenticate +#Theme.find(38).answers.where(plan_id: User.first.plans.pluck(:id)) def extract - @theme = Theme.find(extract_params[:id]) - @answers = @theme.answers + @theme = Theme.find(params[:id]) + @answers = @theme.answers.where(plan_id: @user.plans.pluck(:id)) + admin_answers = [] + org_answers = [] - extract_filtering_params.each do |key, value| - @answers = @answers.public_send(key, value) if value + if params[:admin_visible].present? && params[:admin_visible] + admin_answers = @theme.answers.where(plan_id: @user.org.plans.privately_visible) end + + if params[:org_visible].present? && params[:org_visible] + org_answers = @theme.answers.where(plan_id: @user.org.plans.organisationally_visible) + end + + if params[:template_id].present? && params[:template_id] + @answers = @answers.where(plan_id: @user.plans.where(template_id: params[:template_id]).pluck(:id)) + end + + if params[:question_id].present? && params[:question_id] + @answers = @answers.where(question_id: params[:question_id]) + end + + @answers += admin_answers + org_answers end def extract_params - params.permit(:id, :plan_id, :question_id, :start_date, :end_date) + params.permit(:id, :template_id, :question_id, :start_date, :end_date, :admin_visible, :org_visible) end def extract_filtering_params - extract_params.slice(:plan_id, :question_id, :start_date, :end_date) + extract_params.slice(:template_id, :question_id, :start_date, :end_date, :admin_visible, :org_visible) end end end