Skip to content

Commit

Permalink
Send email when course participation is canceled (#637)
Browse files Browse the repository at this point in the history
* Add mailer

# Conflicts:
#	db/seeds/custom_contents.rb

* Add specs

* change event seeder so its faster to test events

PR (#1039)

---------

Co-authored-by: Andi Idogawa <largo@users.noreply.github.com>
  • Loading branch information
hunchr and Largo committed Sep 26, 2024
1 parent b56c406 commit 62d42f2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
23 changes: 23 additions & 0 deletions app/mailers/event/participation_canceled_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

class Event::ParticipationCanceledMailer < ApplicationMailer
include EventMailer
include MultilingualMailer

CONFIRMATION = "event_participation_canceled"

def confirmation(participation)
@participation = participation
@course = participation.event
@person = participation.person
headers[:bcc] = @course.groups.first.course_admin_email
locales = @course.language.split("_")

compose_multilingual(@person, CONFIRMATION, locales)
end
end
12 changes: 10 additions & 2 deletions app/models/sac_cas/event/participation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ module SacCas::Event::Participation
attr_accessor :adult_consent, :terms_and_conditions, :newsletter, :check_root_conditions

validates :adult_consent, :terms_and_conditions, acceptance: {if: :check_root_conditions}

validates :actual_days, numericality: {greater_than_or_equal_to: 0, allow_blank: true}

validate :assert_actual_days_size

after_update :send_application_canceled_email, if: :state_changed_to_canceled?
end

def subsidy_amount
Expand Down Expand Up @@ -59,4 +59,12 @@ def update_previous_state
self.previous_state = state_was
end
end

def state_changed_to_canceled?
saved_change_to_attribute(:state)&.second == "canceled"
end

def send_application_canceled_email
Event::ParticipationCanceledMailer.confirmation(self).deliver_later
end
end
13 changes: 13 additions & 0 deletions db/seeds/custom_contents.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
{key: Event::ApplicationConfirmationMailer::ASSIGNED,
placeholders_required: "event-name",
placeholders_optional: "recipient-name, event-details, event-number, event-link, application-url, application-closing-at, person-url, missing-information"},
{key: Event::ParticipationCanceledMailer::CONFIRMATION,
placeholders_required: "event-name",
placeholders_optional: "recipient-name, event-details, event-number, event-link, application-url, person-url"},
{key: Event::LeaderReminderMailer::REMINDER_NEXT_WEEK,
placeholders_required: "event-name",
placeholders_optional: "recipient-name, event-details, event-number, event-link"},
Expand Down Expand Up @@ -123,6 +126,16 @@
"Person: {person-url}<br>" \
"Event-Link: {event-link}<br>" \
"Kursdetails:<br><br>{event-details}<br><br>{missing-information}"},
{custom_content_id: CustomContent.get(Event::ParticipationCanceledMailer::CONFIRMATION).id,
locale: "de",
label: "Kurs: E-Mail Abmeldung",
subject: "Kursabmeldung bestätigt",
body: "Hallo {recipient-name},<br><br>" \
"Deine Abmeldung für den Kurs {event-name} (Nummer: {event-number}) wurde bestätigt. " \
"Anmeldung: {application-url}<br>" \
"Person: {person-url}<br>" \
"Event-Link: {event-link}<br>" \
"Kursdetails:<br><br>{event-details}<br><br>{missing-information}"},
{custom_content_id: CustomContent.get(Event::LeaderReminderMailer::REMINDER_NEXT_WEEK).id,
locale: "de",
label: "Kurs: E-Mail Kursvorbereitungen abschliessen",
Expand Down
5 changes: 4 additions & 1 deletion db/seeds/development/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

seeder = SacEventSeeder.new

10.times do
8.times do
seeder.seed_sac_course(Group.root.id)
end
2.times do
seeder.seed_sac_course_which_is_application_closed(Group.root.id)
end
8 changes: 8 additions & 0 deletions db/seeds/development/support/sac_event_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def seed_sac_course(group_id)
event.season = Event::Kind::SEASONS.sample
event.start_point_of_time = :day
event.contact_id = fetch_contact_person.id

event.save!
event
end

def seed_sac_course_which_is_application_closed(group_id)
event = seed_sac_course(group_id)
event.update_column(:state, "assignment_closed")
event.save!
end

Expand Down
25 changes: 25 additions & 0 deletions spec/mailers/event/participation_canceled_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of
# hitobito_sac_cas and licensed under the Affero General Public License version 3
# or later. See the COPYING file at the top-level directory or at
# https://github.com/hitobito/hitobito_sac_cas.

require "spec_helper"

describe Event::ParticipationCanceledMailer do
let(:event) { Fabricate(:sac_open_course) }
let(:participation) { event.participations.create!(person: people(:mitglied)) }
let(:mail) { described_class.confirmation(participation) }

before { event.groups.first.update!(course_admin_email: "admin@example.com") }

it "sends to email addresses of participant" do
expect(mail.to).to match_array(["e.hillary@hitobito.example.com"])
expect(mail.bcc).to match_array(["admin@example.com"])
expect(mail.body.to_s).to include(
"Hallo Edmund,",
"Deine Abmeldung für den Kurs Eventus (Nummer: #{event.number}) wurde bestätigt."
)
end
end
12 changes: 12 additions & 0 deletions spec/models/event/participation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
require "spec_helper"

describe Event::Participation do
include ActiveJob::TestHelper

describe "::callbacks" do
subject(:participation) { Fabricate(:event_participation, event: events(:top_course)) }

Expand Down Expand Up @@ -120,4 +122,14 @@ def build_role(key, role)
end
end
end

describe "canceled" do
let(:event) { Fabricate(:sac_open_course) }
let(:participation) { event.participations.create!(person: people(:mitglied)) }

it "sends a confirmation email" do
expect { participation.update(state: :canceled, canceled_at: Time.zone.today) }
.to have_enqueued_mail(Event::ParticipationCanceledMailer, :confirmation).once
end
end
end

0 comments on commit 62d42f2

Please sign in to comment.