feat: Implement comprehensive email notifications system
This commit implements a complete email notifications system for purchase confirmations and event reminders as requested in the medium priority backlog tasks. ## Features Added ### Purchase Confirmation Emails - Automatically sent when orders are marked as paid - Supports both single tickets and multi-ticket orders - Includes PDF ticket attachments - Professional HTML and text templates in French ### Event Reminder Emails - Automated reminders sent 7 days, 1 day, and day of events - Only sent to users with active tickets - Smart messaging based on time until event - Venue details and ticket information included ### Background Jobs - EventReminderJob: Sends reminders to all users for a specific event - EventReminderSchedulerJob: Daily scheduler to queue reminder jobs - Proper error handling and logging ### Email Templates - Responsive HTML templates with ApéroNight branding - Text fallbacks for better email client compatibility - Dynamic content based on number of tickets and time until event ### Configuration & Testing - Environment-based SMTP configuration for production - Development setup with MailCatcher support - Comprehensive test suite with mocking for PDF generation - Integration tests for end-to-end functionality - Documentation with usage examples ## Technical Implementation - Enhanced TicketMailer with new notification methods - Background job scheduling via Rails initializer - Order model integration for automatic purchase confirmations - Proper associations handling for user/ticket relationships - Configurable via environment variables ## Files Added/Modified - Enhanced app/mailers/ticket_mailer.rb with order support - Added app/jobs/event_reminder_*.rb for background processing - Updated email templates in app/views/ticket_mailer/ - Added automatic scheduling in config/initializers/ - Comprehensive test coverage in test/ directory - Complete documentation in docs/email-notifications.md 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
31
test/jobs/event_reminder_job_test.rb
Normal file
31
test/jobs/event_reminder_job_test.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
require "test_helper"
|
||||
|
||||
class EventReminderJobTest < ActiveJob::TestCase
|
||||
def setup
|
||||
@event = events(:concert_event)
|
||||
@user = users(:one)
|
||||
@ticket = tickets(:one)
|
||||
end
|
||||
|
||||
test "performs event reminder job for users with tickets" do
|
||||
# Mock the mailer to avoid actual email sending in tests
|
||||
TicketMailer.expects(:event_reminder).with(@user, @event, 7).returns(stub(deliver_now: true))
|
||||
|
||||
EventReminderJob.perform_now(@event.id, 7)
|
||||
end
|
||||
|
||||
test "handles missing event gracefully" do
|
||||
assert_raises(ActiveRecord::RecordNotFound) do
|
||||
EventReminderJob.perform_now(999999, 7)
|
||||
end
|
||||
end
|
||||
|
||||
test "logs error when mailer fails" do
|
||||
# Mock a failing mailer
|
||||
TicketMailer.stubs(:event_reminder).raises(StandardError.new("Test error"))
|
||||
|
||||
Rails.logger.expects(:error).with(regexp_matches(/Failed to send event reminder/))
|
||||
|
||||
EventReminderJob.perform_now(@event.id, 7)
|
||||
end
|
||||
end
|
||||
50
test/jobs/event_reminder_scheduler_job_test.rb
Normal file
50
test/jobs/event_reminder_scheduler_job_test.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
require "test_helper"
|
||||
|
||||
class EventReminderSchedulerJobTest < ActiveJob::TestCase
|
||||
def setup
|
||||
@event = events(:concert_event)
|
||||
end
|
||||
|
||||
test "schedules weekly reminders for events starting in 7 days" do
|
||||
# Set event to start in exactly 7 days
|
||||
@event.update(start_time: 7.days.from_now.beginning_of_day + 10.hours)
|
||||
|
||||
assert_enqueued_with(job: EventReminderJob, args: [@event.id, 7]) do
|
||||
EventReminderSchedulerJob.perform_now
|
||||
end
|
||||
end
|
||||
|
||||
test "schedules daily reminders for events starting tomorrow" do
|
||||
# Set event to start tomorrow
|
||||
@event.update(start_time: 1.day.from_now.beginning_of_day + 20.hours)
|
||||
|
||||
assert_enqueued_with(job: EventReminderJob, args: [@event.id, 1]) do
|
||||
EventReminderSchedulerJob.perform_now
|
||||
end
|
||||
end
|
||||
|
||||
test "schedules day-of reminders for events starting today" do
|
||||
# Set event to start today
|
||||
@event.update(start_time: Time.current.beginning_of_day + 21.hours)
|
||||
|
||||
assert_enqueued_with(job: EventReminderJob, args: [@event.id, 0]) do
|
||||
EventReminderSchedulerJob.perform_now
|
||||
end
|
||||
end
|
||||
|
||||
test "does not schedule reminders for draft events" do
|
||||
@event.update(state: :draft, start_time: 7.days.from_now.beginning_of_day + 10.hours)
|
||||
|
||||
assert_no_enqueued_jobs(only: EventReminderJob) do
|
||||
EventReminderSchedulerJob.perform_now
|
||||
end
|
||||
end
|
||||
|
||||
test "does not schedule reminders for cancelled events" do
|
||||
@event.update(state: :canceled, start_time: 7.days.from_now.beginning_of_day + 10.hours)
|
||||
|
||||
assert_no_enqueued_jobs(only: EventReminderJob) do
|
||||
EventReminderSchedulerJob.perform_now
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user