Skip to content

Commit

Permalink
Fixes #104, only send out an email reminder if someone's entered in e…
Browse files Browse the repository at this point in the history
…stimates for a given week and has NO actuals entered.
  • Loading branch information
fermion committed Jun 22, 2013
1 parent cc5dd89 commit c5e27c4
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/tasks/scheduler.rake
Expand Up @@ -4,22 +4,24 @@ desc "At the beginning of each week, this script will send emails to forgetful p
task :send_reminders => :environment do
if Time.now.to_date.wday == 1 # Only do this on Mondays
year, week = *[:year, :cweek].map {|msg| Date.current.send msg}

# Timestamp at begining of past week
@timestamp = Date.commercial(year, week).to_datetime.to_i * 1000 - 7 * 86400 * 1000
@users = Assignment.
joins(:work_weeks).
select("user_id").
where("work_weeks.beginning_of_week = ?", @timestamp - 7 * 86400 * 1000).
where("actual_hours IS NULL OR actual_hours = ?", 0).
where("estimated_hours IS NOT NULL AND estimated_hours > ?", 0).
map { |assignment| User.where(:id => assignment.user_id).first }.
uniq

@users.each do |user|
@timestamp = Date.commercial(year, week).to_datetime.to_i * 1000 - 7 * 86400 * 1000

work_weeks_by_user = WorkWeek.where(beginning_of_week: @timestamp).all.group_by { |ww| ww.user }

work_weeks_by_user.each do |user, work_weeks|
next if user.nil?

# do they care?
if user.user_preferences.try(:email_reminder)
user.send_email_reminder
# do they need a reminder? work weeks with estimates and NO actuals entered for any assignment
# we'll assume that a single work week with actuals is a sign that they've entered their data for the week.
has_work_weeks_with_estimates = work_weeks.any? { |ww| (ww.estimated_hours || 0) > 0 }
has_no_work_work_weeks_with_actuals = work_weeks.none? { |ww| (ww.actual_hours || 0) > 0 }

user.send_email_reminder if has_no_work_work_weeks_with_actuals && has_work_weeks_with_estimates
end
end
end
end

0 comments on commit c5e27c4

Please sign in to comment.