Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GO-149 Download FS messages repeatedly #527

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading