diff --git a/app/models/concerns/status/fasp_concern.rb b/app/models/concerns/status/fasp_concern.rb index 748f22fb0f..84b95dcc5b 100644 --- a/app/models/concerns/status/fasp_concern.rb +++ b/app/models/concerns/status/fasp_concern.rb @@ -5,12 +5,22 @@ module Status::FaspConcern included do after_commit :announce_new_content_to_subscribed_fasp, on: :create + after_commit :announce_updated_content_to_subscribed_fasp, on: :update + after_commit :announce_deleted_content_to_subscribed_fasp, on: :destroy end private def announce_new_content_to_subscribed_fasp store_uri unless uri # TODO: solve this more elegantly - Fasp::AnnounceNewContentWorker.perform_async(uri) + Fasp::AnnounceContentLifecycleEventWorker.perform_async(uri, 'new') + end + + def announce_updated_content_to_subscribed_fasp + Fasp::AnnounceContentLifecycleEventWorker.perform_async(uri, 'update') + end + + def announce_deleted_content_to_subscribed_fasp + Fasp::AnnounceContentLifecycleEventWorker.perform_async(uri, 'delete') end end diff --git a/app/workers/fasp/announce_content_lifecycle_event_worker.rb b/app/workers/fasp/announce_content_lifecycle_event_worker.rb new file mode 100644 index 0000000000..f18b45fae0 --- /dev/null +++ b/app/workers/fasp/announce_content_lifecycle_event_worker.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class Fasp::AnnounceContentLifecycleEventWorker + include Sidekiq::Worker + + sidekiq_options queue: 'fasp', retry: 5 + + def perform(uri, event_type) + Fasp::Subscription.includes(:fasp_provider).content.lifecycle.each do |subscription| + announce(subscription, uri, event_type) + end + end + + private + + def announce(subscription, uri, event_type) + Fasp::Request.new(subscription.fasp_provider).post('/data_sharing/v0/announcements', body: { + source: { + subscription: { + id: subscription.id.to_s, + }, + }, + category: 'content', + eventType: event_type, + objectUris: [uri], + }) + end +end