Skip to content

Commit

Permalink
Merge pull request #2769 from alphagov/republish-translation-put
Browse files Browse the repository at this point in the history
Republish drafts of other translations on PUT of new translation
  • Loading branch information
brucebolt authored Jun 20, 2024
2 parents 71fae1e + d3c4199 commit 81c17f5
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
25 changes: 25 additions & 0 deletions app/commands/v2/put_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class PutContent < BaseCommand
def call
PutContentValidator.new(payload, self).validate

update_draft_items_of_different_locale_but_matching_content_id

remove_previous_path_reservations
reserve_current_path
clear_draft_items_of_same_locale_and_base_path
Expand Down Expand Up @@ -200,6 +202,29 @@ def clear_draft_item_of_different_locale_but_matching_base_path
)
end

def update_draft_items_of_different_locale_but_matching_content_id
return unless payload[:base_path]
return if Document.where(content_id: payload[:content_id], locale: payload[:locale]).any?

draft_editions_for_different_locale = Document
.where(content_id: payload[:content_id])
.where
.not(locale: payload[:locale])
.map(&:draft)
.compact

after_transaction_commit do
draft_editions_for_different_locale.each do |edition|
DownstreamDraftWorker.perform_async_in_queue(
bulk_publishing? ? DownstreamDraftWorker::LOW_QUEUE : DownstreamDraftWorker::HIGH_QUEUE,
"content_id" => edition.content_id,
"locale" => edition.locale,
"source_command" => "put_content",
)
end
end
end

def bulk_publishing?
payload.fetch(:bulk_publishing, false)
end
Expand Down
88 changes: 87 additions & 1 deletion spec/commands/v2/put_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@
end
end

context "when a draft does exist with a different locale" do
context "when a draft does exist with a different locale but same base path" do
let(:en_document) { create(:document, content_id:) }
let!(:en_edition) do
create(:draft_edition, base_path:, document: en_document)
Expand Down Expand Up @@ -387,5 +387,91 @@
described_class.call(payload)
end
end

context "when creating a new translation and a draft exists with a different locale and different base path" do
let(:cy_document) { create(:document, content_id:, locale: "cy") }
let!(:cy_edition) do
create(:draft_edition, base_path: "#{base_path}.cy", document: cy_document)
end

it "sends all draft translations downstream" do
expect(DownstreamDraftWorker).to receive(:perform_async_in_queue)
.with(anything, a_hash_including("content_id" => content_id, "locale" => "en"))

expect(DownstreamDraftWorker).to receive(:perform_async_in_queue)
.with(anything, a_hash_including("content_id" => content_id, "locale" => "cy"))

described_class.call(payload)
end

it "does not send the other translation downstream if there is no base path" do
expect(DownstreamDraftWorker).to receive(:perform_async_in_queue)
.with(anything, a_hash_including("content_id" => content_id, "locale" => "cy"))
.never

payload = {
content_id:,
update_type: "major",
title: "Some Title",
publishing_app:,
rendering_app: "frontend",
document_type: "contact",
schema_name: "contact",
locale:,
redirects: [],
phase: "beta",
change_note:,
details: {},
}

described_class.call(payload)
end
end

context "when updating a translation and a draft exists with a different locale and different base path" do
let(:en_document) { create(:document, content_id:, locale: "en") }
let!(:en_edition) do
create(:draft_edition, base_path:, document: en_document)
end

let(:cy_document) { create(:document, content_id:, locale: "cy") }
let!(:cy_edition) do
create(:draft_edition, base_path: "#{base_path}.cy", document: cy_document)
end

it "sends only sends the updated translation downstream" do
expect(DownstreamDraftWorker).to receive(:perform_async_in_queue)
.with(anything, a_hash_including("content_id" => content_id, "locale" => "en"))

expect(DownstreamDraftWorker).to receive(:perform_async_in_queue)
.with(anything, a_hash_including("content_id" => content_id, "locale" => "cy"))
.never

described_class.call(payload)
end

it "does not send the other translation downstream if there is no base path" do
expect(DownstreamDraftWorker).to receive(:perform_async_in_queue)
.with(anything, a_hash_including("content_id" => content_id, "locale" => "cy"))
.never

payload = {
content_id:,
update_type: "major",
title: "Some Title",
publishing_app:,
rendering_app: "frontend",
document_type: "contact",
schema_name: "contact",
locale:,
redirects: [],
phase: "beta",
change_note:,
details: {},
}

described_class.call(payload)
end
end
end
end

0 comments on commit 81c17f5

Please sign in to comment.