adding next_attempt_at

pull/1/head
Jared Cosulich 9 years ago
parent b518846811
commit bec3f52adf

@ -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

@ -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

@ -0,0 +1,5 @@
class AddNextAttemptAtToRecipientSchedule < ActiveRecord::Migration[5.0]
def change
add_column :recipient_schedules, :next_attempt_at, :timestamp
end
end

@ -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|

@ -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

@ -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

@ -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

Loading…
Cancel
Save