diff --git a/app/jobs/send_invitation_reminders_job.rb b/app/jobs/send_invitation_reminders_job.rb index 9ddc5afde..f38b3b6e4 100644 --- a/app/jobs/send_invitation_reminders_job.rb +++ b/app/jobs/send_invitation_reminders_job.rb @@ -31,13 +31,16 @@ def follow_ups_with_reminder_needed def valid_invitations_sent_3_days_ago @valid_invitations_sent_3_days_ago ||= - # we want the invitation to be valid for at least two days to give time to the user to accept the invitation - Invitation.where("expires_at > ?", 2.days.from_now) + # We want the invitation to be valid for at least two days to give time to the user to accept the invitation. + # We don't send reminders for invitations that never expire since we consider those as invitations + # to optional rdvs. + # We only send reminders for invitations that have been triggered manually and not by the system. + Invitation.manual + .where("expires_at > ?", 2.days.from_now) .where( format: %w[email sms], created_at: Invitation::NUMBER_OF_DAYS_BEFORE_REMINDER.days.ago.all_day ) - .not_reminder end def invitation_sent_3_days_ago?(invitation) diff --git a/app/jobs/send_periodic_invites_job.rb b/app/jobs/send_periodic_invites_job.rb index 0f4240b91..c50e491ee 100644 --- a/app/jobs/send_periodic_invites_job.rb +++ b/app/jobs/send_periodic_invites_job.rb @@ -32,7 +32,9 @@ def send_invite(follow_up) def should_send_periodic_invite?(last_invitation, category_configuration) if category_configuration.day_of_the_month_periodic_invites.present? - Time.zone.today.day == category_configuration.day_of_the_month_periodic_invites + Time.zone.today.day == category_configuration.day_of_the_month_periodic_invites && + # We don't send an invite the first month if the invitation was sent less than 10 days ago + last_invitation.sent_before?(10.days.ago) elsif category_configuration.number_of_days_between_periodic_invites.present? (Time.zone.today - last_invitation.created_at.to_date).to_i == category_configuration.number_of_days_between_periodic_invites diff --git a/spec/jobs/send_periodic_invites_job_spec.rb b/spec/jobs/send_periodic_invites_job_spec.rb index 11d628828..bef52b9aa 100644 --- a/spec/jobs/send_periodic_invites_job_spec.rb +++ b/spec/jobs/send_periodic_invites_job_spec.rb @@ -94,6 +94,24 @@ subject end end + + context "when the last invitation was sent less than 10 days ago" do + let!(:invitation) do + create(:invitation, + follow_up: follow_up, + created_at: 9.days.ago, + expires_at: nil, + organisations: [organisation]) + end + + it "does not send periodic invites" do + expect(SendPeriodicInviteJob).not_to receive(:perform_later).with(invitation.id, category_configuration.id, + "email") + expect(SendPeriodicInviteJob).not_to receive(:perform_later).with(invitation.id, category_configuration.id, + "sms") + subject + end + end end context "when no invitations have been sent" do