Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(invitations): Do not send periodic invites in some cases #2493

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/jobs/send_invitation_reminders_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion app/jobs/send_periodic_invites_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/jobs/send_periodic_invites_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down