Skip to content

Commit 72f3e09

Browse files
committed
Add submission attachments controller and view
Adds a submission attachments controller, where a form submission_format can be set to blank, csv or json. If the json_submission_enabled feature flag is not turned on, it will return a 404 when accessing the controller's actions. A success message is played if the form's submission_format has been changed - depending on what it has been changed to.
1 parent c64e127 commit 72f3e09

4 files changed

Lines changed: 240 additions & 3 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module Forms
2+
class SubmissionAttachmentsController < WebController
3+
before_action :json_submission_enabled?
4+
after_action :verify_authorized
5+
6+
def new
7+
authorize current_form, :can_view_form?
8+
@submission_attachments_input = SubmissionAttachmentsInput.new(form: current_form).assign_form_values
9+
end
10+
11+
def create
12+
authorize current_form, :can_view_form?
13+
@submission_attachments_input = SubmissionAttachmentsInput.new(submission_attachments_input_params)
14+
15+
if @submission_attachments_input.submit
16+
redirect_to form_path(current_form.id), success: success_message(current_form)
17+
else
18+
render :new, status: :unprocessable_content
19+
end
20+
end
21+
22+
private
23+
24+
def submission_attachments_input_params
25+
params.require(:forms_submission_attachments_input).permit(submission_format: []).merge(form: current_form)
26+
end
27+
28+
def json_submission_enabled?
29+
redirect_to error_404_path unless Settings.features.json_submission_enabled
30+
end
31+
32+
def success_message(form)
33+
return nil unless form.submission_format_previously_changed?
34+
35+
case form.submission_format
36+
when []
37+
t("banner.success.form.receive_no_attachments")
38+
when %w[csv]
39+
t("banner.success.form.receive_csv_enabled")
40+
when %w[json]
41+
t("banner.success.form.receive_json_enabled")
42+
when %w[csv json]
43+
t("banner.success.form.receive_csv_and_json_enabled")
44+
end
45+
end
46+
end
47+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<% set_page_title(title_with_error_prefix(t(".title"), @submission_attachments_input.errors.any?)) %>
2+
<% content_for :back_link, govuk_back_link_to(form_path, t("back_link.form_create")) %>
3+
4+
<div class="govuk-grid-row">
5+
<div class="govuk-grid-column-two-thirds">
6+
<%= form_with(model: @submission_attachments_input, url: submission_attachments_create_path) do |f| %>
7+
<% if @submission_attachments_input&.errors.any? %>
8+
<%= f.govuk_error_summary %>
9+
<% end %>
10+
11+
<span class="govuk-caption-l"><%= @submission_attachments_input.form.name %></span>
12+
<h1 class="govuk-heading-l">
13+
<%= t(".title") %>
14+
</h1>
15+
16+
<%= t(".body_html") %>
17+
18+
<%= f.govuk_check_boxes_fieldset :submission_format, legend: { tag: 'h2', size: 'm' } do %>
19+
<%= f.govuk_check_box :submission_format, "csv" %>
20+
<%= f.govuk_check_box :submission_format, "json" %>
21+
<% end %>
22+
<%= f.govuk_submit t(".submit") %>
23+
<% end %>
24+
</div>
25+
</div>

config/locales/en.yml

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,14 @@ en:
7575
forms/delete_confirmation_input:
7676
blank: Select ‘Yes’ to delete the draft
7777
forms/submission_attachments_input:
78-
invalid_submission_format: Choose if you want to get completed forms as CSV or JSON files
7978
attributes:
8079
submission_format:
8180
blank: Choose if you want to get completed forms as CSV or JSON files
81+
invalid_submission_format: Choose if you want to get completed forms as CSV or JSON files
8282
forms/submission_type_input:
8383
attributes:
8484
submission_type:
8585
blank: Choose if you want to get completed forms as CSV files
86-
forms/submission_attachments_input:
87-
invalid_submission_format: Choose if you want to get completed forms as CSV or JSON files
8886
group_member_input:
8987
attributes:
9088
member_email_address:
@@ -226,8 +224,11 @@ en:
226224
payment_link_removed: Your payment link has been removed
227225
payment_link_saved: Your payment link has been saved
228226
privacy_details_saved: Your privacy information link has been saved
227+
receive_csv_and_json_enabled: Completed form emails will include a CSV and a JSON file
229228
receive_csv_disabled: Completed form emails will not include a CSV file
230229
receive_csv_enabled: Completed form emails will include a CSV file
230+
receive_json_enabled: Completed form emails will include a JSON file
231+
receive_no_attachments: Completed form emails will not include a CSV or JSON file
231232
share_preview_completed: The preview task has been completed
232233
support_details_saved: Your contact details for support have been saved
233234
welsh_translation_saved: The Welsh version of your form has been saved
@@ -390,6 +391,21 @@ en:
390391
<p>If you select this option, a CSV file will be attached to each completed form email.</p>
391392
submit: Save and continue
392393
title: Get completed forms as CSV files
394+
submission_attachments:
395+
new:
396+
body_html: |
397+
<p>When your form’s completed, the answers provided will be sent in the body of an email to the email address you set.</p>
398+
399+
<p>You can also get the answers in:</p>
400+
401+
<ul class="govuk-list govuk-list--bullet">
402+
<li>A CSV file - this lists all the answers in plain text, separated by commas. It can be easily imported into a spreadsheet or database application.</li>
403+
<li>a JSON file - this is a plain text format for exchanging and storing data. It can make it easier to use automated processing for your form submissions.</li>
404+
</ul>
405+
406+
<p>If you select one or both of these options, the files will be attached to each completed form email.</p>
407+
submit: Save and continue
408+
title: Get a CSV or JSON file of each completed form
393409
task_list_create:
394410
create_form_section:
395411
declaration: Add a declaration for people to agree to
@@ -826,6 +842,10 @@ en:
826842
confirm: Are you sure you want to make your draft live?
827843
forms_make_live_input:
828844
confirm: Are you sure you want to make your form live?
845+
forms_submission_attachments_input:
846+
submission_format_options:
847+
csv: Get a CSV file of each completed form
848+
json: Get a JSON file of each completed form
829849
forms_submission_type_input:
830850
submission_type_options:
831851
email_with_csv: Get CSV files
@@ -972,6 +992,8 @@ en:
972992
legend:
973993
account_terms_of_use_input:
974994
agreed: Do you agree to these terms?
995+
forms_submission_attachments_input:
996+
submission_format: Do you want to get a CSV or JSON file of each completed form?
975997
forms_submission_type_input:
976998
submission_type: Do you want to get completed forms as CSV files?
977999
mou_signature:
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Forms::SubmissionAttachmentsController, type: :request do
4+
let(:form) { create(:form, :live, submission_format: original_submission_format) }
5+
let(:original_submission_format) { [] }
6+
7+
before do
8+
login_as_super_admin_user
9+
end
10+
11+
describe "#new" do
12+
context "when the json_submission_enabled feature flag is off" do
13+
before do
14+
allow(Settings.features).to receive(:json_submission_enabled).and_return(false)
15+
16+
get submission_attachments_path(form_id: form.id)
17+
end
18+
19+
it "returns a 404 page" do
20+
expect(response).to redirect_to :error_404
21+
end
22+
end
23+
24+
context "when the json_submission_enabled feature flag is on" do
25+
before do
26+
allow(Settings.features).to receive(:json_submission_enabled).and_return(true)
27+
28+
get submission_attachments_path(form_id: form.id)
29+
end
30+
31+
it "renders the submission attachments view" do
32+
expect(response).to have_rendered :new
33+
end
34+
35+
it "uses the form submission attachments input" do
36+
expect(assigns).to include submission_attachments_input: an_instance_of(Forms::SubmissionAttachmentsInput)
37+
end
38+
end
39+
end
40+
41+
describe "#create" do
42+
let(:params) { { forms_submission_attachments_input: { submission_format: } } }
43+
44+
context "when the json_submission_enabled feature flag is off" do
45+
before do
46+
allow(Settings.features).to receive(:json_submission_enabled).and_return(false)
47+
end
48+
49+
it "returns a 404 page" do
50+
post submission_attachments_path(form_id: form.id)
51+
expect(response).to redirect_to :error_404
52+
end
53+
end
54+
55+
context "when the json_submission_enabled feature flag is on" do
56+
before do
57+
allow(Settings.features).to receive(:json_submission_enabled).and_return(true)
58+
end
59+
60+
context "when params are valid" do
61+
let(:submission_format) { %w[csv json] }
62+
63+
it "updates the form submission format" do
64+
expect {
65+
post(submission_attachments_path(form_id: form.id), params:)
66+
}.to change { form.reload.submission_format }.to(submission_format)
67+
end
68+
69+
it "redirects you to the form overview page" do
70+
post(submission_attachments_path(form_id: form.id), params:)
71+
expect(response).to redirect_to(form_path(form.id))
72+
end
73+
74+
context "when submission format has changed to 'csv'" do
75+
let(:submission_format) { %w[csv] }
76+
77+
it "displays a success flash message" do
78+
post(submission_attachments_path(form_id: form.id), params:)
79+
expect(flash[:success]).to eq(I18n.t("banner.success.form.receive_csv_enabled"))
80+
end
81+
end
82+
83+
context "when submission format has changed to 'json'" do
84+
let(:submission_format) { %w[json] }
85+
86+
it "displays a success flash message" do
87+
post(submission_attachments_path(form_id: form.id), params:)
88+
expect(flash[:success]).to eq(I18n.t("banner.success.form.receive_json_enabled"))
89+
end
90+
end
91+
92+
context "when submission format has changed to 'csv json'" do
93+
let(:submission_format) { %w[csv json] }
94+
95+
it "displays a success flash message" do
96+
post(submission_attachments_path(form_id: form.id), params:)
97+
expect(flash[:success]).to eq(I18n.t("banner.success.form.receive_csv_and_json_enabled"))
98+
end
99+
end
100+
101+
context "when submission format has changed to blank" do
102+
let(:original_submission_format) { %w[csv json] }
103+
let(:submission_format) { %w[] }
104+
105+
it "displays a success flash message" do
106+
post(submission_attachments_path(form_id: form.id), params:)
107+
expect(flash[:success]).to eq(I18n.t("banner.success.form.receive_no_attachments"))
108+
end
109+
end
110+
111+
context "when submission format has not changed" do
112+
let(:submission_format) { %w[] }
113+
114+
it "does not display a flash message" do
115+
post(submission_attachments_path(form_id: form.id), params:)
116+
expect(flash[:success]).to be_nil
117+
end
118+
end
119+
end
120+
121+
context "when params are invalid" do
122+
let(:submission_format) { %w[apple] }
123+
124+
it "returns 422" do
125+
post(submission_attachments_path(form_id: form.id), params:)
126+
expect(response).to have_http_status(:unprocessable_content)
127+
end
128+
129+
it "does not update the form" do
130+
expect {
131+
post(submission_attachments_path(form_id: form.id), params:)
132+
}.not_to(change { form.reload.submission_format })
133+
end
134+
135+
it "re-renders the page with an error" do
136+
post(submission_attachments_path(form_id: form.id), params:)
137+
expect(response).to render_template("new")
138+
expect(response.body).to include(I18n.t("activemodel.errors.models.forms/submission_attachments_input.invalid_submission_format"))
139+
end
140+
end
141+
end
142+
end
143+
end

0 commit comments

Comments
 (0)