Skip to content

Commit 214a820

Browse files
authored
Merge pull request #2111 from govuk-forms/store-welsh-from-document-on-submission
Store Welsh from document on submission
2 parents 229afd5 + 3384372 commit 214a820

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

app/services/form_submission_service.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ def fetch_english_language_form
5353

5454
raise ActiveResource::ResourceNotFound.new(404, "Not Found") if english_form_document.nil?
5555

56+
@welsh_form = form if form.welsh?
5657
@form = Form.new(english_form_document)
5758
end
5859

60+
def welsh_form_document
61+
return unless submission_locale == :cy
62+
return @welsh_form.document_json if @welsh_form.present?
63+
64+
Api::V2::FormDocumentRepository.find_with_mode(form_id: form.id, mode:, language: :cy)
65+
end
66+
5967
def validate_submission
6068
raise StandardError, "Form id(#{form.id}) has no completed steps i.e questions/answers to submit" if current_context.completed_steps.blank?
6169
end
@@ -89,6 +97,7 @@ def create_submission_record
8997
answers: current_context.answers,
9098
mode: mode,
9199
form_document: form.document_json,
100+
welsh_form_document: welsh_form_document,
92101
submission_locale:,
93102
created_at: timestamp,
94103
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddWelshFormDocumentToSubmissions < ActiveRecord::Migration[8.1]
2+
def change
3+
add_column :submissions, :welsh_form_document, :jsonb
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/services/form_submission_service_spec.rb

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,12 @@
190190
}.to change(Submission, :count).by(1)
191191
.and change(Delivery, :count).by(1)
192192

193-
expect(Submission.last).to have_attributes(reference:, form_id: form.id, answers: answers.deep_stringify_keys,
194-
mode: "form", form_document: document_json,
193+
expect(Submission.last).to have_attributes(reference:,
194+
form_id: form.id,
195+
answers: answers.deep_stringify_keys,
196+
mode: "form",
197+
form_document: document_json,
198+
welsh_form_document: nil,
195199
submission_locale: "en")
196200
end
197201

@@ -283,6 +287,32 @@
283287
end
284288
end
285289

290+
context "when Welsh has been used to complete the form" do
291+
let(:locales_used) { %i[en cy] }
292+
293+
before do
294+
ActiveResource::HttpMock.respond_to do |mock|
295+
mock.get "/api/v2/forms/1/live?language=cy", {}, welsh_form_document.to_json, 200
296+
end
297+
end
298+
299+
it "fetches the Welsh form" do
300+
service.submit
301+
expect(ActiveResource::HttpMock.requests).to include ActiveResource::Request.new(:get, "/api/v2/forms/1/live?language=cy")
302+
end
303+
304+
it "saves the submission data including the Welsh version of the form" do
305+
expect {
306+
service.submit
307+
}.to change(Submission, :count).by(1)
308+
309+
expect(Submission.last.form_document["language"]).to eq("en")
310+
expect(Submission.last.form_document["name"]).to eq("Form 1")
311+
expect(Submission.last.welsh_form_document["language"]).to eq("cy")
312+
expect(Submission.last.welsh_form_document["name"]).to eq("Welsh Form 1")
313+
end
314+
end
315+
286316
context "when form is not in english" do
287317
let(:submission_type) { "email" }
288318
let(:submission_format) { [] }
@@ -305,10 +335,22 @@
305335
service.submit
306336
}.to change(Submission, :count).by(1)
307337

308-
# expect(Submission.last).to have_attributes(form_id: form.id, answers: answers.deep_stringify_keys, form_document: document_json)
309338
expect(Submission.last.form_document["language"]).to eq("en")
310339
expect(Submission.last.form_document["name"]).to eq("Form 1")
311340
end
341+
342+
context "when Welsh has been used to complete the form" do
343+
let(:locales_used) { %i[en cy] }
344+
345+
it "saves the original Welsh version of the form on the submission" do
346+
expect {
347+
service.submit
348+
}.to change(Submission, :count).by(1)
349+
350+
expect(Submission.last.welsh_form_document["language"]).to eq("cy")
351+
expect(Submission.last.welsh_form_document["name"]).to eq("Welsh Form 1")
352+
end
353+
end
312354
end
313355
end
314356

0 commit comments

Comments
 (0)