diff --git a/app/controllers/api/v0/themes_controller.rb b/app/controllers/api/v0/themes_controller.rb new file mode 100644 index 0000000..ba48b82 --- /dev/null +++ b/app/controllers/api/v0/themes_controller.rb @@ -0,0 +1,49 @@ +module Api + module V0 + class ThemesController < Api::V0::BaseController + before_action :authenticate + + def extract + # check if the user has permissions to use the themes API + @theme = Theme.find(params[:id]) + raise Pundit::NotAuthorizedError unless Api::V0::ThemePolicy.new(@user, @theme).extract? + @answers = @theme.answers.where(plan_id: @user.plans.pluck(:id)) + admin_answers = [] + org_answers = [] + + 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 + + if params[:start_date].present? && params[:start_date] + @answers = @answers.where('answers.created_at >=?', params[:start_date]) + end + + if params[:end_date].present? && params[:end_date] + @answers = @answers.where('answers.created_at <=?', params[:end_date]) + end + + end + + def extract_params + params.permit(:id, :template_id, :question_id, :start_date, :end_date, :admin_visible, :org_visible) + end + + def extract_filtering_params + extract_params.slice(:template_id, :question_id, :start_date, :end_date, :admin_visible, :org_visible) + end + end + end +end diff --git a/app/controllers/super_admin/themes_controller.rb b/app/controllers/super_admin/themes_controller.rb index 75532fe..afdcca8 100644 --- a/app/controllers/super_admin/themes_controller.rb +++ b/app/controllers/super_admin/themes_controller.rb @@ -64,6 +64,7 @@ end redirect_to(action: :index) end + # Private instance methods private diff --git a/app/models/answer.rb b/app/models/answer.rb index 4e8a3e5..fbc8aac 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -30,6 +30,11 @@ :question, :user, :plan, :question_options, :notes, :note_ids, :id, :as => [:default, :admin] + scope :plan_id, ->(id) { where('answers.plan_id = ?', id) } + scope :question_id, ->(id) { where('answers.question_id = ?', id) } + scope :since, ->(date) { where('answers.created_at >= ?', date) if date } + scope :until, ->(date) { where('answers.created_at <= ?', date) if date } + ## # Validations # validates :user, :plan, :question, presence: true diff --git a/app/models/theme.rb b/app/models/theme.rb index 4f0738e..d13ca7b 100644 --- a/app/models/theme.rb +++ b/app/models/theme.rb @@ -4,6 +4,7 @@ # Associations has_and_belongs_to_many :questions, join_table: "questions_themes" has_and_belongs_to_many :guidances, join_table: "themes_in_guidance" + has_many :answers, through: :questions ## # Possibly needed for active_admin diff --git a/app/models/token_permission_type.rb b/app/models/token_permission_type.rb index 5770e6c..34c2b75 100644 --- a/app/models/token_permission_type.rb +++ b/app/models/token_permission_type.rb @@ -20,6 +20,7 @@ PLANS = TokenPermissionType.where(token_type: 'plans').first.freeze TEMPLATES = TokenPermissionType.where(token_type: 'templates').first.freeze STATISTICS = TokenPermissionType.where(token_type: 'statistics').first.freeze + THEMES = TokenPermissionType.where(token_type: 'themes').first.freeze ## diff --git a/app/policies/api/v0/theme_policy.rb b/app/policies/api/v0/theme_policy.rb new file mode 100644 index 0000000..375a96a --- /dev/null +++ b/app/policies/api/v0/theme_policy.rb @@ -0,0 +1,23 @@ +module Api + module V0 + class ThemePolicy < ApplicationPolicy + attr_reader :user, :theme + + def initialize(user, theme) + raise Pundit::NotAuthorizedError, _("must be logged in") unless user + unless user.org.token_permission_types.include? TokenPermissionType::THEMES + raise Pundit::NotAuthorizedError, _("must have access to theme api") + end + @user = user + @theme = theme + end + + ## + # always allowed as index chooses which themes to display + def extract? + true + end + + end + end + end \ No newline at end of file diff --git a/app/policies/theme_policy.rb b/app/policies/theme_policy.rb index 3767696..b725b78 100644 --- a/app/policies/theme_policy.rb +++ b/app/policies/theme_policy.rb @@ -1,7 +1,7 @@ class ThemePolicy < ApplicationPolicy def initialize(user, *args) raise Pundit::NotAuthorizedError, _("must be logged in") unless user - @user = user + @user = user end def index? @user.can_super_admin? diff --git a/app/views/api/v0/themes/extract.jbuilder b/app/views/api/v0/themes/extract.jbuilder new file mode 100644 index 0000000..1c05f2d --- /dev/null +++ b/app/views/api/v0/themes/extract.jbuilder @@ -0,0 +1,15 @@ +json.theme @theme.title +json.answers @answers do |a| + json.id a.id + json.answer a.text + json.created_at a.created_at + json.question do + json.id a.question.id + json.title a.question.text + json.type a.question.question_format.title + end + json.plan do + json.id a.plan.id + json.title a.plan.title + end +end diff --git a/app/views/shared/_create_plan_modal.html.erb b/app/views/shared/_create_plan_modal.html.erb index 6523ae4..caf5a3f 100644 --- a/app/views/shared/_create_plan_modal.html.erb +++ b/app/views/shared/_create_plan_modal.html.erb @@ -5,7 +5,7 @@