diff --git a/app/models/attempt.rb b/app/models/attempt.rb index 7f80d578..9f1ae468 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -20,9 +20,17 @@ class Attempt < ApplicationRecord scope :with_no_response, -> { where('answer_index is null and open_response_id is null')} def messages + if student.present? + intro = "#{student.name}'s school, " + else + intro = "Your child's school, " + end + + intro += "#{recipient.school.name}, would love your opinion on this question:" + [ #question.text, - "#{question.text}\r\n#{question.option1}: Reply 1\n\r#{question.option2}: Reply 2\n\r#{question.option3}: Reply 3\n\r#{question.option4}: Reply 4\n\r#{question.option5}: Reply 5\n\rReply 'stop' to stop these messages." + "#{intro}\n\r#{question.text}\n\r#{question.option1}: Reply 1\n\r#{question.option2}: Reply 2\n\r#{question.option3}: Reply 3\n\r#{question.option4}: Reply 4\n\r#{question.option5}: Reply 5\n\rReply 'stop' to stop these messages." ] end diff --git a/spec/lib/tasks/survey_rake_spec.rb b/spec/lib/tasks/survey_rake_spec.rb index 4ac40645..733b12ab 100644 --- a/spec/lib/tasks/survey_rake_spec.rb +++ b/spec/lib/tasks/survey_rake_spec.rb @@ -202,7 +202,7 @@ describe "survey:attempt_questions" do let(:students_recipient) { recipients[1] } let(:students_recipient_schedule) { - recipient_schedule = students_recipient.recipient_schedules.for_schedule(schedule).first + students_recipient.recipient_schedules.for_schedule(schedule).first } describe 'With A FOR_CHILD Question Is Asked' do @@ -232,18 +232,7 @@ describe "survey:attempt_questions" do expect(students_recipient_schedule.reload.next_attempt_at).to eq(Time.new) end - it 'should set the next_attempt_at to now when attempt is made on second student' do - students_recipient.attempts.last.save_response(answer_index: 3) - expect{students_recipient_schedule.attempt_question}.to change{students_recipient.attempts.count}.by(1) - expect(students_recipient_schedule.reload.queued_question_ids).to be_present - - attempt = students_recipient.attempts.last - expect(attempt.student).to eq(students_recipient.students[1]) - attempt.save_response(answer_index: 4) - expect(students_recipient_schedule.reload.next_attempt_at).to eq(Time.new) - end - - it 'should set the next_attempt_at in the future when an attempt is made on last student' do + it 'should set the next_attempt_at in the future when an attempts are made on each student' do students_recipient.attempts.last.save_response(answer_index: 3) expect{students_recipient_schedule.attempt_question}.to change{students_recipient.attempts.count}.by(1) expect(students_recipient_schedule.reload.queued_question_ids).to be_present @@ -267,15 +256,27 @@ describe "survey:attempt_questions" do expect(students_recipient_schedule.reload.next_attempt_at).to_not eq(date + 2.days) end - it 'should mention the students name in the text' + it 'should mention the students name in the text' do + expect(FakeSMS.messages[1].body).to match(/Student0's school, School, would love your opinion on this question/) + end - it 'resends the question about the same student if not responded to' + it 'should not mention the students name in the text if the recipient has no student specified' do + expect(FakeSMS.messages[0].body).to match(/Your child's school, School, would love your opinion on this question/) + end - it 'still sends when no students are present' + it 'resends the question about the same student if not responded to' do + message_count = FakeSMS.messages.length + expect{students_recipient_schedule.attempt_question}.to change{students_recipient.attempts.count}.by(0) + expect(FakeSMS.messages.length).to eq(message_count + 1) + expect(FakeSMS.messages.last.body).to match(/Student0's school, School/) + expect(FakeSMS.messages.last.body).to match(questions.first.text) + end - it 'doesnt store any queued_question_ids when no students are present' + it 'doesnt store any queued_question_ids when no students are present' do + recipient_schedule = recipients[0].recipient_schedules.for_schedule(schedule).first + expect(recipient_schedule.queued_question_ids).to be_nil + end - it 'doesnt store any queued_question_ids when just one student is present' end describe 'With A General Question Is Asked' do @@ -283,10 +284,48 @@ describe "survey:attempt_questions" do subject.invoke end - it 'should not queue up an questions regardless of how many students there are' + it 'should not queue up an questions regardless of how many students there are' do + expect(students_recipient_schedule.queued_question_ids).to be_nil + end + + it 'should not mention the students name in the text' do + FakeSMS.messages.each do |message| + expect(message.body).to match(/Your child's school, School, would love your opinion on this question/) + end + end + + end + end + + describe 'One Student In A Family' do - it 'should not mention the students name in the text' + before :each do + recipients[1].students.create(name: "Only Student") + end + + let(:students_recipient) { recipients[1] } + let(:students_recipient_schedule) { + students_recipient.recipient_schedules.for_schedule(schedule).first + } + + describe 'With A FOR_CHILD Question Is Asked' do + let!(:date) { ActiveSupport::TimeZone["UTC"].parse(now.strftime("%Y-%m-%dT20:00:00%z")) } + + before :each do + questions.first.update_attributes(for_recipient_students: true) + Timecop.freeze(date) { subject.invoke } + end + it 'should create one attempt per recipient regardless of students' do + expect(FakeSMS.messages.length).to eq(3) + recipients.each do |recipient| + expect(recipient.attempts.count).to eq(1) + end + end + + it 'doesnt store any queued_question_ids' do + expect(students_recipient_schedule.queued_question_ids).to be_nil + end end end diff --git a/spec/models/attempt_spec.rb b/spec/models/attempt_spec.rb index 86b04a40..bfd2474f 100644 --- a/spec/models/attempt_spec.rb +++ b/spec/models/attempt_spec.rb @@ -98,7 +98,7 @@ RSpec.describe Attempt, type: :model do # expect(FakeSMS.messages.first.body).to eq("Question 0:1") expect(FakeSMS.messages.last.to).to eq('111111111') - expect(FakeSMS.messages.last.body).to eq("Question 0:1\r\nOption 0:1 A: Reply 1\n\rOption 0:1 B: Reply 2\n\rOption 0:1 C: Reply 3\n\rOption 0:1 D: Reply 4\n\rOption 0:1 E: Reply 5\n\rReply 'stop' to stop these messages.") + expect(FakeSMS.messages.last.body).to eq("Your child's school, School, would love your opinion on this question:\n\rQuestion 0:1\n\rOption 0:1 A: Reply 1\n\rOption 0:1 B: Reply 2\n\rOption 0:1 C: Reply 3\n\rOption 0:1 D: Reply 4\n\rOption 0:1 E: Reply 5\n\rReply 'stop' to stop these messages.") end it 'should update sent_at' do