send invite notifications using a hook

This commit is contained in:
clemkeirua 2020-09-25 11:10:12 +02:00 committed by Keirua (Rebase PR Action)
parent dd7840cdc4
commit d23b1136ad
2 changed files with 11 additions and 6 deletions

View file

@ -15,12 +15,7 @@ class InvitesController < ApplicationController
) )
if invite.valid? if invite.valid?
if invite.user.present? # The notification is sent through an after commit hook in order to avoir concurrency issues
InviteMailer.invite_user(invite).deliver_later
else
InviteMailer.invite_guest(invite).deliver_later
end
flash.notice = "Une invitation a été envoyée à #{invite.email}." flash.notice = "Une invitation a été envoyée à #{invite.email}."
else else
flash.alert = invite.errors.full_messages flash.alert = invite.errors.full_messages

View file

@ -19,6 +19,8 @@ class Invite < ApplicationRecord
before_validation -> { sanitize_email(:email) } before_validation -> { sanitize_email(:email) }
after_save_commit :send_notification
validates :email, presence: true validates :email, presence: true
validates :email, uniqueness: { scope: :dossier_id } validates :email, uniqueness: { scope: :dossier_id }
@ -32,4 +34,12 @@ class Invite < ApplicationRecord
scope :kept, -> { joins(:dossier).merge(Dossier.kept) } scope :kept, -> { joins(:dossier).merge(Dossier.kept) }
default_scope { kept } default_scope { kept }
def send_notification
if self.user.present?
InviteMailer.invite_user(self).deliver_later
else
InviteMailer.invite_guest(self).deliver_later
end
end
end end