Skip to content

Commit

Permalink
Prevent the survey from being sent multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
lizconlan committed Dec 16, 2016
1 parent 2f9bad3 commit 82c6bd4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/model_patches.rb
Expand Up @@ -111,17 +111,24 @@ def alert_survey
" )"
).includes(:user)

# TODO: change the initial query to iterate over users rather
# than info_requests rather than using an array to check whether
# we're about to send multiple emails to the same user_id
sent_to = []
for info_request in info_requests
# Exclude users who have already completed the survey
# Exclude users who have already completed the survey or
# have already been sent a survey email in this run
logger.debug "[alert_survey] Considering #{info_request.user.url_name}"
next if info_request.user.survey.already_done?
next if info_request.user.survey.already_done? || sent_to.include?(info_request.user_id)

store_sent = UserInfoRequestSentAlert.new
store_sent.info_request = info_request
store_sent.user = info_request.user
store_sent.alert_type = 'survey_1'
store_sent.info_request_event_id = info_request.info_request_events[0].id

sent_to << info_request.user_id

RequestMailer.survey_alert(info_request).deliver
store_sent.save!
end
Expand Down
21 changes: 19 additions & 2 deletions spec/model_patches_spec.rb
Expand Up @@ -21,8 +21,12 @@
ActionMailer::Base.deliveries = []
end

def get_surveyable_request
info_request = FactoryGirl.create(:info_request)
def get_surveyable_request(user=nil)
info_request = if user
FactoryGirl.create(:info_request, :user => user)
else
FactoryGirl.create(:info_request)
end
info_request.created_at = Time.now - (2.weeks + 1.hour)
info_request.save!
info_request
Expand Down Expand Up @@ -79,6 +83,19 @@ def get_surveyable_request
expect(ActionMailer::Base.deliveries.size).to eq(0)
end
end

context 'when a user has made multiple qualifying requests' do

it 'does not send multiple alerts' do
allow_any_instance_of(User).to receive(:survey).
and_return(double('survey', :already_done? => false))
request = get_surveyable_request
get_surveyable_request(request.user)
RequestMailer.alert_new_response_reminders
expect(ActionMailer::Base.deliveries.size).to eq(1)
end
end

end

context 'when SEND_SURVEY_MAILS is not set' do
Expand Down

0 comments on commit 82c6bd4

Please sign in to comment.