Skip to content

Commit 8eb4e43

Browse files
authored
Merge pull request #607 from praekeltfoundation/capi-backend-changes
Add process whatsapp template send status task
2 parents 555f628 + dc8c7eb commit 8eb4e43

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 4.2.13 on 2024-07-17 13:09
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
(
10+
"eventstore",
11+
"0067_rename_registration_completed_at_whatsapptemplatesendstatus_action_completed_at_and_more",
12+
),
13+
]
14+
15+
operations = [
16+
migrations.AddField(
17+
model_name="whatsapptemplatesendstatus",
18+
name="contact_uuid",
19+
field=models.UUIDField(blank=True, null=True),
20+
),
21+
migrations.AddField(
22+
model_name="whatsapptemplatesendstatus",
23+
name="data",
24+
field=models.JSONField(blank=True, default=dict, null=True),
25+
),
26+
migrations.AddField(
27+
model_name="whatsapptemplatesendstatus",
28+
name="flow_uuid",
29+
field=models.UUIDField(blank=True, null=True),
30+
),
31+
]

eventstore/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,3 +1148,6 @@ class Status:
11481148
status = models.CharField(
11491149
max_length=30, choices=Status.choices, default=Status.WIRED
11501150
)
1151+
contact_uuid = models.UUIDField(null=True, blank=True)
1152+
flow_uuid = models.UUIDField(null=True, blank=True)
1153+
data = models.JSONField(default=dict, blank=True, null=True)

eventstore/tasks.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,3 +842,27 @@ def update_whatsapp_template_send_status(message_id, preferred_channel=None):
842842
status.save()
843843
except WhatsAppTemplateSendStatus.DoesNotExist:
844844
pass
845+
846+
847+
@app.task(acks_late=True, soft_time_limit=10, time_limit=15)
848+
def process_whatsapp_template_send_status():
849+
filter_date = timezone.now() - timedelta(
850+
hours=settings.WHATSAPP_TEMPLATE_SEND_TIMEOUT_HOURS
851+
)
852+
ready_statuses = WhatsAppTemplateSendStatus.objects.filter(
853+
flow_uuid__isnull=False, status=WhatsAppTemplateSendStatus.Status.EVENT_RECEIVED
854+
)
855+
expired_statuses = WhatsAppTemplateSendStatus.objects.filter(
856+
flow_uuid__isnull=False,
857+
status=WhatsAppTemplateSendStatus.Status.WIRED,
858+
sent_at__lt=filter_date,
859+
)
860+
861+
all_statuses = ready_statuses | expired_statuses
862+
863+
for status in all_statuses:
864+
async_create_flow_start.delay(
865+
extra=status.data,
866+
flow=str(status.flow_uuid),
867+
contacts=[str(status.contact_uuid)],
868+
)

eventstore/tests/test_tasks.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import requests
66
import responses
77
from django.test import TestCase, override_settings
8+
from django.utils import timezone
89
from temba_client.v2 import TembaClient
910

1011
from eventstore import tasks
@@ -952,3 +953,48 @@ def test_update_status_sms(self):
952953
)
953954
self.assertEqual(self.status.preferred_channel, "SMS")
954955
self.assertIsNotNone(self.status.event_received_at)
956+
957+
958+
class ProcessWhatsAppTemplateSendStatusTests(TestCase):
959+
def setUp(self):
960+
self.contact_uuid = "06639860-1967-4b2b-97a5-f17b094f3763"
961+
self.flow_uuid = "f4cc0bfb-57ec-4fd9-8c32-4a6a72b14d53"
962+
self.status_ignored = WhatsAppTemplateSendStatus.objects.create(
963+
message_id="test-message-id1"
964+
)
965+
self.status_ready = WhatsAppTemplateSendStatus.objects.create(
966+
message_id="test-message-id2",
967+
status=WhatsAppTemplateSendStatus.Status.EVENT_RECEIVED,
968+
data={"status": "ready"},
969+
flow_uuid=self.flow_uuid,
970+
contact_uuid=self.contact_uuid,
971+
)
972+
self.status_expired = WhatsAppTemplateSendStatus.objects.create(
973+
message_id="test-message-id3",
974+
status=WhatsAppTemplateSendStatus.Status.WIRED,
975+
data={"status": "expired"},
976+
flow_uuid=self.flow_uuid,
977+
contact_uuid=self.contact_uuid,
978+
)
979+
self.status_expired.sent_at = timezone.now() - datetime.timedelta(hours=4)
980+
self.status_expired.save()
981+
982+
@mock.patch("eventstore.tasks.async_create_flow_start")
983+
def test_starts_flows(self, mock_async_flow_start):
984+
tasks.process_whatsapp_template_send_status()
985+
986+
self.assertEqual(
987+
mock_async_flow_start.delay.mock_calls,
988+
[
989+
mock.call(
990+
extra=self.status_ready.data,
991+
flow=self.status_ready.flow_uuid,
992+
contacts=[self.status_ready.contact_uuid],
993+
),
994+
mock.call(
995+
extra=self.status_expired.data,
996+
flow=self.status_expired.flow_uuid,
997+
contacts=[self.status_expired.contact_uuid],
998+
),
999+
],
1000+
)

ndoh_hub/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@
279279
day_of_week=RANDOM_CONTACTS_DAY_OF_WEEK,
280280
),
281281
},
282+
"process-whatsapp-template-send-status": {
283+
"task": "eventstore.tasks.process_whatsapp_template_send_status",
284+
"schedule": 300.0,
285+
},
282286
}
283287

284288
BULK_INSERT_EVENTS_ENABLED = env.bool("BULK_INSERT_EVENTS_ENABLED", False)
@@ -427,3 +431,7 @@
427431
AAQ_UD_INBOUND_CHECK_AUTH = env.str("AAQ_UD_INBOUND_CHECK_AUTH", None)
428432

429433
ALERT_OPTOUT_PHRASE = env.str("ALERT_OPTOUT_PHRASE", "Opt out of alerts")
434+
435+
WHATSAPP_TEMPLATE_SEND_TIMEOUT_HOURS = env.int(
436+
"WHATSAPP_TEMPLATE_SEND_TIMEOUT_HOURS", 3
437+
)

0 commit comments

Comments
 (0)