Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/services/form_submission_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddWelshFormDocumentToSubmissions < ActiveRecord::Migration[8.1]
def change
add_column :submissions, :welsh_form_document, :jsonb
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 45 additions & 3 deletions spec/services/form_submission_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) { [] }
Expand All @@ -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

Expand Down