Skip to content

Commit

Permalink
Download FS messages repeatedly
Browse files Browse the repository at this point in the history
  • Loading branch information
luciajanikova committed Dec 12, 2024
1 parent f8ea9d3 commit a9b4965
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
11 changes: 7 additions & 4 deletions app/jobs/fs/download_received_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions app/jobs/fs/download_sent_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/jobs/fs/sync_box_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions app/models/fs/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a9b4965

Please sign in to comment.