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
45 changes: 17 additions & 28 deletions app/services/form_document_sync_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def initialize(form)

def synchronize_live_form
FormDocument.transaction do
synchronize_documents_for_tag(LIVE_TAG, live_at: form.updated_at)

# A new live version replaces any previous archived version
delete_form_documents_by_tag(ARCHIVED_TAG)

synchronize_documents_for_tag(LIVE_TAG, live_at: form.updated_at)
end
end

Expand All @@ -23,24 +23,18 @@ def synchronize_archived_form
# Ensure we only archive forms that are currently live
raise ActiveRecord::RecordNotFound, "Cannot archive a form that has no live version." unless live_documents.exists?

# Remove any pre-existing archived documents
delete_form_documents_by_tag(ARCHIVED_TAG)

# Change all live documents to archived
live_documents.update_all(tag: ARCHIVED_TAG)
end
end

def synchronize_archived_welsh_form
FormDocument.transaction do
# Ensure we only archive forms that are currently live
raise ActiveRecord::RecordNotFound, "Cannot archive a form that has no live version." unless FormDocument.where(form:, tag: LIVE_TAG, language: "cy").exists?
live_welsh_form_document = FormDocument.find_by(form:, tag: LIVE_TAG, language: "cy")

# Remove any pre-existing archived documents
FormDocument.where(form:, tag: ARCHIVED_TAG, language: "cy").delete_all
raise ActiveRecord::RecordNotFound, "Cannot archive a form that has no live version." unless live_welsh_form_document

# Change all live documents to archived
FormDocument.where(form:, tag: LIVE_TAG, language: "cy").update_all(tag: ARCHIVED_TAG)
live_welsh_form_document.update!(tag: ARCHIVED_TAG)

# Update the content of the live version to show that it doesn't support welsh anymore
FormDocument.where(form:, tag: [LIVE_TAG, DRAFT_TAG], language: "en").find_each do |live_document|
Expand All @@ -57,36 +51,31 @@ def synchronize_only_live_english_form
# If we've already made the Welsh version live, changes to the Welsh version must be made live at the same time by calling a different method
raise ActiveRecord::RecordNotFound, "Cannot make changes to only the live English form if there is already a live Welsh version." if FormDocument.where(form:, tag: LIVE_TAG, language: "cy").exists?

content = form_content("en", live_at: form.updated_at)
update_or_create_form_document(LIVE_TAG, content, "en")

# Update the content of the live English version to to not include Welsh in available_languages
FormDocument.where(form:, tag: [LIVE_TAG], language: "en").find_each do |live_document|
live_document.content["available_languages"] = %w[en]
live_document.save!
end

# A new live version replaces any previous archived version
delete_form_documents_by_tag(ARCHIVED_TAG)

content = form_content("en", live_at: form.updated_at)
content["available_languages"] = %w[en] # don't include Welsh in available languages
update_or_create_form_document(LIVE_TAG, content, "en")
end
end

def synchronize_only_live_welsh_form
FormDocument.transaction do
live_english_form_document = FormDocument.find_by(form:, tag: LIVE_TAG, language: "en")

# Ensure we only make Welsh version live if there is already an existing live English version
raise ActiveRecord::RecordNotFound, "Cannot make Welsh version live unless there is already a live English version." unless FormDocument.where(form:, tag: LIVE_TAG, language: "en").exists?
raise ActiveRecord::RecordNotFound, "Cannot make Welsh version live unless there is already a live English version." unless live_english_form_document

# A new live version replaces the archived version
delete_form_documents_by_tag(ARCHIVED_TAG)

content = form_content("cy", live_at: form.updated_at)
update_or_create_form_document(LIVE_TAG, content, "cy")

# Update the content of the live English version to show that it now supports Welsh
FormDocument.where(form:, tag: [LIVE_TAG], language: "en").find_each do |live_document|
live_document.content["available_languages"] = %w[en cy]
live_document.save!
end

# A new live version replaces any previous archived version
delete_form_documents_by_tag(ARCHIVED_TAG)
live_english_form_document.content["available_languages"] = %w[en cy]
live_english_form_document.save!
end
end

Expand Down
39 changes: 1 addition & 38 deletions spec/services/form_document_sync_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,6 @@
}.to(change { live_form_document.reload.tag }.from("live").to("archived"))
end
end

context "when there is an existing archived form document" do
before do
create :form_document, :live, form:, content: "live content"
create :form_document, :archived, form:, content: "old archived content"
end

it "replaces the archived form document" do
service.synchronize_archived_form
expect(FormDocument.find_by!(form:, tag: "archived").content).to eq("live content")
end

context "and deleting the existing archived FormDocuments fails" do
before do
allow(service).to receive(:delete_form_documents_by_tag).with(FormDocumentSyncService::ARCHIVED_TAG)
.and_raise(ActiveRecord::StatementInvalid)
end

it "does not change the archived FormDocument" do
expect {
service.synchronize_archived_form
}.to raise_error(ActiveRecord::StatementInvalid).and(not_change { form.reload.archived_form_document.content })
end
end
end
end

describe "#synchronize_archived_welsh_form" do
Expand Down Expand Up @@ -209,18 +184,6 @@
}.to(change { live_form_document_en.reload.content["available_languages"] }.from(%w[en cy]).to(%w[en]))
end
end

context "when there is an existing archived Welsh form document" do
before do
create :form_document, :live, form:, content: "live content cy", language: "cy"
create :form_document, :archived, form:, content: "old archived content cy", language: "cy"
end

it "replaces the archived form document" do
service.synchronize_archived_form
expect(FormDocument.find_by!(form:, tag: "archived", language: "cy").content).to eq("live content cy")
end
end
end

describe "#update_draft_form_document" do
Expand Down Expand Up @@ -480,7 +443,7 @@
end
end

context "when there is an existing archived form document" do
context "when there is an existing archived Welsh form document" do
before do
create :form_document, :archived, form:, language: "cy"
end
Expand Down