Skip to content

Commit 4471f0e

Browse files
authored
Merge branch 'main' into add-user-research-contact-preference
2 parents 5aa1ce2 + 6804ccd commit 4471f0e

12 files changed

Lines changed: 129 additions & 287 deletions

app/helpers/report_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def report_questions_table_rows(questions)
7373
def report_forms_table_row(form)
7474
[
7575
form_link(form),
76-
form.dig("group", "organisation", "name") || "",
76+
form["organisation_name"],
7777
]
7878
end
7979

app/services/reports/feature_report_service.rb

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,48 +81,29 @@ def forms_with_branch_routes
8181
def forms_with_payments
8282
form_documents
8383
.select { |form| Reports::FormDocumentsService.has_payments?(form) }
84-
.map { |form| form_details(form) }
8584
end
8685

8786
def forms_with_exit_pages
8887
form_documents
8988
.select { |form| Reports::FormDocumentsService.has_exit_pages?(form) }
90-
.map { |form| form_details(form) }
9189
end
9290

9391
def forms_with_csv_submission_enabled
9492
form_documents
9593
.select { |form| Reports::FormDocumentsService.has_csv_submission_enabled?(form) }
96-
.map { |form| form_details(form) }
9794
end
9895

9996
private
10097

10198
def questions_details(form, step)
102-
form = form_details(form)
10399
step.dup.merge("form" => form)
104100
end
105101

106102
def form_with_routes_details(form)
107-
form = form_details(form)
108103
form["metadata"] = {
109104
"number_of_routes" => form["content"]["steps"].count { |step| step["routing_conditions"].present? },
110105
"number_of_branch_routes" => Reports::FormDocumentsService.count_secondary_skip_routes(form),
111106
}
112107
form
113108
end
114-
115-
def form_details(form)
116-
form = form.dup
117-
form["group"] = {
118-
"organisation" => {
119-
"name" => organisation_name(form["form_id"]),
120-
},
121-
}
122-
form
123-
end
124-
125-
def organisation_name(form_id)
126-
GroupForm.find_by_form_id(form_id)&.group&.organisation&.name
127-
end
128109
end

app/services/reports/form_documents_service.rb

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,33 @@ def has_exit_pages?(form_document)
4848
private
4949

5050
def draft_form_documents
51-
draft_forms = Form.joins(group_form: { group: :organisation })
52-
.where(state: %w[draft live_with_draft archived_with_draft])
53-
.where.not(organisation: { "internal": true })
51+
draft_forms = Form
52+
.joins(group_form: { group: :organisation })
53+
.where(state: %w[draft live_with_draft archived_with_draft])
54+
.where.not(organisation: { "internal": true })
55+
.select("forms.*",
56+
"organisation.name AS organisation_name",
57+
"organisation.id AS organisation_id",
58+
"groups.external_id AS group_external_id",
59+
"groups.name AS group_name")
5460

55-
draft_forms.map { |form| FormDocument.new(form:, tag: "draft", content: form.as_form_document).as_json }
61+
draft_forms.map do |form|
62+
form_details = FormDocument.new(form:, tag: "draft", content: form.as_form_document).as_json
63+
form_details.merge({
64+
"organisation_name" => form.organisation_name,
65+
"organisation_id" => form.organisation_id,
66+
"group_external_id" => form.group_external_id,
67+
"group_name" => form.group_name,
68+
})
69+
end
5670
end
5771

5872
def live_form_documents
59-
FormDocument.joins(form: { group_form: { group: :organisation } }).where(tag: "live").where.not(organisation: { "internal": true }).map(&:as_json)
73+
FormDocument.joins(form: { group_form: { group: :organisation } })
74+
.where(tag: "live")
75+
.where.not(organisation: { "internal": true })
76+
.select("form_documents.*", "organisation.name AS organisation_name", "organisation.id AS organisation_id", "groups.external_id AS group_external_id", "groups.name AS group_name")
77+
.map(&:as_json)
6078
end
6179

6280
def secondary_skip_conditions(form_document)

app/services/reports/forms_csv_report_service.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,15 @@ def csv
4747

4848
def form_row(form)
4949
form_id = form["form_id"]
50-
group = GroupForm.find_by_form_id(form_id)&.group
5150
[
5251
form_id,
5352
form["tag"],
5453
form["content"]["name"],
5554
form["content"]["form_slug"],
56-
group&.organisation&.name,
57-
group&.organisation&.id,
58-
group&.name,
59-
group&.external_id,
55+
form["organisation_name"],
56+
form["organisation_id"],
57+
form["group_name"],
58+
form["group_external_id"],
6059
form["content"]["created_at"],
6160
form["content"]["updated_at"],
6261
form["content"]["steps"].length,

app/services/reports/questions_csv_report_service.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ def csv
5050
def question_row(step)
5151
form = step["form"]
5252
form_id = form["form_id"]
53-
group = GroupForm.find_by_form_id(form_id)&.group
53+
GroupForm.find_by_form_id(form_id)&.group
5454

5555
[
5656
form_id,
5757
form["tag"],
5858
form["content"]["name"],
59-
group&.organisation&.name,
60-
group&.organisation&.id,
61-
group&.name,
62-
group&.external_id,
59+
form["organisation_name"],
60+
form["organisation_id"],
61+
form["group_name"],
62+
form["group_external_id"],
6363
step["position"],
6464
step["data"]["question_text"],
6565
step["data"]["answer_type"],

spec/helpers/report_helpers_spec.rb

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
RSpec.describe ReportHelper, type: :helper do
44
let(:forms) do
55
[
6-
{ "form_id" => 1, "tag" => "live", "content" => { "name" => "All question types form" }, "group" => { "organisation" => { "name" => "Government Digital Service" } } },
7-
{ "form_id" => 3, "tag" => "live", "content" => { "name" => "Branch route form" }, "group" => { "organisation" => { "name" => "Ministry of Tests" } } },
8-
{ "form_id" => 4, "tag" => "live", "content" => { "name" => "Skip route form" }, "group" => { "organisation" => { "name" => "Department for Testing" } } },
6+
{ "form_id" => 1, "tag" => "live", "content" => { "name" => "All question types form" }, "organisation_name" => "Government Digital Service" },
7+
{ "form_id" => 3, "tag" => "live", "content" => { "name" => "Branch route form" }, "organisation_name" => "Ministry of Tests" },
8+
{ "form_id" => 4, "tag" => "live", "content" => { "name" => "Skip route form" }, "organisation_name" => "Department for Testing" },
99
]
1010
end
1111

1212
let(:forms_with_routes) do
1313
[
14-
{ "form_id" => 3, "tag" => "live", "content" => { "name" => "Branch route form" }, "group" => { "organisation" => { "name" => "Ministry of Tests" } }, "metadata" => { "number_of_routes" => 2, "number_of_branch_routes" => 1 } },
15-
{ "form_id" => 4, "tag" => "live", "content" => { "name" => "Skip route form" }, "group" => { "organisation" => { "name" => "Department for Testing" } }, "metadata" => { "number_of_routes" => 1, "number_of_branch_routes" => 0 } },
14+
{ "form_id" => 3, "tag" => "live", "content" => { "name" => "Branch route form" }, "organisation_name" => "Ministry of Tests", "metadata" => { "number_of_routes" => 2, "number_of_branch_routes" => 1 } },
15+
{ "form_id" => 4, "tag" => "live", "content" => { "name" => "Skip route form" }, "organisation_name" => "Department for Testing", "metadata" => { "number_of_routes" => 1, "number_of_branch_routes" => 0 } },
1616
]
1717
end
1818

1919
let(:questions) do
2020
[
21-
{ "type" => "question_page", "data" => { "question_text" => "Email address" }, "form" => { "form_id" => 1, "tag" => "live", "content" => { "name" => "All question types form" }, "group" => { "organisation" => { "name" => "Government Digital Service" } } } },
22-
{ "type" => "question_page", "data" => { "question_text" => "What’s your email address?" }, "form" => { "form_id" => 3, "tag" => "live", "content" => { "name" => "Branch route form" }, "group" => { "organisation" => { "name" => "Ministry of Tests" } } } },
21+
{ "type" => "question_page", "data" => { "question_text" => "Email address" }, "form" => { "form_id" => 1, "tag" => "live", "content" => { "name" => "All question types form" }, "organisation_name" => "Government Digital Service" } },
22+
{ "type" => "question_page", "data" => { "question_text" => "What’s your email address?" }, "form" => { "form_id" => 3, "tag" => "live", "content" => { "name" => "Branch route form" }, "organisation_name" => "Ministry of Tests" } },
2323
]
2424
end
2525

@@ -154,20 +154,6 @@
154154
"Department for Testing",
155155
]
156156
end
157-
158-
context "when form is not in a group" do
159-
let(:forms) do
160-
[
161-
{ "form_id" => 1, "tag" => "live", "content" => { "name" => "All question types form" }, "group" => nil },
162-
]
163-
end
164-
165-
it "returns the empty string for the organisation name" do
166-
expect(helper.report_forms_table_rows(forms).map(&:second)).to eq [
167-
"",
168-
]
169-
end
170-
end
171157
end
172158

173159
describe "#report_forms_with_routes_table_head" do
@@ -228,20 +214,6 @@
228214
0
229215
]
230216
end
231-
232-
context "when form is not in a group" do
233-
let(:forms) do
234-
[
235-
{ "form_id" => 1, "tag" => "live", "content" => { "name" => "All question types form" }, "group" => nil },
236-
]
237-
end
238-
239-
it "returns the empty string for the organisation name" do
240-
expect(helper.report_forms_table_rows(forms).map(&:second)).to eq [
241-
"",
242-
]
243-
end
244-
end
245217
end
246218

247219
describe "#report_questions_table_head" do
@@ -282,19 +254,5 @@
282254
"What’s your email address?",
283255
]
284256
end
285-
286-
context "when form is not in a group" do
287-
let(:questions) do
288-
[
289-
{ "type" => "question_page", "data" => { "question_text" => "Email address" }, "form" => { "form_id" => 1, "tag" => "live", "content" => { "name" => "All question types form" }, "group" => nil } },
290-
]
291-
end
292-
293-
it "returns the empty string for the organisation name" do
294-
expect(helper.report_questions_table_rows(questions).map(&:second)).to eq [
295-
"",
296-
]
297-
end
298-
end
299257
end
300258
end

0 commit comments

Comments
 (0)