Skip to content

Commit

Permalink
Enqueue job to delete assets when destroying a policy group attachment
Browse files Browse the repository at this point in the history
Previously, when deleting a policy group attachment, we called the `AttachmentUpdater` - this enqueued the `MetadataWorker`, which dealt with both deletion and updates. We have since separated the update and delete logic for editions. We are now explicit about the fact that the delete request only gets sent to Asset Manager on publish. This logic is captured in the `AttachmentAssetDeleter`.

Nonetheless, since Policy Groups are not editionable, they do not use that workflow - their attachments go live upon creation and should be removed from live immediately upon being destroyed. This commit ensures that assets belonging to a policy group's deleted attachments are deleted from Asset Manager on update.
  • Loading branch information
lauraghiorghisor-tw committed Jan 22, 2025
1 parent 77a59a5 commit ab930d7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/models/policy_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PolicyGroup < ApplicationRecord
after_create :extract_dependencies
after_update :extract_dependencies
after_destroy :remove_all_dependencies
after_update :publish_deleted_attachments

def extract_dependencies
remove_all_dependencies
Expand All @@ -28,6 +29,14 @@ def extract_dependencies
end
end

def publish_deleted_attachments
deleted_attachments.each do |attachment|
next unless attachment.attachment_data.present? && attachment.deleted?

DeleteAttachmentAssetJob.perform_async(attachment.attachment_data.id)
end
end

def remove_all_dependencies
policy_group_dependencies.delete_all
end
Expand Down
31 changes: 31 additions & 0 deletions test/integration/attachment_deletion_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,37 @@ class AttachmentDeletionIntegrationTest < ActionDispatch::IntegrationTest
end
end

context "given a policy group" do
let(:managing_editor) { create(:managing_editor) }
let(:attachable) { create(:policy_group) }
let(:attachment) { build(:file_attachment, attachable:) }
let(:asset_manager_id) { attachment.attachment_data.assets.first.asset_manager_id }

before do
login_as(managing_editor)
stub_asset(asset_manager_id, { "draft" => false, "parent_document_url" => nil })

attachable.attachments << [attachment]
attachable.save!
end

it "deletes the corresponding asset in Asset Manager when the policy group is saved" do
Services.asset_manager.expects(:delete_asset).once.with(asset_manager_id)
Services.asset_manager.expects(:update_asset).with(asset_manager_id).never

visit admin_policy_group_attachments_path(attachable)
within page.find("li", text: attachment.title) do
click_link "Delete attachment"
end
click_button "Delete attachment"
assert_text "Attachment deleted"
click_link "Group"
click_button "Save"

DeleteAttachmentAssetJob.drain
end
end

private

def setup_publishing_api_for(edition)
Expand Down
30 changes: 30 additions & 0 deletions test/unit/app/models/policy_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,34 @@ class PolicyGroupTest < ActiveSupport::TestCase

assert_empty policy_group.depended_upon_contacts
end

test "deletes assets belonging to deleted attachments on save" do
first_attachment = create(:file_attachment, ordering: 1)
second_attachment = create(:file_attachment, ordering: 2)
policy_group = FactoryBot.create(
:policy_group,
attachments: [first_attachment, second_attachment],
)
first_attachment.destroy!
second_attachment.destroy!

DeleteAttachmentAssetJob.expects(:perform_async).with(first_attachment.attachment_data.id).once
DeleteAttachmentAssetJob.expects(:perform_async).with(second_attachment.attachment_data.id).once

policy_group.save!
end

test "does not delete assets belonging to not deleted attachments on save" do
first_attachment = create(:file_attachment, ordering: 1)
second_attachment = create(:file_attachment, ordering: 2)
policy_group = FactoryBot.create(
:policy_group,
attachments: [first_attachment, second_attachment],
)

DeleteAttachmentAssetJob.expects(:perform_async).with(first_attachment.attachment_data.id).never
DeleteAttachmentAssetJob.expects(:perform_async).with(second_attachment.attachment_data.id).never

policy_group.save!
end
end

0 comments on commit ab930d7

Please sign in to comment.