YaleSTC/shifts

View on GitHub
lib/tasks/email_stats.rake

Summary

Maintainability
Test Coverage
namespace :email do
  def shift_email(department)
    shifts_to_email = Shift.in_department(department).active.between(1.day.ago.midnight, Time.now).stats_unsent
      
    # Mailer methods does not work with ActiveRecord::Relation, or array of 
    # Records, thus the need to convert to array of ids
    missed_shifts = shifts_to_email.missed.collect(&:id)
    late_shifts = shifts_to_email.late.collect(&:id)
    left_early_shifts = shifts_to_email.left_early.collect(&:id)
    unless shifts_to_email.empty?
      UserMailer.delay.email_stats(missed_shifts, late_shifts, left_early_shifts, department)    
    end
  
    for shift in shifts_to_email
      shift.stats_unsent = false  
      shift.save(validate: false)
    end

  end


  
  desc "Sends out an e-mail to the admin whenever a user misses, is late to, or leaves a shift early"

  task daily_stats: :environment do
    departments_that_want_admin_emailed = Department.all.select { |d| d.department_config.stats_mailer_address }
    for dept in departments_that_want_admin_emailed
      shift_email(dept)
    end
  end

end