diff --git a/app/jobs/govbox/destroy_box_data_job.rb b/app/jobs/govbox/destroy_box_data_job.rb new file mode 100644 index 000000000..9000a58c9 --- /dev/null +++ b/app/jobs/govbox/destroy_box_data_job.rb @@ -0,0 +1,10 @@ +module Govbox + class DestroyBoxDataJob < ApplicationJob + queue_as :default + + def perform(box) + Govbox::ApiConnection.find_by(box_id: box.id).destroy + Govbox::Folder.where(box_id: box.id).destroy_all + end + end +end diff --git a/app/lib/event_bus.rb b/app/lib/event_bus.rb index c29c44357..8fb3d896b 100644 --- a/app/lib/event_bus.rb +++ b/app/lib/event_bus.rb @@ -34,3 +34,4 @@ def self.reset! ->(message_thread_tag) { Searchable::ReindexMessageThreadJob.perform_later(message_thread_tag.message_thread) } EventBus.subscribe :tag_renamed, ->(tag) { Searchable::ReindexMessageThreadsWithTagIdJob.perform_later(tag.id) } EventBus.subscribe :tag_removed, ->(tag) { Searchable::ReindexMessageThreadsWithTagIdJob.perform_later(tag.id) } +EventBus.subscribe :box_destroyed, ->(box) { Govbox::DestroyBoxDataJob.perform_now(box) } diff --git a/app/models/box.rb b/app/models/box.rb index 5aac274e6..da0a0974a 100644 --- a/app/models/box.rb +++ b/app/models/box.rb @@ -17,9 +17,6 @@ class Box < ApplicationRecord has_many :message_threads, through: :folders, extend: MessageThreadsExtensions, dependent: :destroy has_many :message_drafts_imports, dependent: :destroy - before_destroy do - Govbox::ApiConnection.find_by(box_id: id).destroy - Govbox::Folder.where(box_id: id).destroy_all - end + before_destroy ->(box) { EventBus.publish(:box_destroyed, box) } end