Verified Commit b1166c99 authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

fix seed and factory reminder mailings VolunteerMailer handling

- rename VolunteerMailer methods to match ReminderMailing.kinds
  (trial_period, termination, half_year)
- if factory is passed 'sending_triggered: true' it sends mails
  after ReminderMailing's saved
- VolunteerMailer's method is picked directly from ReminderMailing.kind
parent 1366d6d4
Pipeline #22127 passed with stage
in 44 minutes and 57 seconds
......@@ -85,7 +85,7 @@ class ReminderMailingsController < ApplicationController
' versandt.'
end
@reminder_mailing.reminder_mailing_volunteers.picked.each do |mailing_volunteer|
VolunteerMailer.trial_period_reminder(mailing_volunteer).deliver_later
VolunteerMailer.public_send(@reminder_mailing.kind, mailing_volunteer).deliver_later
end
@reminder_mailing.update(sending_triggered: true)
redirect_to reminder_mailings_path, notice: 'Probezeit Erinnerungs-Emails werden versendet.'
......@@ -97,7 +97,7 @@ class ReminderMailingsController < ApplicationController
' versandt.'
end
@reminder_mailing.reminder_mailing_volunteers.picked.each do |mailing_volunteer|
VolunteerMailer.half_year_reminder(mailing_volunteer).deliver_later
VolunteerMailer.public_send(@reminder_mailing.kind, mailing_volunteer).deliver_later
end
@reminder_mailing.update(sending_triggered: true)
redirect_to reminder_mailings_path, notice: 'Halbjahres Erinnerungs-Emails werden versendet.'
......@@ -108,7 +108,7 @@ class ReminderMailingsController < ApplicationController
return redirect_to reminder_mailings_path, notice: 'Dieses Beendigungs-Mailing wurde bereits'\
' versandt.'
end
VolunteerMailer.termination_email(@reminder_mailing.reminder_mailing_volunteers.first).deliver_later
VolunteerMailer.public_send(@reminder_mailing.kind, @reminder_mailing.reminder_mailing_volunteers.first).deliver_later
@reminder_mailing.update(sending_triggered: true)
redirect_to reminder_mailings_path, notice: 'Beendigungs-Email wird versendet.'
end
......
......@@ -17,21 +17,21 @@ class VolunteerMailer < ApplicationMailer
mail(to: @volunteer.contact.primary_email, subject: @subject)
end
def termination_email(reminder_mailing_volunteer)
def termination(reminder_mailing_volunteer)
@volunteer = reminder_mailing_volunteer.volunteer
@subject, @body = reminder_mailing_volunteer.process_template.values_at(:subject, :body)
reminder_mailing_volunteer.update(email_sent: true)
mail(to: @volunteer.contact.primary_email, subject: @subject)
end
def trial_period_reminder(reminder_mailing_volunteer)
def trial_period(reminder_mailing_volunteer)
@volunteer = reminder_mailing_volunteer.volunteer
@subject, @body = reminder_mailing_volunteer.process_template.values_at(:subject, :body)
reminder_mailing_volunteer.update(email_sent: true)
mail(to: @volunteer.contact.primary_email, subject: @subject)
end
def half_year_reminder(reminder_mailing_volunteer)
def half_year(reminder_mailing_volunteer)
@volunteer = reminder_mailing_volunteer.volunteer
@subject, @body = reminder_mailing_volunteer.process_template.values_at(:subject, :body)
reminder_mailing_volunteer.update(email_sent: true)
......
......@@ -60,11 +60,11 @@ end
def handle_reminder_mailing_seed(mailer_type, reminder_mailables)
reminder_mailing = FactoryBot.create(:reminder_mailing, mailer_type, reminder_mailing_volunteers: reminder_mailables)
reminder_mailing.reminder_mailing_volunteers
.map { |rmv| rmv.update(picked: true) }
.each do |mailing_volunteer|
VolunteerMailer.trial_period_reminder(mailing_volunteer).deliver_later
end
reminder_mailing.reminder_mailing_volunteers.each do |mailing_volunteer|
mailing_volunteer.update(picked: true)
VolunteerMailer.public_send(reminder_mailing.kind.to_sym, mailing_volunteer).deliver
end
reminder_mailing.update(sending_triggered: true)
end
......
def create_reminder_mailable(reminder_mailing, volunteer)
case reminder_mailing.kind
when 'half_year'
create(:assignment, volunteer: volunteer, creator: reminder_mailing.creator,
period_start: FFaker::Time.between(6.months.ago, 12.months.ago))
when 'trial_period'
create(:assignment, volunteer: volunteer, creator: reminder_mailing.creator,
period_start: FFaker::Time.between(6.weeks.ago, 8.weeks.ago))
when 'termination'
start_date = FFaker::Time.between(1.year.ago, 2.years.ago)
create(:assignment, volunteer: volunteer, creator: reminder_mailing.creator,
period_start: start_date,
period_end: FFaker::Time.between(start_date + 100.days, 2.days.ago))
end
end
FactoryBot.define do
factory :reminder_mailing do
association :creator, factory: :user
......@@ -49,5 +33,34 @@ FactoryBot.define do
)
end
end
after(:save) do |reminder_mailing|
trigger_reminder_mailing_send(reminder_mailing) if reminder_mailing.sending_triggered
end
end
end
def create_reminder_mailable(reminder_mailing, volunteer)
case reminder_mailing.kind
when 'half_year'
create(:assignment, volunteer: volunteer, creator: reminder_mailing.creator,
period_start: FFaker::Time.between(6.months.ago, 12.months.ago))
when 'trial_period'
create(:assignment, volunteer: volunteer, creator: reminder_mailing.creator,
period_start: FFaker::Time.between(6.weeks.ago, 8.weeks.ago))
when 'termination'
start_date = FFaker::Time.between(1.year.ago, 2.years.ago)
create(:assignment, volunteer: volunteer, creator: reminder_mailing.creator,
period_start: start_date,
period_end: FFaker::Time.between(start_date + 100.days, 2.days.ago))
end
end
def trigger_reminder_mailing_send(reminder_mailing)
reminder_mailing.reminder_mailing_volunteers
.each do |mailing_volunteer|
mailing_volunteer.update(picked: true)
VolunteerMailer.public_send(reminder_mailing.kind, mailing_volunteer).deliver
end
end
......@@ -13,7 +13,7 @@ class VolunteerMailerTest < ActionMailer::TestCase
assignment = make_assignment(start_date: 7.weeks.ago)
mailing = create_probation_mailing(*group_assignments, assignment)
mailing.reminder_mailing_volunteers.each do |rmv|
mailer = VolunteerMailer.trial_period_reminder(rmv).deliver
mailer = VolunteerMailer.public_send(mailing.kind, rmv).deliver
assert_equal rmv.process_template[:subject], mailer.subject
assert mailer.to.include? rmv.volunteer.contact.primary_email
assert_match rmv.process_template[:body], mailer.body.encoded
......@@ -27,7 +27,7 @@ class VolunteerMailerTest < ActionMailer::TestCase
assignment = make_assignment(start_date: 8.months.ago)
mailing = create_half_year_mailing(*group_assignments, assignment)
mailing.reminder_mailing_volunteers.each do |rmv|
mailer = VolunteerMailer.half_year_reminder(rmv).deliver
mailer = VolunteerMailer.public_send(mailing.kind, rmv).deliver
assert_equal rmv.process_template[:subject], mailer.subject
assert mailer.to.include? rmv.volunteer.contact.primary_email
assert_match rmv.process_template[:body], mailer.body.encoded
......@@ -38,7 +38,7 @@ class VolunteerMailerTest < ActionMailer::TestCase
assignment = make_assignment(start_date: 8.months.ago, end_date: 2.days.ago)
mailing = create_termination_mailing(assignment)
mailing.reminder_mailing_volunteers do |rmv|
mailer = VolunteerMailer.termination_email(rmv).deliver
mailer = VolunteerMailer.public_send(mailing.kind, rmv).deliver
assert_equal rmv.process_template[:subject], mailer.subject
assert mailer.to.include? rmv.volunteer.contact.primary_email
assert_match rmv.process_template[:body], mailer.body.encoded
......@@ -53,7 +53,7 @@ class VolunteerMailerTest < ActionMailer::TestCase
body: '%{Anrede} %{Name} %{FeedbackLink} %{Einsatz} %{EinsatzTitel} %{EmailAbsender} '\
'%{EinsatzStart}'
mailing_volunteer = termination_reminder.reminder_mailing_volunteers.first
mailer = VolunteerMailer.termination_email(mailing_volunteer).deliver
mailer = VolunteerMailer.public_send(termination_reminder.kind, mailing_volunteer).deliver
assert_equal mailing_volunteer.process_template[:subject], mailer.subject
assert mailer.to.include? mailing_volunteer.volunteer.contact.primary_email
assert_match mailing_volunteer.volunteer.contact.natural_name, mailer.body.encoded
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment