Skip to content

Commit

Permalink
Republish drafts of other translations on PUT of new translation
Browse files Browse the repository at this point in the history
When a draft translation is added and another translation of the same
content ID exists, we need to send the existing translation(s)
downstream so their `available_translations` links can be updated to
include the new translation.

We're not currently doing this, which leads to a situation where the
link to the new translation never appears on the draft of the existing
translations.
  • Loading branch information
brucebolt committed Jun 12, 2024
1 parent 2ec6d7a commit c801987
Show file tree
Hide file tree
Showing 2 changed files with 66 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
42 changes: 41 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,45 @@
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
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
end
end
end

0 comments on commit c801987

Please sign in to comment.