24 lines
620 B
Ruby
24 lines
620 B
Ruby
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
|
|
ActiveRecord::Base.transaction do
|
|
ActiveRecord::Base.connection.execute('LOCK members IN ACCESS EXCLUSIVE MODE')
|
|
|
|
scheduled_for_today.each do |n|
|
|
n.deliver!
|
|
end
|
|
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
|