diff --git a/app/helpers/admin/republishing_helper.rb b/app/helpers/admin/republishing_helper.rb index 6715436d8897..4516f9f2246e 100644 --- a/app/helpers/admin/republishing_helper.rb +++ b/app/helpers/admin/republishing_helper.rb @@ -17,6 +17,13 @@ def bulk_content_type_metadata confirmation_path: admin_bulk_republishing_all_confirm_path("all-documents-with-pre-publication-editions"), republish_method: -> { BulkRepublisher.new.republish_all_documents_with_pre_publication_editions }, }, + all_documents_with_pre_publication_editions_with_html_attachments: { + id: "all-documents-with-pre-publication-editions-with-html-attachments", + name: "all documents with pre-publication editions with HTML attachments", + republishing_path: admin_bulk_republishing_all_republish_path("all-documents-with-pre-publication-editions-with-html-attachments"), + confirmation_path: admin_bulk_republishing_all_confirm_path("all-documents-with-pre-publication-editions-with-html-attachments"), + republish_method: -> { BulkRepublisher.new.republish_all_documents_with_pre_publication_editions_with_html_attachments }, + }, all_published_organisation_about_us_pages: { id: "all-published-organisation-about-us-pages", name: "all published organisation 'About us' pages", diff --git a/app/models/republishing_event.rb b/app/models/republishing_event.rb index 5adf36bf08d2..c3ea1b0c6e02 100644 --- a/app/models/republishing_event.rb +++ b/app/models/republishing_event.rb @@ -11,6 +11,7 @@ class RepublishingEvent < ApplicationRecord enum :bulk_content_type, %i[ all_documents all_documents_with_pre_publication_editions + all_documents_with_pre_publication_editions_with_html_attachments all_published_organisation_about_us_pages ] end diff --git a/app/services/bulk_republisher.rb b/app/services/bulk_republisher.rb index 4a045ce7b1cf..c8204db4b03c 100644 --- a/app/services/bulk_republisher.rb +++ b/app/services/bulk_republisher.rb @@ -24,4 +24,15 @@ def republish_all_documents_with_pre_publication_editions PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", edition.document.id, true) end end + + def republish_all_documents_with_pre_publication_editions_with_html_attachments + document_ids = Edition + .in_pre_publication_state + .where(id: HtmlAttachment.where(attachable_type: "Edition").select(:attachable_id)) + .pluck(:document_id) + + document_ids.each do |document_id| + PublishingApiDocumentRepublishingWorker.perform_async_in_queue("bulk_republishing", document_id, true) + end + end end diff --git a/test/unit/app/services/bulk_republisher_test.rb b/test/unit/app/services/bulk_republisher_test.rb index a7c29cb7f1f4..e2baa3934b26 100644 --- a/test/unit/app/services/bulk_republisher_test.rb +++ b/test/unit/app/services/bulk_republisher_test.rb @@ -73,4 +73,46 @@ class BulkRepublisherTest < ActiveSupport::TestCase BulkRepublisher.new.republish_all_documents_with_pre_publication_editions end end + + describe "#republish_all_documents_with_pre_publication_editions_with_html_attachments" do + test "queues all documents with pre-publication editions with HTML attachments for republishing" do + queue_sequence = sequence("queue") + + 2.times do + draft_edition = build(:draft_edition) + document_with_pre_publication_edition_with_html_attachment = create(:document, editions: [build(:published_edition), draft_edition]) + create(:html_attachment, attachable_type: "Edition", attachable_id: draft_edition.id) + + PublishingApiDocumentRepublishingWorker + .expects(:perform_async_in_queue) + .with("bulk_republishing", document_with_pre_publication_edition_with_html_attachment.id, true) + .in_sequence(queue_sequence) + end + + BulkRepublisher.new.republish_all_documents_with_pre_publication_editions_with_html_attachments + end + + test "doesn't queue documents for republishing if the editions with HTML attachments aren't pre-publication editions" do + document = create(:document, editions: [build(:published_edition)]) + create(:html_attachment, attachable_type: "Edition", attachable_id: document.live_edition_id) + + PublishingApiDocumentRepublishingWorker + .expects(:perform_async_in_queue) + .with("bulk_republishing", document.id, true) + .never + + BulkRepublisher.new.republish_all_documents_with_pre_publication_editions_with_html_attachments + end + + test "doesn't queue documents republishing when there are pre-publication editions but none have HTML attachments" do + document = create(:document, editions: [build(:published_edition), build(:draft_edition)]) + + PublishingApiDocumentRepublishingWorker + .expects(:perform_async_in_queue) + .with("bulk_republishing", document.id, true) + .never + + BulkRepublisher.new.republish_all_documents_with_pre_publication_editions_with_html_attachments + end + end end