From aaab2b0b4f22e122a3b15689982223190423b7f0 Mon Sep 17 00:00:00 2001 From: Rae Stanton Date: Fri, 11 Aug 2023 12:09:16 -0700 Subject: [PATCH] WIP part 2 Now I see headers for the CSV, but no data for the hours report is being rendered to the CSV. --- app/mailers/learning_hours_mailer.rb | 24 ++++++++++++++++--- .../previews/learning_hours_mailer_preview.rb | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/app/mailers/learning_hours_mailer.rb b/app/mailers/learning_hours_mailer.rb index 0e0e1d9159..a67879d803 100644 --- a/app/mailers/learning_hours_mailer.rb +++ b/app/mailers/learning_hours_mailer.rb @@ -1,11 +1,29 @@ class LearningHoursMailer < ApplicationMailer - def learning_hours_report_email(user, casa_org, csv_data) + + def learning_hours_report_email(user) @user = user - @casa_org = casa_org - attachments['learning_hours_report.csv'] = csv_data + + # Ensure user is provided + if @user.nil? + Bugsnag.notify("No user provided for LearningHoursMailer#learning_hours_report_email") + return + end + + @casa_org = user.casa_org + + # Generate the learning hours CSV for the current month + start_date = Date.today.beginning_of_month + end_date = Date.today.end_of_month + learning_hours = LearningHour.where(user_id: @user.id, occurred_at: start_date..end_date) + csv_data = LearningHoursExportCsvService.new(learning_hours).perform + + attachments["learning-hours-report-#{Date.today}.csv"] = csv_data + mail(to: @user.email, subject: "Learning Hours Report - #{last_day_of_month.strftime('%Y-%m-%d')}") end + private + def last_day_of_month Date.today.end_of_month end diff --git a/lib/mailers/previews/learning_hours_mailer_preview.rb b/lib/mailers/previews/learning_hours_mailer_preview.rb index d36b7daff0..70114c7f4b 100644 --- a/lib/mailers/previews/learning_hours_mailer_preview.rb +++ b/lib/mailers/previews/learning_hours_mailer_preview.rb @@ -6,7 +6,7 @@ def learning_hours_report_email if user.nil? DebugPreviewMailer.invalid_user("user") else - LearningHoursMailer.learning_hours_report_email(user, @casa_org, @csv_data) + LearningHoursMailer.learning_hours_report_email(user) end end end