class Notification < ApplicationRecord belongs_to :member def self.send_scheduled_for_today Member .where('expires_on < ?', Date.today - self.templates.first[:to_be_sent_on]) .filter_map(&:todays_notification) .each(&:deliver!) end def self.templates [ { to_be_sent_on: -60.days, template: "expiration_in_60d" }, { to_be_sent_on: -30.days, template: "expiration_in_30d" }, { to_be_sent_on: 0.days, template: "expired" }, { to_be_sent_on: 30.days, template: "expired_30d_ago" }, { to_be_sent_on: 60.days, template: "expired_60d_ago" }, { to_be_sent_on: Config.payment_pending_grace_period, template: "cancelled" }, ] end def deliver! # TODO: do something about failures save! NotificationMailer.with(member: self.member).send(template).deliver_now! update(status: 'sent', sent_at: Time.current) end end