Skip to content

Commit

Permalink
Fix contacts whose current_session_uuid doesn't match a waiting session
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Feb 26, 2025
1 parent 224bfdc commit df7b744
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 11 additions & 4 deletions temba/contacts/migrations/0205_create_session_expires_fires.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def create_session_expires_fires(apps, schema_editor):
ContactFire = apps.get_model("contacts", "ContactFire")
FlowSession = apps.get_model("flows", "FlowSession")

num_created = 0
num_created, num_skipped = 0, 0

while True:
# find contacts with waiting sessions that don't have a corresponding session expiration fire
Expand All @@ -24,7 +24,7 @@ def create_session_expires_fires(apps, schema_editor):
if not batch:
break

sessions = FlowSession.objects.filter(uuid__in=[c.current_session_uuid for c in batch]).only(
sessions = FlowSession.objects.filter(uuid__in=[c.current_session_uuid for c in batch], status="W").only(
"uuid", "created_on"
)
created_on_by_uuid = {s.uuid: s.created_on for s in sessions}
Expand All @@ -43,10 +43,17 @@ def create_session_expires_fires(apps, schema_editor):
session_uuid=contact.current_session_uuid,
)
)
else:
contact.current_session_uuid = None
contact.current_flow = None
contact.save(update_fields=("current_session_uuid", "current_flow"))

ContactFire.objects.bulk_create(to_create)
num_skipped += 1

if to_create:
ContactFire.objects.bulk_create(to_create)
num_created += len(to_create)
print(f"Created {num_created} session expiration fires")
print(f"Created {num_created} session expiration fires ({num_skipped} skipped)")


def apply_manual(): # pragma: no cover
Expand Down
16 changes: 16 additions & 0 deletions temba/contacts/tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ def create_contact_and_sessions(name, phone, current_session_uuid):
# contact with no waiting session
self.contact4 = self.create_contact("Dan", phone="+1234567004")

# contact with session mismatch
self.contact5 = self.create_contact(
"Dan", phone="+1234567004", current_session_uuid="ffca65c7-42ac-40cd-bef0-63aedc099ec9"
)
FlowSession.objects.create(
uuid="80466ed4-de5c-49e8-acad-2432b4e9cdf9",
contact=self.contact5,
status=FlowSession.STATUS_WAITING,
output_url="http://sessions.com/123.json",
created_on=timezone.now(),
)

def test_migration(self):
def assert_session_expire(contact):
self.assertTrue(contact.fires.exists())
Expand All @@ -70,3 +82,7 @@ def assert_session_expire(contact):
assert_session_expire(self.contact3)

self.assertFalse(self.contact4.fires.exists())
self.assertFalse(self.contact5.fires.exists())

self.contact5.refresh_from_db()
self.assertIsNone(self.contact5.current_session_uuid)

0 comments on commit df7b744

Please sign in to comment.