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

Don't send notifications about past SSRs when turning on info notifications #2783

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix Slack acknowledgment reminders by @vadimkerr ([#2769](https://github.com/grafana/oncall/pull/2769))
- Fix issue with updating "Require resolution note" setting by @Ferril ([#2782](https://github.com/grafana/oncall/pull/2782))
- Don't send notifications about past SSRs when turning on info notifications by @vadimkerr ([#2783](https://github.com/grafana/oncall/pull/2783))

## v1.3.23 (2023-08-10)

Expand Down
8 changes: 4 additions & 4 deletions engine/apps/mobile_app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,10 @@ def _should_notify_user_about_shift_swap_request(
except MobileAppUserSettings.DoesNotExist:
return False # don't notify if the app is not configured

return (
mobile_app_user_settings.info_notifications_enabled # info notifications must be enabled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand it ok, the idea is to flag the swap request as notified if a notification should have been sent while notifications are disabled, so when enabling we don't try to send it later, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the idea

and user.is_in_working_hours(now, mobile_app_user_settings.time_zone) # user must be in working hours
and not _has_user_been_notified_for_shift_swap_request(shift_swap_request, user) # don't notify twice
return user.is_in_working_hours( # user must be in working hours
now, mobile_app_user_settings.time_zone
) and not _has_user_been_notified_for_shift_swap_request( # don't notify twice
shift_swap_request, user
)


Expand Down
31 changes: 30 additions & 1 deletion engine/apps/mobile_app/tests/test_shift_swap_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,32 @@ def test_notify_user_about_shift_swap_request(make_organization, make_user, make
assert message.apns.payload.aps.sound.critical is False


@pytest.mark.django_db
def test_notify_user_about_shift_swap_request_info_notifications_disabled(
make_organization, make_user, make_schedule, make_shift_swap_request
):
organization = make_organization()
beneficiary = make_user(organization=organization)
benefactor = make_user(organization=organization)
schedule = make_schedule(organization, schedule_class=OnCallScheduleWeb)

FCMDevice.objects.create(user=benefactor, registration_id="test_device_id")
MobileAppUserSettings.objects.create(user=benefactor, info_notifications_enabled=False)

now = timezone.datetime(2023, 8, 1, 19, 38, tzinfo=timezone.utc)
vstpme marked this conversation as resolved.
Show resolved Hide resolved
swap_start = now + timezone.timedelta(days=100)
swap_end = swap_start + timezone.timedelta(days=1)

shift_swap_request = make_shift_swap_request(
schedule, beneficiary, swap_start=swap_start, swap_end=swap_end, created_at=now
)

with patch("apps.mobile_app.tasks._send_push_notification") as mock_send_push_notification:
notify_user_about_shift_swap_request(shift_swap_request.pk, benefactor.pk)

mock_send_push_notification.assert_not_called()


@pytest.mark.django_db
def test_should_notify_user(make_organization, make_user, make_schedule, make_shift_swap_request):
organization = make_organization()
Expand All @@ -288,8 +314,11 @@ def test_should_notify_user(make_organization, make_user, make_schedule, make_sh
assert not MobileAppUserSettings.objects.exists()
assert _should_notify_user_about_shift_swap_request(shift_swap_request, benefactor, now) is False

# check _should_notify_user_about_shift_swap_request is True when info notifications are disabled
mobile_app_settings = MobileAppUserSettings.objects.create(user=benefactor, info_notifications_enabled=False)
assert _should_notify_user_about_shift_swap_request(shift_swap_request, benefactor, now) is False
with patch.object(benefactor, "is_in_working_hours", return_value=True):
with patch("apps.mobile_app.tasks._has_user_been_notified_for_shift_swap_request", return_value=False):
assert _should_notify_user_about_shift_swap_request(shift_swap_request, benefactor, now) is True

mobile_app_settings.info_notifications_enabled = True
mobile_app_settings.save(update_fields=["info_notifications_enabled"])
Expand Down