Skip to content

Commit cce14fb

Browse files
authored
Merge pull request #3595 from DMPRoadmap/momo/add-complete-flag
Add complete plan flag to V2 API
2 parents f4ea8f5 + 465bfca commit cce14fb

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## Unreleased
4+
- Add complete plan flag to V2 API [#3595](https://github.com/DMPRoadmap/roadmap/pull/3595)
5+
36
## v5.0.2
47
- Bump Ruby to v3.1.4 and use `.ruby-version` in CI
58
- [#3566](https://github.com/DMPRoadmap/roadmap/pull/3566)

app/controllers/api/v2/plans_controller.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ module Api
44
module V2
55
class PlansController < BaseApiController # rubocop:todo Style/Documentation
66
respond_to :json
7+
before_action :set_complete_param, only: %i[show index]
78

89
# GET /api/v2/plans/:id
910
def show
1011
raise Pundit::NotAuthorizedError unless @scopes.include?('read')
1112

12-
@plan = Plan.find_by(id: params[:id])
13+
@plan = Plan.includes(roles: :user).find_by(id: params[:id])
1314

1415
raise Pundit::NotAuthorizedError unless @plan.present?
1516

@@ -25,9 +26,17 @@ def index
2526
raise Pundit::NotAuthorizedError unless @scopes.include?('read')
2627

2728
@plans = PlansPolicy::Scope.new(@resource_owner).resolve
29+
@plans = @plans.includes(answers: { question: :section }) if @complete
2830
@items = paginate_response(results: @plans)
2931
render '/api/v2/plans/index', status: :ok
3032
end
33+
34+
private
35+
36+
# GET /api/v2/plans?complete=true and /api/v2/plans/:id?complete=true
37+
def set_complete_param
38+
@complete = params[:complete].to_s.downcase == 'true'
39+
end
3140
end
3241
end
3342
end

app/presenters/api/v2/plan_presenter.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ module Api
44
module V2
55
# Helper class for the API V2 project / DMP
66
class PlanPresenter
7-
attr_reader :data_contact, :contributors, :costs
7+
attr_reader :data_contact, :contributors, :costs, :complete_plan_data
88

9-
def initialize(plan:)
9+
def initialize(plan:, complete: false)
1010
@contributors = []
1111
return unless plan.present?
1212

@@ -22,6 +22,8 @@ def initialize(plan:)
2222
end
2323

2424
@costs = plan_costs(plan: @plan)
25+
26+
@complete_plan_data = fetch_all_q_and_a if complete
2527
end
2628

2729
# Extract the ARK or DOI for the DMP OR use its URL if none exists
@@ -55,6 +57,25 @@ def plan_costs(plan:)
5557
currency_code: 'usd', value: answer.text }
5658
end
5759
end
60+
61+
# Fetch all questions and answers from a plan, regardless of theme
62+
def fetch_all_q_and_a
63+
answers = @plan.answers
64+
return [] unless answers.present?
65+
66+
answers.filter_map do |answer|
67+
q = answer.question
68+
next unless q.present?
69+
70+
{
71+
id: q.id,
72+
title: "Question #{q.number || q.id}",
73+
section: q.section&.title,
74+
question: q.text.to_s,
75+
answer: answer.text.to_s
76+
}
77+
end
78+
end
5879
end
5980
end
6081
end

app/views/api/v2/plans/_show.json.jbuilder

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
json.schema 'https://github.com/RDA-DMP-Common/RDA-DMP-Common-Standard/tree/master/examples/JSON/JSON-schema/1.0'
66

7-
presenter = Api::V2::PlanPresenter.new(plan: plan)
7+
presenter = Api::V2::PlanPresenter.new(plan: plan, complete: @complete)
88

99
# Note the symbol of the dmproadmap json object
1010
# nested in extensions which is the container for the json template object, etc.
@@ -68,5 +68,20 @@ unless @minimal
6868
json.title template.title
6969
end
7070
end
71+
72+
if @complete
73+
json.complete_plan do
74+
q_and_a = presenter.complete_plan_data
75+
next if q_and_a.blank?
76+
77+
json.array! q_and_a do |item|
78+
json.question_id item[:id]
79+
json.title item[:title]
80+
json.section item[:section]
81+
json.question item[:question]
82+
json.answer item[:answer]
83+
end
84+
end
85+
end
7186
end
7287
end

0 commit comments

Comments
 (0)