diff --git a/app/controllers/api/v2/plans_controller.rb b/app/controllers/api/v2/plans_controller.rb index 7714e9c892..33434b7a13 100644 --- a/app/controllers/api/v2/plans_controller.rb +++ b/app/controllers/api/v2/plans_controller.rb @@ -4,6 +4,7 @@ module Api module V2 class PlansController < BaseApiController # rubocop:todo Style/Documentation respond_to :json + before_action :set_complete_param, only: %i[show index] # GET /api/v2/plans/:id def show @@ -17,6 +18,7 @@ def show raise Pundit::NotAuthorizedError unless plans_policy.show? @items = [@plan] + render '/api/v2/plans/index', status: :ok end @@ -26,8 +28,13 @@ def index @plans = PlansPolicy::Scope.new(@resource_owner).resolve @items = paginate_response(results: @plans) + render '/api/v2/plans/index', status: :ok end + + def set_complete_param + @complete = params[:complete].to_s.downcase == 'true' + end end end end diff --git a/app/presenters/api/v2/plan_presenter.rb b/app/presenters/api/v2/plan_presenter.rb index 204cd68011..dd5973e169 100644 --- a/app/presenters/api/v2/plan_presenter.rb +++ b/app/presenters/api/v2/plan_presenter.rb @@ -4,9 +4,9 @@ module Api module V2 # Helper class for the API V2 project / DMP class PlanPresenter - attr_reader :data_contact, :contributors, :costs + attr_reader :data_contact, :contributors, :costs, :complete_plan_data - def initialize(plan:) + def initialize(plan:, complete: false) @contributors = [] return unless plan.present? @@ -22,6 +22,8 @@ def initialize(plan:) end @costs = plan_costs(plan: @plan) + + @complete_plan_data = fetch_all_q_and_a if complete end # Extract the ARK or DOI for the DMP OR use its URL if none exists @@ -37,6 +39,22 @@ def identifier private + # Fetch all questions and answers from a plan, regardless of theme + def fetch_all_q_and_a + return [] unless @plan.questions.present? + + @plan.questions.filter_map do |q| + a = @plan.answers.find { |ans| ans.question_id == q.id } + next unless a.present? + + { + title: "Question #{q.number || q.id}", + question: q.text.to_s, + answer: a.text.to_s + } + end + end + # Retrieve the answers that have the Budget theme def plan_costs(plan:) theme = Theme.where(title: 'Cost').first diff --git a/app/views/api/v2/plans/_show.json.jbuilder b/app/views/api/v2/plans/_show.json.jbuilder index 0de52775a3..3cd5dfe8c3 100644 --- a/app/views/api/v2/plans/_show.json.jbuilder +++ b/app/views/api/v2/plans/_show.json.jbuilder @@ -4,7 +4,7 @@ json.schema 'https://github.com/RDA-DMP-Common/RDA-DMP-Common-Standard/tree/master/examples/JSON/JSON-schema/1.0' -presenter = Api::V2::PlanPresenter.new(plan: plan) +presenter = Api::V2::PlanPresenter.new(plan: plan, complete: @complete) # Note the symbol of the dmproadmap json object # nested in extensions which is the container for the json template object, etc. @@ -68,5 +68,18 @@ unless @minimal json.title template.title end end + + if @complete + json.complete_plan do + q_and_a = presenter.complete_plan_data + next if q_and_a.blank? + + json.array! q_and_a do |item| + json.title item[:title] + json.question item[:question] + json.answer item[:answer] + end + end + end end end