Skip to content

Commit

Permalink
Add except_drafts scope
Browse files Browse the repository at this point in the history
  • Loading branch information
luciajanikova committed Nov 14, 2024
1 parent 568cf9d commit 55a89fa
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/jobs/fs/download_sent_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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(type: [nil, 'Message']).where("metadata ->> 'fs_message_id' = ?", fs_message_id).any?
return if box.messages.except_drafts.where("metadata ->> 'fs_message_id' = ?", fs_message_id).any?

ActiveRecord::Base.transaction do
fs_api = fs_client.api(api_connection: box.api_connection, box: box)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/fs/sync_box_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def perform(box, from: Date.today - 1.week, to: Date.tomorrow)
raise unless box.is_a?(Fs::Box)
return unless box.syncable?

box.messages.outbox.where(type: [nil, 'Message']).find_each do |outbox_message|
box.messages.outbox.except_drafts.find_each do |outbox_message|
DownloadSentMessageRelatedMessagesJob.perform_later(outbox_message, from: from, to: to)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/govbox/process_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ProcessMessageJob < ApplicationJob
retry_on ::ApplicationRecord::FailedToAcquireLockError, wait: :polynomially_longer, attempts: Float::INFINITY

def perform(govbox_message)
processed_message = ::Message.where(type: [nil, 'Message']).where(uuid: govbox_message.message_id).joins(:thread).where(thread: { box_id: govbox_message.box.id }).take
processed_message = ::Message.except_drafts.where(uuid: govbox_message.message_id).joins(:thread).where(thread: { box_id: govbox_message.box.id }).take

ActiveRecord::Base.transaction do
message = Govbox::Message.create_message_with_thread!(govbox_message)
Expand Down
3 changes: 2 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class Message < ApplicationRecord
delegate :tenant, to: :thread
delegate :box, to: :thread

scope :except_drafts, -> { where(type: [nil, 'Message']) }
scope :outbox, -> { where(outbox: true) }
scope :inbox, -> { where.not(outbox: true).where(type: [nil, 'Message']) }
scope :inbox, -> { except_drafts.where.not(outbox: true) }

after_update_commit ->(message) { EventBus.publish(:message_changed, message) }
after_destroy_commit ->(message) { EventBus.publish(:message_destroyed, message) }
Expand Down

0 comments on commit 55a89fa

Please sign in to comment.