Skip to content

Commit

Permalink
Merge pull request #5905 from nyaruka/session_and_fire_tweaks
Browse files Browse the repository at this point in the history
Add new 'session expire' type to contact fires and re-add expired status to sessions
  • Loading branch information
rowanseymour authored Feb 25, 2025
2 parents 1deaad8 + 039df61 commit 029fff7
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 10 deletions.
26 changes: 26 additions & 0 deletions temba/contacts/migrations/0204_alter_contactfire_fire_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.1.4 on 2025-02-25 16:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("contacts", "0203_remove_contactfire_extra"),
]

operations = [
migrations.AlterField(
model_name="contactfire",
name="fire_type",
field=models.CharField(
choices=[
("E", "Wait Expiration"),
("T", "Wait Timeout"),
("S", "Session Expiration"),
("C", "Campaign Event"),
],
max_length=1,
),
),
]
12 changes: 9 additions & 3 deletions temba/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def get_scheduled(self) -> list:
"""
from temba.campaigns.models import CampaignEvent

fires = self.fires.filter(fire_type=ContactFire.TYPE_CAMPAIGN)
fires = self.fires.filter(fire_type=ContactFire.TYPE_CAMPAIGN_EVENT)
event_ids = {int(f.scope) for f in fires}
events = CampaignEvent.objects.filter(
campaign__org=self.org, campaign__is_archived=False, id__in=event_ids, is_active=True
Expand Down Expand Up @@ -1717,8 +1717,14 @@ class ContactFire(models.Model):

TYPE_WAIT_EXPIRATION = "E"
TYPE_WAIT_TIMEOUT = "T"
TYPE_CAMPAIGN = "C"
TYPE_CHOICES = ((TYPE_WAIT_EXPIRATION, "Expiration"), (TYPE_WAIT_TIMEOUT, "Timeout"), (TYPE_CAMPAIGN, "Campaign"))
TYPE_SESSION_EXPIRATION = "S"
TYPE_CAMPAIGN_EVENT = "C"
TYPE_CHOICES = (
(TYPE_WAIT_EXPIRATION, "Wait Expiration"),
(TYPE_WAIT_TIMEOUT, "Wait Timeout"),
(TYPE_SESSION_EXPIRATION, "Session Expiration"),
(TYPE_CAMPAIGN_EVENT, "Campaign Event"),
)

id = models.BigAutoField(auto_created=True, primary_key=True)
org = models.ForeignKey(Org, on_delete=models.PROTECT, db_index=False)
Expand Down
4 changes: 2 additions & 2 deletions temba/contacts/tests/test_contactcrudl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,14 +1114,14 @@ def test_scheduled(self):
fire1 = ContactFire.objects.create(
org=self.org,
contact=contact1,
fire_type=ContactFire.TYPE_CAMPAIGN,
fire_type=ContactFire.TYPE_CAMPAIGN_EVENT,
scope=str(event1.id),
fire_on=timezone.now() + timedelta(days=2),
)
fire2 = ContactFire.objects.create(
org=self.org,
contact=contact1,
fire_type=ContactFire.TYPE_CAMPAIGN,
fire_type=ContactFire.TYPE_CAMPAIGN_EVENT,
scope=str(event2.id),
fire_on=timezone.now() + timedelta(days=5),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.1.4 on 2025-02-25 16:49

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("flows", "0377_remove_flowsession_flowsessions_contact_waiting"),
]

operations = [
migrations.AlterField(
model_name="flowrun",
name="session",
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, to="flows.flowsession"),
),
migrations.AlterField(
model_name="flowsession",
name="status",
field=models.CharField(
choices=[("W", "Waiting"), ("C", "Completed"), ("I", "Interrupted"), ("X", "Expired"), ("F", "Failed")],
max_length=1,
),
),
]
10 changes: 6 additions & 4 deletions temba/flows/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,11 +1004,13 @@ class FlowSession(models.Model):
STATUS_WAITING = "W"
STATUS_COMPLETED = "C"
STATUS_INTERRUPTED = "I"
STATUS_EXPIRED = "X"
STATUS_FAILED = "F"
STATUS_CHOICES = (
(STATUS_WAITING, "Waiting"),
(STATUS_COMPLETED, "Completed"),
(STATUS_INTERRUPTED, "Interrupted"),
(STATUS_EXPIRED, "Expired"),
(STATUS_FAILED, "Failed"),
)

Expand Down Expand Up @@ -1108,14 +1110,11 @@ class FlowRun(models.Model):
org = models.ForeignKey(Org, on_delete=models.PROTECT, related_name="runs", db_index=False)
flow = models.ForeignKey(Flow, on_delete=models.PROTECT, related_name="runs")
status = models.CharField(max_length=1, choices=STATUS_CHOICES)
session_uuid = models.UUIDField(null=True)

# contact isn't an index because we have flows_flowrun_contact_inc_flow below
contact = models.ForeignKey(Contact, on_delete=models.PROTECT, related_name="runs", db_index=False)

# session this run belongs to (can be null if session has been trimmed)
session = models.ForeignKey(FlowSession, on_delete=models.PROTECT, related_name="runs", null=True)
session_uuid = models.UUIDField(null=True) # to replace session_id above

# when this run was created, last modified and exited
created_on = models.DateTimeField(default=timezone.now)
modified_on = models.DateTimeField(default=timezone.now)
Expand All @@ -1138,6 +1137,9 @@ class FlowRun(models.Model):
# current node location of this run in the flow
current_node_uuid = models.UUIDField(null=True)

# TODO drop
session = models.ForeignKey(FlowSession, on_delete=models.PROTECT, null=True)

@dataclass
class Step:
node: UUID
Expand Down
2 changes: 1 addition & 1 deletion temba/flows/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_trim(self):

# create an IVR call with session
call = self.create_incoming_call(flow, contact)
run4 = call.session.runs.get()
run4 = FlowRun.objects.get(session_uuid=call.session_uuid)

self.assertIsNotNone(run1.session)
self.assertIsNotNone(run2.session)
Expand Down
4 changes: 4 additions & 0 deletions temba/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ def create_incoming_call(
contact=contact,
status=FlowRun.STATUS_COMPLETED,
session=session,
session_uuid=session.uuid,
exited_on=timezone.now(),
)
Msg.objects.create(
Expand All @@ -599,6 +600,9 @@ def create_incoming_call(
modified_on=timezone.now(),
)

call.session_uuid = session.uuid
call.save(update_fields=("session_uuid",))

return call

def create_archive(
Expand Down

0 comments on commit 029fff7

Please sign in to comment.