Class: Nuntius::PurgeMessageJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/nuntius/purge_message_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(account_id, months) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'app/jobs/nuntius/purge_message_job.rb', line 3

def perform(, months)
  messages = Nuntius::Message.distinct.select(:id).where("metadata ->> 'account_id' = :account", account: )
    .where(created_at: ..months.months.ago.beginning_of_day)
    .where.not(status: %w[complaint bounced])

  Nuntius::Message.where(parent_message_id: messages.pluck(:id)).in_batches.update_all(parent_message_id: nil)

  messages.in_batches.destroy_all
end