From a2300a58b98765ed406f1e995f88f64e0ee8aebe Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Tue, 18 Apr 2017 12:00:24 -0400 Subject: [PATCH] moving weekend logic into the next_attempt_at logic in recipient_schedule --- app/models/recipient_schedule.rb | 8 +++++++- lib/tasks/survey.rake | 8 +++----- spec/lib/tasks/survey_rake_spec.rb | 1 + spec/models/recipient_schedule_spec.rb | 27 ++++++++++++++++++++------ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/app/models/recipient_schedule.rb b/app/models/recipient_schedule.rb index 7eb20419..4ad8fd1e 100644 --- a/app/models/recipient_schedule.rb +++ b/app/models/recipient_schedule.rb @@ -60,12 +60,18 @@ class RecipientSchedule < ApplicationRecord attempted_question_ids: attempted.empty? ? nil : attempted.join(','), queued_question_ids: queued.empty? ? nil : queued.join(','), last_attempt_at: (unanswered_attempt || attempt).sent_at, - next_attempt_at: next_attempt_at + (60 * 60 * schedule.frequency_hours) + next_attempt_at: next_valid_attempt_time ) end return (unanswered_attempt || attempt) end + def next_valid_attempt_time + local_time = (next_attempt_at + (60 * 60 * schedule.frequency_hours)).in_time_zone('Eastern Time (US & Canada)') + local_time += 1.day while local_time.on_weekend? + return local_time + end + def self.create_for_recipient(recipient_or_recipient_id, schedule, next_attempt_at=nil) if next_attempt_at.nil? next_attempt_at = Time.at(schedule.start_date.to_time.to_i + (60 * schedule.time)) diff --git a/lib/tasks/survey.rake b/lib/tasks/survey.rake index 469ad310..a966320b 100644 --- a/lib/tasks/survey.rake +++ b/lib/tasks/survey.rake @@ -2,11 +2,9 @@ namespace :survey do desc 'Text all Recipients ready for an Attempt (only on weekdays)' task :attempt_questions => :environment do - if Date.today.on_weekday? - Schedule.active.each do |schedule| - schedule.recipient_schedules.ready.each do |recipient_schedule| - recipient_schedule.attempt_question - end + Schedule.active.each do |schedule| + schedule.recipient_schedules.ready.each do |recipient_schedule| + recipient_schedule.attempt_question end end end diff --git a/spec/lib/tasks/survey_rake_spec.rb b/spec/lib/tasks/survey_rake_spec.rb index d579c835..7eae2c07 100644 --- a/spec/lib/tasks/survey_rake_spec.rb +++ b/spec/lib/tasks/survey_rake_spec.rb @@ -13,6 +13,7 @@ describe "survey:attempt_questions" do n += 1.day until n.on_weekday? return n } + let(:ready_recipient_schedule) { double('ready recipient schedule', attempt_question: nil) } let(:recipient_schedules) { double("recipient schedules", ready: [ready_recipient_schedule]) } let(:active_schedule) { double("active schedule", recipient_schedules: recipient_schedules) } diff --git a/spec/models/recipient_schedule_spec.rb b/spec/models/recipient_schedule_spec.rb index f838cfac..47c84cff 100644 --- a/spec/models/recipient_schedule_spec.rb +++ b/spec/models/recipient_schedule_spec.rb @@ -21,7 +21,7 @@ RSpec.describe RecipientSchedule, type: :model do recipient_list_id: recipient_list.id, question_list: question_list, random: false, - frequency_hours: 24 * 7 + frequency_hours: 24 ) end let!(:recipient_schedule) { schedule.recipient_schedules.for(recipient).first } @@ -32,8 +32,8 @@ RSpec.describe RecipientSchedule, type: :model do schedule: schedule, upcoming_question_ids: '1,3', attempted_question_ids: '2', - last_attempt_at: 1.day.ago, - next_attempt_at: 1.day.ago + (60 * 60 * schedule.frequency_hours) + last_attempt_at: Date.today + (60 * 60 * schedule.frequency_hours), + next_attempt_at: 1.day.from_now + (60 * 60 * schedule.frequency_hours) ) end @@ -73,7 +73,21 @@ RSpec.describe RecipientSchedule, type: :model do it 'should not do anything' do expect(attempt).to be_nil end + end + + describe 'right before a weekend' do + before :each do + friday_time = ActiveSupport::TimeZone["UTC"].parse('2017-04-21T20:00:00') + Timecop.freeze() + recipient_schedule.update_attributes(next_attempt_at: friday_time) + end + let!(:attempt) { recipient_schedule.attempt_question } + + it 'should schedule the next attempt for after the weekend' do + next_weekday_time = ActiveSupport::TimeZone["UTC"].parse('2017-04-24T20:00:00') + expect(recipient_schedule.reload.next_attempt_at).to eq(next_weekday_time) + end end describe 'with an opted in recipient' do @@ -103,10 +117,11 @@ RSpec.describe RecipientSchedule, type: :model do it 'should update next_attempt_at' do now = DateTime.now - date = ActiveSupport::TimeZone["UTC"].parse(now.strftime("%Y-%m-%dT16:00:00%z")) - time = date.to_time.to_i + (60 * 60 * 24 * 7) + date = ActiveSupport::TimeZone["Eastern Time (US & Canada)"].parse(now.strftime("%Y-%m-%dT16:00:00%z")) + date += 1.day + date += 1.day if date.on_weekend? - expect(recipient_schedule.next_attempt_at.to_i).to eq(time) + expect(recipient_schedule.next_attempt_at).to eq(date.to_time) end end end