Skip to content
7 changes: 7 additions & 0 deletions app/controllers/api/v2/plans_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +18,7 @@ def show
raise Pundit::NotAuthorizedError unless plans_policy.show?

@items = [@plan]

render '/api/v2/plans/index', status: :ok
end

Expand All @@ -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
16 changes: 16 additions & 0 deletions app/presenters/api/v2/plan_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ def identifier
Identifier.new(value: Rails.application.routes.url_helpers.api_v2_plan_url(@plan))
end

# 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

private

# Retrieve the answers that have the Budget theme
Expand Down
13 changes: 13 additions & 0 deletions app/views/api/v2/plans/_show.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,18 @@ unless @minimal
json.title template.title
end
end

if @complete
json.complete_plan do
q_and_a = presenter.send(:fetch_all_q_and_a)
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