diff --git a/app/services/gpo_reminder_sender.rb b/app/services/gpo_reminder_sender.rb index 32dcf07a20d..867cbd77130 100644 --- a/app/services/gpo_reminder_sender.rb +++ b/app/services/gpo_reminder_sender.rb @@ -7,7 +7,12 @@ def send_emails(for_letters_sent_before) profiles_due_for_reminder(letter_eligible_range).each do |profile| profile.user.send_email_to_all_addresses(:gpo_reminder) - profile.gpo_confirmation_codes.first.update(reminder_sent_at: Time.zone.now) + profile.gpo_confirmation_codes.all.each do |gpo_code| + next if gpo_code.reminder_sent_at + next unless letter_eligible_range.cover?(gpo_code.created_at) + + gpo_code.update(reminder_sent_at: Time.zone.now) + end analytics.idv_gpo_reminder_email_sent(user_id: profile.user.uuid) end end diff --git a/spec/services/gpo_reminder_sender_spec.rb b/spec/services/gpo_reminder_sender_spec.rb index 4d8fbd89d28..4b8f866842d 100644 --- a/spec/services/gpo_reminder_sender_spec.rb +++ b/spec/services/gpo_reminder_sender_spec.rb @@ -54,11 +54,19 @@ def set_gpo_verification_pending_at(user, to_time) user. gpo_verification_pending_profile. update(gpo_verification_pending_at: to_time) + + user. + gpo_verification_pending_profile. + gpo_confirmation_codes.each do |code| + code.update(code_sent_at: to_time, created_at: to_time, updated_at: to_time) + end end def set_reminder_sent_at(to_time) gpo_confirmation_code.update( reminder_sent_at: to_time, + created_at: to_time, + updated_at: to_time, ) end @@ -72,17 +80,22 @@ def set_reminder_sent_at(to_time) context 'when a user has requested two letters' do before do - set_gpo_verification_pending_at(user, time_due_for_reminder - 2.days) - new_confirmation_code = create(:gpo_confirmation_code) + timestamp = time_due_for_reminder - 2.days + set_gpo_verification_pending_at(user, timestamp) + new_confirmation_code = create(:gpo_confirmation_code, created_at: timestamp) user.gpo_verification_pending_profile.gpo_confirmation_codes << new_confirmation_code end include_examples 'sends emails', expected_number_of_emails: 2 - it 'updates the GPO verification code `reminder_sent_at`' do + it 'updates the GPO verification code `reminder_sent_at` for both codes' do subject.send_emails(time_due_for_reminder) + user.gpo_verification_pending_profile.gpo_confirmation_codes.each(&:reload) - expect(gpo_confirmation_code.reminder_sent_at).to be_within(1.second).of(Time.zone.now) + expect(user.gpo_verification_pending_profile.gpo_confirmation_codes[0].reminder_sent_at). + to be_within(1.second).of(Time.zone.now) + expect(user.gpo_verification_pending_profile.gpo_confirmation_codes[1].reminder_sent_at). + to be_within(1.second).of(Time.zone.now) end end @@ -93,6 +106,7 @@ def set_reminder_sent_at(to_time) it 'updates the GPO verification code `reminder_sent_at`' do subject.send_emails(time_due_for_reminder) + gpo_confirmation_code.reload expect(gpo_confirmation_code.reminder_sent_at).to be_within(1.second).of(Time.zone.now) end @@ -106,6 +120,7 @@ def set_reminder_sent_at(to_time) it 'updates the GPO verification code `reminder_sent_at`' do subject.send_emails(time_due_for_reminder) + gpo_confirmation_code.reload expect(gpo_confirmation_code.reminder_sent_at).to be_within(1.second).of(Time.zone.now) end