Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit aa5aa58

Browse files
authored
Merge pull request #723 from alphagov/add-separate-endpoint-for-add-another-answer-report
Add separate endpoint for add another answer report
2 parents 6b86c59 + f2378c5 commit aa5aa58

5 files changed

Lines changed: 86 additions & 0 deletions

File tree

app/controllers/api/v1/reports_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ def features
55
render json: feature_stats.to_json, status: :ok
66
end
77

8+
def add_another_answer_forms
9+
data = Reports::FeatureUsageService.new.add_another_answer_forms
10+
11+
render json: data.to_json, status: :ok
12+
end
13+
814
def selection_questions_summary
915
statistics = Reports::SelectionQuestionService.new.live_form_statistics
1016

app/services/reports/feature_usage_service.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ def report
1212
}
1313
end
1414

15+
def add_another_answer_forms
16+
forms = all_forms_with_add_another_answer
17+
18+
# adding the count even though forms-admin doesn't use it as ActiveResource doesn't like parsing JSON with a single root key
19+
{ forms:, count: forms.length }
20+
end
21+
1522
private
1623

1724
# NOTE: all of these methods currently query the Form table rather than the MadeLiveForm table.

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
scope :reports do
5050
get "/features", to: "api/v1/reports#features"
51+
get "/add-another-answer-forms", to: "api/v1/reports#add_another_answer_forms"
5152
get "/selection-questions-summary", to: "api/v1/reports#selection_questions_summary"
5253
get "/selection-questions-with-autocomplete", to: "api/v1/reports#selection_questions_with_autocomplete"
5354
get "/selection-questions-with-radios", to: "api/v1/reports#selection_questions_with_radios"

spec/requests/api/v1/reports_controller_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,42 @@
6969
end
7070
end
7171

72+
describe "GET /add-another-answer-forms" do
73+
let!(:form_with_repeatable_question) { create(:form, state: :live, pages: [repeatable_page]) }
74+
let(:repeatable_page) { build(:page, answer_type: "text", is_repeatable: true) }
75+
76+
before do
77+
create :form, state: :live, pages: [
78+
(build :page, answer_type: "text"),
79+
(build :page, answer_type: "text"),
80+
]
81+
82+
get "/api/v1/reports/add-another-answer-forms"
83+
end
84+
85+
it "returns the forms with repeatable questions" do
86+
response_hash = JSON.parse(response.body, symbolize_names: true)
87+
88+
expect(response_hash).to eq({
89+
count: 1,
90+
forms: [
91+
{
92+
form_id: form_with_repeatable_question.id,
93+
name: form_with_repeatable_question.name,
94+
state: form_with_repeatable_question.state,
95+
repeatable_pages: [
96+
{ page_id: repeatable_page.id, question_text: repeatable_page.question_text },
97+
],
98+
},
99+
],
100+
})
101+
end
102+
103+
it "returns http success" do
104+
expect(response).to have_http_status(:success)
105+
end
106+
end
107+
72108
describe "GET /selection-questions-summary" do
73109
before do
74110
form_1_pages = [

spec/services/reports/feature_usage_service_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,40 @@
196196
end
197197
end
198198
end
199+
200+
describe "#add_another_answer_forms" do
201+
let!(:add_another_answer_draft_form) { create(:form, state: "draft", pages: draft_form_pages) }
202+
let(:draft_form_pages) { pages_with_add_another_answer }
203+
let!(:add_another_answer_live_form) { create(:form, state: "live", pages: live_form_pages) }
204+
let(:live_form_pages) do
205+
[
206+
(build :page, answer_type: "name", is_repeatable: true),
207+
(build :page, answer_type: "text", is_repeatable: true),
208+
]
209+
end
210+
211+
it "obtains all forms in the add another answer report" do
212+
report = features_report_service.add_another_answer_forms
213+
214+
expect(report[:forms]).to contain_exactly({
215+
form_id: add_another_answer_draft_form.id,
216+
name: add_another_answer_draft_form.name,
217+
state: add_another_answer_draft_form.state,
218+
repeatable_pages: contain_exactly({ page_id: draft_form_pages.first.id, question_text: draft_form_pages.first.question_text }),
219+
}, {
220+
form_id: add_another_answer_live_form.id,
221+
name: add_another_answer_live_form.name,
222+
state: add_another_answer_live_form.state,
223+
repeatable_pages: contain_exactly(
224+
{ page_id: live_form_pages.first.id, question_text: live_form_pages.first.question_text },
225+
{ page_id: live_form_pages.second.id, question_text: live_form_pages.second.question_text },
226+
),
227+
})
228+
end
229+
230+
it "returns the count" do
231+
report = features_report_service.add_another_answer_forms
232+
expect(report[:count]).to eq 2
233+
end
234+
end
199235
end

0 commit comments

Comments
 (0)