Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(699) Allow Content Block schedule to be edited #9699

Merged
merged 8 commits into from
Dec 5, 2024
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
6 changes: 6 additions & 0 deletions features/support/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
block.call
end
end

Around("@enable-sidekiq-test-mode") do |_scenario, block|
Sidekiq::Testing.fake! do
block.call
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def status_item
{
field: "Status",
value: scheduled_value,
edit: {
href: helpers.content_block_manager.content_block_manager_content_block_document_schedule_edit_path(content_block_document),
link_text: sanitize("Edit <span class='govuk-visually-hidden'>schedule</span>"),
link_text_no_enhance: true,
},
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module CanScheduleOrPublish
extend ActiveSupport::Concern

def schedule_or_publish(template = "content_block_manager/content_block/editions/workflow/schedule_publishing")
@schema = ContentBlockManager::ContentBlock::Schema.find_by_block_type(@content_block_edition.document.block_type)

if params[:schedule_publishing].blank?
@content_block_edition.errors.add(:schedule_publishing, "cannot be blank")
raise ActiveRecord::RecordInvalid, @content_block_edition
elsif params[:schedule_publishing] == "schedule"
ContentBlockManager::ScheduleEditionService.new(@schema).call(@content_block_edition, scheduled_publication_params)
else
publish and return
end

redirect_to content_block_manager.content_block_manager_content_block_workflow_path(id: @content_block_edition.id,
step: :confirmation,
is_scheduled: true)
rescue ActiveRecord::RecordInvalid
render template
end

def publish
new_edition = ContentBlockManager::PublishEditionService.new.call(@content_block_edition)
redirect_to content_block_manager.content_block_manager_content_block_workflow_path(id: new_edition.id, step: :confirmation)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class ContentBlockManager::ContentBlock::Documents::ScheduleController < ContentBlockManager::BaseController
include CanScheduleOrPublish

def edit
document = ContentBlockManager::ContentBlock::Document.find(params[:document_id])
@content_block_edition = document.latest_edition
end

def update
document = ContentBlockManager::ContentBlock::Document.find(params[:document_id])
@content_block_edition = document.latest_edition
schedule_or_publish("content_block_manager/content_block/documents/schedule/edit")
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ContentBlockManager::ContentBlock::Editions::WorkflowController < ContentBlockManager::BaseController
include CanScheduleOrPublish

NEW_BLOCK_STEPS = {
review: "review",
edit_draft: "edit_draft",
Expand Down Expand Up @@ -115,27 +117,8 @@ def schedule_publishing
render :schedule_publishing
end

def schedule_or_publish
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id])
@schema = ContentBlockManager::ContentBlock::Schema.find_by_block_type(@content_block_edition.document.block_type)

if params[:schedule_publishing].blank?
@content_block_edition.errors.add(:schedule_publishing, "cannot be blank")
raise ActiveRecord::RecordInvalid, @content_block_edition
elsif params[:schedule_publishing] == "schedule"
ContentBlockManager::ScheduleEditionService.new(@schema).call(@content_block_edition, scheduled_publication_params)
else
publish and return
end

redirect_to content_block_manager.content_block_manager_content_block_workflow_path(id: @content_block_edition.id,
step: :confirmation,
is_scheduled: true)
rescue ActiveRecord::RecordInvalid
render "content_block_manager/content_block/editions/workflow/schedule_publishing"
end

REVIEW_ERROR = Data.define(:attribute, :full_message)

def publish
if params[:step] == NEW_BLOCK_STEPS[:review] && params[:is_confirmed].blank?
@confirm_error_copy = "Confirm details are correct"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module ContentBlock::Edition::Workflow

module ClassMethods
def valid_state?(state)
%w[draft published scheduled].include?(state)
%w[draft published scheduled superseded].include?(state)
end
end

Expand All @@ -20,13 +20,17 @@ def valid_state?(state)
state :draft
state :published
state :scheduled
state :superseded

event :publish do
transitions from: %i[draft scheduled], to: :published
end
event :schedule do
transitions from: %i[draft], to: :scheduled
end
event :supersede do
transitions from: %i[scheduled], to: :superseded
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module ContentBlockManager
module Concerns
module Dequeueable
extend ActiveSupport::Concern

def dequeue_all_previously_queued_editions(content_block_edition)
content_block_edition.document.editions.where(state: :scheduled).find_each do |edition|
next if content_block_edition.id == edition.id

ContentBlockManager::SchedulePublishingWorker.dequeue(edition)
edition.supersede!
end
end

def dequeue_current_edition_if_previously_scheduled(content_block_edition)
ContentBlockManager::SchedulePublishingWorker.dequeue(content_block_edition) if content_block_edition.scheduled?
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module ContentBlockManager
class CreateEditionService
include Publishable

def initialize(schema)
@schema = schema
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,74 @@
module ContentBlockManager
class PublishEditionService
include Publishable
class PublishingFailureError < StandardError; end

include Concerns::Dequeueable

def call(edition)
publish_with_rollback(edition)
end

private

def publish_with_rollback(content_block_edition)
document = content_block_edition.document
schema = ContentBlockManager::ContentBlock::Schema.find_by_block_type(document.block_type)
content_id = document.content_id
content_id_alias = document.content_id_alias

create_publishing_api_edition(
content_id:,
content_id_alias:,
schema_id: schema.id,
title: content_block_edition.title,
details: content_block_edition.details,
instructions_to_publishers: content_block_edition.instructions_to_publishers,
links: {
primary_publishing_organisation: [
content_block_edition.lead_organisation.content_id,
],
},
)
dequeue_current_edition_if_previously_scheduled(content_block_edition)
dequeue_all_previously_queued_editions(content_block_edition)
publish_publishing_api_edition(content_id:)
update_content_block_document_with_latest_edition(content_block_edition)
content_block_edition.public_send(:publish!)
content_block_edition
rescue PublishingFailureError => e
discard_publishing_api_edition(content_id:)
raise e
end

def create_publishing_api_edition(content_id:, content_id_alias:, schema_id:, title:, instructions_to_publishers:, details:, links:)
Services.publishing_api.put_content(content_id, {
schema_name: schema_id,
document_type: schema_id,
publishing_app: Whitehall::PublishingApp::WHITEHALL,
title:,
instructions_to_publishers:,
content_id_alias:,
details:,
links:,
update_type: "major",
})
end

def publish_publishing_api_edition(content_id:)
Services.publishing_api.publish(content_id, "content_block")
rescue GdsApi::HTTPErrorResponse => e
raise PublishingFailureError, "Could not publish #{content_id} because: #{e.message}"
end

def update_content_block_document_with_latest_edition(content_block_edition)
content_block_edition.document.update!(
latest_edition_id: content_block_edition.id,
live_edition_id: content_block_edition.id,
)
end

def discard_publishing_api_edition(content_id:)
Services.publishing_api.discard_draft(content_id)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ContentBlockManager
class ScheduleEditionService
include Publishable
include Concerns::Dequeueable

def initialize(schema)
@schema = schema
Expand All @@ -18,6 +18,20 @@ def call(edition, scheduled_publication_params)

private

def schedule_with_rollback
raise ArgumentError, "Local database changes not given" unless block_given?

ActiveRecord::Base.transaction do
content_block_edition = yield

dequeue_current_edition_if_previously_scheduled(content_block_edition)
content_block_edition.schedule! unless content_block_edition.scheduled?

dequeue_all_previously_queued_editions(content_block_edition)
ContentBlockManager::SchedulePublishingWorker.queue(content_block_edition)
end
end

def send_publish_intents_for_host_documents(content_block_edition:)
host_content_items = ContentBlockManager::GetHostContentItems.by_embedded_document(
content_block_document: content_block_edition.document,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= render "content_block_manager/content_block/shared/schedule_publishing", {
context: "Edit a content block",
back_link: content_block_manager.content_block_manager_content_block_document_path(id: @content_block_edition.document.id),
form_url: content_block_manager.content_block_manager_content_block_document_update_schedule_path(document_id: @content_block_edition.document.id),
} %>
Loading
Loading