diff --git a/app/commands/v2/put_content.rb b/app/commands/v2/put_content.rb index 87af32a281..e8792b74d3 100644 --- a/app/commands/v2/put_content.rb +++ b/app/commands/v2/put_content.rb @@ -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 @@ -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 diff --git a/spec/commands/v2/put_content_spec.rb b/spec/commands/v2/put_content_spec.rb index 5fc949fcaf..cf2089ccb9 100644 --- a/spec/commands/v2/put_content_spec.rb +++ b/spec/commands/v2/put_content_spec.rb @@ -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) @@ -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