diff --git a/app/jobs/fs/download_received_message_job.rb b/app/jobs/fs/download_received_message_job.rb index 4e860961..03413fa9 100644 --- a/app/jobs/fs/download_received_message_job.rb +++ b/app/jobs/fs/download_received_message_job.rb @@ -4,14 +4,17 @@ def perform(fs_message_id, box:, fs_client: FsEnvironment.fs_client) raise unless box.is_a?(Fs::Box) return unless box.syncable? - return if box.messages.where("metadata ->> 'fs_message_id' = ?", fs_message_id).any? + fs_api = fs_client.api(api_connection: box.api_connection, box: box) ActiveRecord::Base.transaction do - fs_api = fs_client.api(api_connection: box.api_connection, box: box) - + persisted_message = box.messages.where("metadata ->> 'fs_message_id' = ?", fs_message_id)&.take raw_message = fs_api.fetch_received_message(fs_message_id) - Fs::Message.create_inbox_message_with_thread!(raw_message, box: box) + if persisted_message + Fs::Message.update_message_data(persisted_message, raw_message) + else + Fs::Message.create_inbox_message_with_thread!(raw_message, box: box) + end end end end diff --git a/app/jobs/fs/download_sent_message_job.rb b/app/jobs/fs/download_sent_message_job.rb index a501fb65..0765fc21 100644 --- a/app/jobs/fs/download_sent_message_job.rb +++ b/app/jobs/fs/download_sent_message_job.rb @@ -4,16 +4,18 @@ def perform(fs_message_id, box:, fs_client: FsEnvironment.fs_client) raise unless box.is_a?(Fs::Box) return unless box.syncable? - return if box.messages.not_drafts.where("metadata ->> 'fs_message_id' = ?", fs_message_id).any? + fs_api = fs_client.api(api_connection: box.api_connection, box: box) ActiveRecord::Base.transaction do - fs_api = fs_client.api(api_connection: box.api_connection, box: box) - + persisted_message = box.messages.where("metadata ->> 'fs_message_id' = ?", fs_message_id)&.take raw_message = fs_api.fetch_sent_message(fs_message_id) - message = Fs::Message.create_outbox_message_with_thread!(raw_message, box: box) - - DownloadSentMessageRelatedMessagesJob.set(wait: 3.minutes).perform_later(message) + if persisted_message + Fs::Message.update_message_data(persisted_message, raw_message) + else + message = Fs::Message.create_outbox_message_with_thread!(raw_message, box: box) + DownloadSentMessageRelatedMessagesJob.set(wait: 3.minutes).perform_later(message) + end end end end diff --git a/app/jobs/fs/sync_box_job.rb b/app/jobs/fs/sync_box_job.rb index 1e135005..f1b5e28e 100644 --- a/app/jobs/fs/sync_box_job.rb +++ b/app/jobs/fs/sync_box_job.rb @@ -5,6 +5,7 @@ def perform(box, from: Date.today - 1.week, to: Date.tomorrow) return unless box.syncable? box.messages.outbox.not_drafts.find_each do |outbox_message| + DownloadSentMessageJob.perform_later(outbox_message.metadata['fs_message_id'], box: outbox_message.box) if (from..to).cover?(outbox_message.delivered_at) DownloadSentMessageRelatedMessagesJob.perform_later(outbox_message, from: from, to: to) end end diff --git a/app/models/fs/message.rb b/app/models/fs/message.rb index 89b9bbf1..dc6b3ca9 100644 --- a/app/models/fs/message.rb +++ b/app/models/fs/message.rb @@ -60,6 +60,24 @@ def self.create_outbox_message_with_thread!(raw_message, box:) message end + def self.update_message_data(message, raw_message) + puts 'Message data updated!' + + MessageThread.with_advisory_lock!(message.thread.merge_identifiers.first.uuid, transaction: true, timeout_seconds: 10) do + message.metadata.merge!( + { + "fs_status" => raw_message['status'], + "fs_submission_status" => raw_message['submission_status'], + "fs_period" => raw_message['period'], + "fs_dismissal_reason" => raw_message['dismissal_reason'], + "fs_other_attributes" => raw_message['other_attributes'], + } + ) + message.save! + update_html_visualization(message) + end + end + private def self.create_inbox_message(raw_message)