diff --git a/app/models/recipient_schedule.rb b/app/models/recipient_schedule.rb index a4093c7e..a307f709 100644 --- a/app/models/recipient_schedule.rb +++ b/app/models/recipient_schedule.rb @@ -4,6 +4,8 @@ class RecipientSchedule < ApplicationRecord belongs_to :schedule has_many :attempts + scope :ready, -> { where('next_attempt_at <= ?', Time.new) } + def next_question upcoming = upcoming_question_ids.split(/,/) Question.where(id: upcoming.first).first @@ -22,7 +24,8 @@ class RecipientSchedule < ApplicationRecord update_attributes( upcoming_question_ids: upcoming, attempted_question_ids: attempted, - last_attempt_at: attempt.sent_at + last_attempt_at: attempt.sent_at, + next_attempt_at: attempt.sent_at + (60 * 60 * schedule.frequency_hours) ) end return attempt diff --git a/app/models/schedule.rb b/app/models/schedule.rb index ca68c3d3..6053e66e 100644 --- a/app/models/schedule.rb +++ b/app/models/schedule.rb @@ -2,6 +2,7 @@ class Schedule < ApplicationRecord belongs_to :school belongs_to :recipient_list belongs_to :question_list + has_many :recipient_schedules validates :name, presence: true validates :recipient_list, presence: true diff --git a/db/migrate/20170308160911_add_next_attempt_at_to_recipient_schedule.rb b/db/migrate/20170308160911_add_next_attempt_at_to_recipient_schedule.rb new file mode 100644 index 00000000..cd6c9deb --- /dev/null +++ b/db/migrate/20170308160911_add_next_attempt_at_to_recipient_schedule.rb @@ -0,0 +1,5 @@ +class AddNextAttemptAtToRecipientSchedule < ActiveRecord::Migration[5.0] + def change + add_column :recipient_schedules, :next_attempt_at, :timestamp + end +end diff --git a/db/schema.rb b/db/schema.rb index 39ec57cb..1694c071 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170307153205) do +ActiveRecord::Schema.define(version: 20170308160911) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -84,6 +84,7 @@ ActiveRecord::Schema.define(version: 20170307153205) do t.datetime "last_attempt_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "next_attempt_at" end create_table "recipients", force: :cascade do |t| diff --git a/lib/tasks/survey.rake b/lib/tasks/survey.rake index 1e8d668b..83faa3e6 100644 --- a/lib/tasks/survey.rake +++ b/lib/tasks/survey.rake @@ -2,9 +2,9 @@ namespace :survey do desc 'Text all Recipients ready for an Attempt' task :make_attempts => :environment do - Schedule.active.all.each do |schedule| + Schedule.active.each do |schedule| schedule.recipient_schedules.ready.each do |recipient_schedule| - + # recipient_schedule. end end end diff --git a/spec/lib/tasks/survey_rake_spec.rb b/spec/lib/tasks/survey_rake_spec.rb index 146efadc..2c009ce5 100644 --- a/spec/lib/tasks/survey_rake_spec.rb +++ b/spec/lib/tasks/survey_rake_spec.rb @@ -3,11 +3,9 @@ require 'rails_helper' describe "survey:make_attempts" do include_context "rake" - - # let(:old_schedule) { stub("csv data") } - # let(:paused_schedule) { stub("csv data") } - # let(:report) { stub("generated report", :to_csv => csv) } - # let(:user_records) { stub("user records for report") } + let(:ready_recipient_schedules) { double('ready recipient schedules') } + let(:recipient_schedules) { double("recipient schedules", ready: []) } + let(:active_schedule) { double("active schedule", recipient_schedules: recipient_schedules) } before do # ReportGenerator.stubs(:generate) @@ -20,6 +18,8 @@ describe "survey:make_attempts" do end it "finds all active schedules" do + expect(active_schedule).to (receive(:recipient_schedules)) + expect(Schedule).to receive(:active).and_return([active_schedule]) subject.invoke end end diff --git a/spec/models/recipient_schedule_spec.rb b/spec/models/recipient_schedule_spec.rb index 18a5ee41..5c3e2c91 100644 --- a/spec/models/recipient_schedule_spec.rb +++ b/spec/models/recipient_schedule_spec.rb @@ -8,7 +8,14 @@ RSpec.describe RecipientSchedule, type: :model do let(:recipient) { Recipient.create!(name: 'Parent', phone: '1112223333') } let(:recipient_list) { RecipientList.create(name: 'Parent List', recipient_ids: recipient.id.to_s)} - let(:schedule) { Schedule.create!(name: 'Parent Schedule', recipient_list_id: recipient_list.id, question_list: question_list) } + let(:schedule) do + Schedule.create!( + name: 'Parent Schedule', + recipient_list_id: recipient_list.id, + question_list: question_list, + frequency_hours: 24 * 7 + ) + end let!(:recipient_schedule) do RecipientSchedule.create!( @@ -16,10 +23,31 @@ RSpec.describe RecipientSchedule, type: :model do schedule: schedule, upcoming_question_ids: "#{question.id},3", attempted_question_ids: '2', - last_attempt_at: 2.weeks.ago + last_attempt_at: 2.weeks.ago, + next_attempt_at: 2.weeks.ago + (60 * 60 * schedule.frequency_hours) ) end + let!(:not_ready_recipient_schedule) do + RecipientSchedule.create!( + recipient: recipient, + schedule: schedule, + upcoming_question_ids: "#{question.id},3", + attempted_question_ids: '2', + last_attempt_at: 1.day.ago, + next_attempt_at: 1.day.ago + (60 * 60 * schedule.frequency_hours) + ) + end + + describe 'ready' do + subject { schedule.recipient_schedules.ready } + + it ('should only provide recipient_schedules who are ready to send a message') do + expect(subject.length).to eq(1) + expect(subject.first).to eq(recipient_schedule) + end + end + describe 'next_question' do it 'should provide the next question from the upcoming_question_ids list' do expect(recipient_schedule.next_question).to eq(question) @@ -55,5 +83,8 @@ RSpec.describe RecipientSchedule, type: :model do expect(recipient_schedule.last_attempt_at.to_i).to eq(Time.new.to_i) end + it 'should update next_attempt_at' do + expect(recipient_schedule.next_attempt_at.to_i).to eq((Time.new + (60 * 60 * schedule.frequency_hours)).to_i) + end end end