From b0a6834a4b97bc685a499fc26ffe5028a96ffa11 Mon Sep 17 00:00:00 2001 From: Stephen Daly Date: Wed, 20 May 2026 10:33:47 +0100 Subject: [PATCH 1/2] Add welsh_form_document column to submissions In order to send confirmation emails with a copy of the user's answers in Welsh, we need to store the Welsh version of the form document at the time of submission in order to get the Welsh translations for the questions that exist in the form at that moment in time. This is necessary as we send confirmation emails asynchronously using ActiveJob, so cannot retrieve the document from forms-admin at the time of sending as it may have changed since the submission. --- .../20260520093250_add_welsh_form_document_to_submissions.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20260520093250_add_welsh_form_document_to_submissions.rb diff --git a/db/migrate/20260520093250_add_welsh_form_document_to_submissions.rb b/db/migrate/20260520093250_add_welsh_form_document_to_submissions.rb new file mode 100644 index 000000000..aaffdaf79 --- /dev/null +++ b/db/migrate/20260520093250_add_welsh_form_document_to_submissions.rb @@ -0,0 +1,5 @@ +class AddWelshFormDocumentToSubmissions < ActiveRecord::Migration[8.1] + def change + add_column :submissions, :welsh_form_document, :jsonb + end +end diff --git a/db/schema.rb b/db/schema.rb index ce96ed055..fa90a1f56 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_05_06_142828) do +ActiveRecord::Schema[8.1].define(version: 2026_05_20_093250) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -46,6 +46,7 @@ t.string "reference" t.string "submission_locale", default: "en", null: false, comment: "The language the form was submitted in ISO 2 letter format. Normally either 'en' or 'cy'" t.datetime "updated_at", null: false + t.jsonb "welsh_form_document" t.index ["created_at", "form_id", "mode"], name: "index_submissions_on_created_at_and_form_id_and_mode" end From 338437266f392ac1f6c3ae0967134e952133a2e4 Mon Sep 17 00:00:00 2001 From: Stephen Daly Date: Wed, 20 May 2026 11:25:18 +0100 Subject: [PATCH 2/2] Store Welsh form document on submission If Welsh has been used when completing the form, store the Welsh form document on the submission. If we've already loaded the Welsh form document, we can use that - otherwise retrieve it from forms-admin. --- app/services/form_submission_service.rb | 9 ++++ spec/services/form_submission_service_spec.rb | 48 +++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app/services/form_submission_service.rb b/app/services/form_submission_service.rb index 24fda7bb5..94f7252fd 100644 --- a/app/services/form_submission_service.rb +++ b/app/services/form_submission_service.rb @@ -53,9 +53,17 @@ def fetch_english_language_form raise ActiveResource::ResourceNotFound.new(404, "Not Found") if english_form_document.nil? + @welsh_form = form if form.welsh? @form = Form.new(english_form_document) end + def welsh_form_document + return unless submission_locale == :cy + return @welsh_form.document_json if @welsh_form.present? + + Api::V2::FormDocumentRepository.find_with_mode(form_id: form.id, mode:, language: :cy) + end + def validate_submission raise StandardError, "Form id(#{form.id}) has no completed steps i.e questions/answers to submit" if current_context.completed_steps.blank? end @@ -89,6 +97,7 @@ def create_submission_record answers: current_context.answers, mode: mode, form_document: form.document_json, + welsh_form_document: welsh_form_document, submission_locale:, created_at: timestamp, ) diff --git a/spec/services/form_submission_service_spec.rb b/spec/services/form_submission_service_spec.rb index c61c1878b..45f5de9ea 100644 --- a/spec/services/form_submission_service_spec.rb +++ b/spec/services/form_submission_service_spec.rb @@ -190,8 +190,12 @@ }.to change(Submission, :count).by(1) .and change(Delivery, :count).by(1) - expect(Submission.last).to have_attributes(reference:, form_id: form.id, answers: answers.deep_stringify_keys, - mode: "form", form_document: document_json, + expect(Submission.last).to have_attributes(reference:, + form_id: form.id, + answers: answers.deep_stringify_keys, + mode: "form", + form_document: document_json, + welsh_form_document: nil, submission_locale: "en") end @@ -283,6 +287,32 @@ end end + context "when Welsh has been used to complete the form" do + let(:locales_used) { %i[en cy] } + + before do + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/api/v2/forms/1/live?language=cy", {}, welsh_form_document.to_json, 200 + end + end + + it "fetches the Welsh form" do + service.submit + expect(ActiveResource::HttpMock.requests).to include ActiveResource::Request.new(:get, "/api/v2/forms/1/live?language=cy") + end + + it "saves the submission data including the Welsh version of the form" do + expect { + service.submit + }.to change(Submission, :count).by(1) + + expect(Submission.last.form_document["language"]).to eq("en") + expect(Submission.last.form_document["name"]).to eq("Form 1") + expect(Submission.last.welsh_form_document["language"]).to eq("cy") + expect(Submission.last.welsh_form_document["name"]).to eq("Welsh Form 1") + end + end + context "when form is not in english" do let(:submission_type) { "email" } let(:submission_format) { [] } @@ -305,10 +335,22 @@ service.submit }.to change(Submission, :count).by(1) - # expect(Submission.last).to have_attributes(form_id: form.id, answers: answers.deep_stringify_keys, form_document: document_json) expect(Submission.last.form_document["language"]).to eq("en") expect(Submission.last.form_document["name"]).to eq("Form 1") end + + context "when Welsh has been used to complete the form" do + let(:locales_used) { %i[en cy] } + + it "saves the original Welsh version of the form on the submission" do + expect { + service.submit + }.to change(Submission, :count).by(1) + + expect(Submission.last.welsh_form_document["language"]).to eq("cy") + expect(Submission.last.welsh_form_document["name"]).to eq("Welsh Form 1") + end + end end end