2022-06-25 12:48:46 +00:00
|
|
|
class Notification < ApplicationRecord
|
|
|
|
belongs_to :member
|
|
|
|
|
|
|
|
scope :scheduled_for_today, ->() { where(status: 'scheduled', to_be_sent_on: Date.today) }
|
|
|
|
|
|
|
|
def self.send_scheduled_for_today
|
2023-04-05 22:04:48 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
ActiveRecord::Base.connection.execute('LOCK members IN ACCESS EXCLUSIVE MODE')
|
|
|
|
|
|
|
|
scheduled_for_today.each do |n|
|
|
|
|
n.deliver!
|
|
|
|
end
|
2022-06-25 12:48:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deliver!
|
|
|
|
# actually send the email.
|
|
|
|
NotificationMailer.with(notification: self).send(template).deliver_now!
|
|
|
|
|
|
|
|
update(status: 'sent', sent_at: Time.current)
|
|
|
|
# TODO: do something about failures
|
|
|
|
end
|
|
|
|
end
|