saucy/app/mailers/notification_mailer.rb
Hugo Peixoto 76df4d41b7 Redo notification system
Previously, saucy generated each member's future notification every day
(marked as scheduled). And every day saucy would deliver every unsent
notification scheduled for that day.

This means that each member with expiration date in the future had ~6
notifications scheduled for them in the database.

This was troublesome because if the cron job that delivers notification
was down or the server was down for more than 24h, notifications were
silently skipped and we had no easy way of detecting it: we had to check
every member for missing sent notifications.

It also had the disadvantage that we were deleting and recreating
hundreds of database entries for no good reason.

To fix this, the new system no longer stores future scheduled
notifications. Instead, it now only stores sent notifications and
notifications currently being delivered. Every day, the deliver task
checks every member if there's a notification that needs to be sent in
that day, by checking if we're past the date of sending a particular
notification type and checking if in that window of time no
notifications of that type have been sent.

Let's suppose we send 6 notifications, one per month starting 60 days
before the membership expires until 90 days after:

  -60d   -30d    t=0    30d    60d    90d
----|------|------|------|------|------|------
    A      B      C      D      E      F

If we're on day t=-60d, we need to deliver a notification of type A. But
first we check if no notifications of type A have been sent to that
member on that day.

From days t=-59d to t=-31d, we check if in the time range t=[-60d, -31d]
any notification of type A was sent to that member. If not, we need to
deliver it.

If we're on days t=[-30d, -1d], we do the same but for notification of
type B.
2024-03-23 15:31:32 +00:00

65 lines
998 B
Ruby

class NotificationMailer < ApplicationMailer
default to: ->() { @member.email }
def expiration_in_60d
set_member
mail
end
def expiration_in_30d
set_member
mail
end
def expired
set_member
mail
end
def expired_30d_ago
set_member
mail
end
def expired_60d_ago
set_member
mail
end
def cancelled
set_member
mail
end
def registration
set_member
mail
end
def first_payment_confirmation
set_contribution
mail
end
def payment_renewal_confirmation
set_contribution
mail
end
private
def set_contribution
@contribution = params[:contribution]
@member = @contribution.member
end
def set_member
@member = params[:member]
@payment = @member.create_payment
end
def default_i18n_subject
mailer_scope = self.class.mailer_name.tr("/", ".")
I18n.t(:subject, scope: [mailer_scope, action_name], organization_short_name: Config.organization_short_name)
end
end