# What this PR does
Address issue where if the user had multiple registered devices w/ FCM,
doing django queries like `.first()` could potentially pick the wrong
device. Do this in two ways:
1. set the `DELETE_INACTIVE_DEVICES` `fcm_django` setting to `True`.
According to the
[docs](20e275618b/README.rst (L127-L130)),
this works as follows:
> devices to which notifications cannot be sent, are deleted upon
receiving error response from FCM
2. Customizing the `FCMDevice` model provided by `fcm_django`. Add a new
method, `get_active_device_for_user`, so that we can centralize the
logic for this rather than duplicating
`FCMDevice.objects.filter(user=user).first()`
## Which issue(s) this PR fixes
https://raintank-corp.slack.com/archives/C0229FD3CE9/p1688461915752119
## Checklist
- [x] Unit, integration, and e2e (if applicable) tests updated
- [ ] Documentation added (or `pr:no public docs` PR label added if not
required) (N/A)
- [x] `CHANGELOG.md` updated (or `pr:no changelog` PR label added if not
required)
13 lines
528 B
Python
13 lines
528 B
Python
import pytest
|
|
|
|
from apps.mobile_app.models import FCMDevice
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_get_active_device_for_user_works(make_organization_and_user):
|
|
_, user = make_organization_and_user()
|
|
FCMDevice.objects.create(user=user, registration_id="inactive_device_id", active=False)
|
|
active_device = FCMDevice.objects.create(user=user, registration_id="active_device_id", active=True)
|
|
|
|
assert FCMDevice.objects.filter(user=user).count() == 2
|
|
assert FCMDevice.get_active_device_for_user(user) == active_device
|