BathHacked/energy-sparks

View on GitHub
app/services/targets/target_mailer_service.rb

Summary

Maintainability
A
1 hr
Test Coverage
module Targets
  class TargetMailerService
    def list_schools
      candidates = School.visible.data_enabled.reject {|s| s.has_target? || s.has_school_target_event?(:first_target_sent)}
      with_enough_data(candidates)
    end

    def list_schools_requiring_reminder
      candidates = School.visible.data_enabled.reject { |s| reject_for_reminder?(s) }
      with_enough_data(candidates)
    end

    def list_schools_requiring_review
      candidates = School.visible.data_enabled.reject {|s| !s.has_target? || s.has_current_target? || s.has_school_target_event?(:review_target_sent)}
      with_enough_data(candidates)
    end

    def invite_schools_to_set_first_target
      list_schools.each do |school|
        users = users(school)
        if users.any?
          TargetMailer.with_user_locales(users: users, school: school) { |mailer| mailer.first_target.deliver_now }
          school.school_target_events.create(event: :first_target_sent)
        end
      end
    end

    def remind_schools_to_set_first_target
      list_schools_requiring_reminder.each do |school|
        users = users(school)
        if users.any?
          TargetMailer.with_user_locales(users: users, school: school) { |mailer| mailer.first_target_reminder.deliver_now }
          school.school_target_events.create(event: :first_target_reminder_sent)
        end
      end
    end

    def invite_schools_to_review_target
      list_schools_requiring_review.each do |school|
        users = users(school)
        if users.any?
          TargetMailer.with_user_locales(users: users, school: school) { |mailer| mailer.review_target.deliver_now }
          school.school_target_events.create(event: :review_target_sent)
        end
      end
    end

    private

    def with_enough_data(schools)
      schools.select do |school|
        Targets::SchoolTargetService.targets_enabled?(school) && Targets::SchoolTargetService.new(school).enough_data?
      end
    end

    def users(school)
      school.all_adult_school_users.uniq
    end

    def reject_for_reminder?(school)
      return true if school.has_target? || school.has_school_target_event?(:first_target_reminder_sent)
      school.school_target_events.where(event: :first_target_sent).where('created_at <= ?', 30.days.ago).empty?
    end
  end
end