Skip to content

Commit

Permalink
Add optin as field to channelevents
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Oct 4, 2023
1 parent 8ceac8b commit b47e99f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
21 changes: 21 additions & 0 deletions temba/channels/migrations/0177_channelevent_optin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.3 on 2023-10-04 15:54

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


class Migration(migrations.Migration):
dependencies = [
("msgs", "0250_alter_msg_msg_type"),
("channels", "0176_remove_channel_alert_email_delete_alert"),
]

operations = [
migrations.AddField(
model_name="channelevent",
name="optin",
field=models.ForeignKey(
null=True, on_delete=django.db.models.deletion.PROTECT, related_name="optins", to="msgs.optin"
),
),
]
1 change: 1 addition & 0 deletions temba/channels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ class ChannelEvent(models.Model):
contact_urn = models.ForeignKey(
"contacts.ContactURN", on_delete=models.PROTECT, null=True, related_name="channel_events"
)
optin = models.ForeignKey("msgs.OptIn", null=True, on_delete=models.PROTECT, related_name="optins")
extra = JSONAsTextField(null=True, default=dict)
occurred_on = models.DateTimeField()
created_on = models.DateTimeField(default=timezone.now)
Expand Down
4 changes: 2 additions & 2 deletions temba/contacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ def get_history(self, after: datetime, before: datetime, include_event_types: se
self.msgs.filter(created_on__gte=after, created_on__lt=before)
.exclude(status=Msg.STATUS_PENDING)
.order_by("-created_on", "-id")
.select_related("channel", "contact_urn", "broadcast")[:limit]
.select_related("channel", "contact_urn", "broadcast", "optin")[:limit]
)

# get all runs start started or ended in this period
Expand All @@ -797,7 +797,7 @@ def get_history(self, after: datetime, before: datetime, include_event_types: se
channel_events = (
self.channel_events.filter(created_on__gte=after, created_on__lt=before)
.order_by("-created_on")
.select_related("channel")[:limit]
.select_related("channel", "optin")[:limit]
)

campaign_events = (
Expand Down
2 changes: 1 addition & 1 deletion temba/mailroom/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def from_channel_event(cls, org: Org, user: User, obj: ChannelEvent) -> dict:
if obj.event_type in ChannelEvent.CALL_TYPES:
ch_event["duration"] = extra.get("duration")
elif obj.event_type in (ChannelEvent.TYPE_OPTIN, ChannelEvent.TYPE_OPTOUT):
ch_event["optin"] = _optin(OptIn.objects.get(org=org, id=extra.get("optin_id")))
ch_event["optin"] = _optin(obj.optin) if obj.optin else None

return {
"type": cls.TYPE_CHANNEL_EVENT,
Expand Down

0 comments on commit b47e99f

Please sign in to comment.