Simplify mailer parameters to be serializable (even if mail_template.id is nil)
This commit is contained in:
parent
3ba5de88b6
commit
0f945e2fea
4 changed files with 46 additions and 29 deletions
|
@ -1,7 +1,12 @@
|
|||
class NotificationMailer < ApplicationMailer
|
||||
default to: Proc.new { @user.email }
|
||||
|
||||
after_action :create_commentaire_for_notification, only: :send_notification
|
||||
after_action :create_commentaire_for_notification, only: [:send_notification, :send_dossier_received]
|
||||
|
||||
def send_dossier_received(dossier_id)
|
||||
dossier = Dossier.find(dossier_id)
|
||||
send_notification(dossier, dossier.procedure.received_mail_template)
|
||||
end
|
||||
|
||||
def send_notification(dossier, mail_template, attestation = nil)
|
||||
vars_mailer(dossier)
|
||||
|
|
|
@ -75,7 +75,7 @@ class Dossier < ActiveRecord::Base
|
|||
|
||||
after_save :build_default_champs, if: Proc.new { procedure_id_changed? }
|
||||
after_save :build_default_individual, if: Proc.new { procedure.for_individual? }
|
||||
after_save :send_notification_email
|
||||
after_save :send_dossier_received
|
||||
|
||||
validates :user, presence: true
|
||||
|
||||
|
@ -375,9 +375,9 @@ class Dossier < ActiveRecord::Base
|
|||
value.nil? || value.kind_of?(Time) ? value : value.to_s
|
||||
end
|
||||
|
||||
def send_notification_email
|
||||
def send_dossier_received
|
||||
if state_changed? && EN_INSTRUCTION.include?(state)
|
||||
NotificationMailer.send_notification(self, procedure.received_mail_template).deliver_later
|
||||
NotificationMailer.send_dossier_received(id).deliver_later
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,17 +1,30 @@
|
|||
require "spec_helper"
|
||||
|
||||
RSpec.describe NotificationMailer, type: :mailer do
|
||||
shared_examples_for "create a commentaire not notified" do
|
||||
it do
|
||||
expect { subject.deliver_now }.to change { Commentaire.count }.by(1)
|
||||
expect { subject.deliver_now }.to_not change { Notification.count }
|
||||
|
||||
subject.deliver_now
|
||||
commentaire = Commentaire.last
|
||||
expect(commentaire.body).to include(email_template.object_for_dossier(dossier), email_template.body_for_dossier(dossier))
|
||||
expect(commentaire.dossier).to eq(dossier)
|
||||
end
|
||||
end
|
||||
|
||||
let(:user) { create(:user) }
|
||||
let(:dossier) { create(:dossier, user: user) }
|
||||
|
||||
describe '.send_notification' do
|
||||
let(:user) { create(:user) }
|
||||
let(:dossier) { create(:dossier, user: user) }
|
||||
let(:email) { instance_double('email', object_for_dossier: 'object', body_for_dossier: 'body') }
|
||||
let(:email_template) { instance_double('email_template', object_for_dossier: 'object', body_for_dossier: 'body') }
|
||||
let(:attestation) { nil }
|
||||
let(:notifications_count_before) { Notification.count }
|
||||
|
||||
subject { described_class.send_notification(dossier, email, attestation) }
|
||||
subject { described_class.send_notification(dossier, email_template, attestation) }
|
||||
|
||||
it { expect(subject.subject).to eq(email.object_for_dossier) }
|
||||
it { expect(subject.body).to eq(email.body_for_dossier) }
|
||||
it { expect(subject.subject).to eq(email_template.object_for_dossier) }
|
||||
it { expect(subject.body).to eq(email_template.body_for_dossier) }
|
||||
it { expect(subject.attachments['attestation.pdf']).to eq(nil) }
|
||||
|
||||
context 'when an attestation is provided' do
|
||||
|
@ -25,23 +38,26 @@ RSpec.describe NotificationMailer, type: :mailer do
|
|||
end
|
||||
end
|
||||
|
||||
it "creates a commentaire, which is not notified" do
|
||||
described_class.send_notification(dossier, email).deliver_now
|
||||
it_behaves_like "create a commentaire not notified"
|
||||
end
|
||||
|
||||
commentaire = Commentaire.last
|
||||
notifications_count_after = Notification.count
|
||||
describe '.send_dossier_received' do
|
||||
subject { described_class.send_dossier_received(dossier.id) }
|
||||
let(:email_template) { create(:received_mail) }
|
||||
|
||||
expect(commentaire.dossier).to eq(dossier)
|
||||
expect(commentaire.email).to eq("contact@tps.apientreprise.fr")
|
||||
expect(commentaire.body).to eq("[object]<br><br>body")
|
||||
expect(notifications_count_before).to eq(notifications_count_after)
|
||||
before do
|
||||
dossier.procedure.received_mail = email_template
|
||||
end
|
||||
|
||||
it do
|
||||
expect(subject.subject).to eq(email_template.object)
|
||||
expect(subject.body).to eq(email_template.body)
|
||||
end
|
||||
|
||||
it_behaves_like "create a commentaire not notified"
|
||||
end
|
||||
|
||||
describe ".new_answer" do
|
||||
let(:user) { create(:user) }
|
||||
let(:dossier) { create(:dossier, user: user) }
|
||||
|
||||
subject(:subject) { described_class.new_answer(dossier) }
|
||||
|
||||
it { expect(subject.body).to match('Un nouveau message est disponible dans votre espace TPS.') }
|
||||
|
|
|
@ -766,26 +766,22 @@ describe Dossier do
|
|||
it { is_expected.to include(dossier4)}
|
||||
end
|
||||
|
||||
describe "#send_notification_email" do
|
||||
describe "#send_dossier_received" do
|
||||
let(:procedure) { create(:procedure) }
|
||||
let(:dossier) { create(:dossier, procedure: procedure, state: :initiated) }
|
||||
|
||||
before do
|
||||
ActionMailer::Base.deliveries.clear
|
||||
allow(NotificationMailer).to receive(:send_dossier_received).and_return(double(deliver_later: nil))
|
||||
end
|
||||
|
||||
it "sends an email when the dossier becomes received" do
|
||||
dossier.received!
|
||||
|
||||
mail = ActionMailer::Base.deliveries.last
|
||||
|
||||
expect(mail.subject).to eq("Votre dossier TPS nº #{dossier.id} va être instruit")
|
||||
expect(NotificationMailer).to have_received(:send_dossier_received).with(dossier.id)
|
||||
end
|
||||
|
||||
it "does not an email when the dossier becomes closed" do
|
||||
dossier.closed!
|
||||
|
||||
expect(ActionMailer::Base.deliveries.size).to eq(0)
|
||||
expect(NotificationMailer).to_not have_received(:send_dossier_received)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue