Skip to content

Commit

Permalink
Merge pull request #620 from praekeltfoundation/fix-capi-events
Browse files Browse the repository at this point in the history
Mark status as completed when flow is started
  • Loading branch information
erikh360 authored Aug 19, 2024
2 parents c0f15be + eee9189 commit f4ff0a0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
9 changes: 7 additions & 2 deletions eventstore/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,6 @@ def update_whatsapp_template_send_status(message_id, preferred_channel=None):
if preferred_channel:
status.preferred_channel = preferred_channel

status.save()

if status.flow_uuid:
extra = status.data
extra["preferred_channel"] = status.preferred_channel
Expand All @@ -853,6 +851,10 @@ def update_whatsapp_template_send_status(message_id, preferred_channel=None):
flow=str(status.flow_uuid),
contacts=[str(status.contact_uuid)],
)
status.status = WhatsAppTemplateSendStatus.Status.ACTION_COMPLETED
status.action_completed_at = timezone.now()

status.save()
except WhatsAppTemplateSendStatus.DoesNotExist:
pass

Expand Down Expand Up @@ -881,3 +883,6 @@ def process_whatsapp_template_send_status():
flow=str(status.flow_uuid),
contacts=[str(status.contact_uuid)],
)
status.status = WhatsAppTemplateSendStatus.Status.ACTION_COMPLETED
status.action_completed_at = timezone.now()
status.save()
20 changes: 17 additions & 3 deletions eventstore/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import json
import uuid
from unittest import mock

import requests
Expand Down Expand Up @@ -955,18 +956,20 @@ def test_update_status_whatsapp_ready(self, mock_flow_start):
If the event is received and it has a flow to start, the status record needs to
be updated and the async_create_flow_start task needs to be called
"""
self.status.flow_uuid = "flow-uuid"
self.status.flow_uuid = uuid.uuid4()
self.status.save()
tasks.update_whatsapp_template_send_status(self.status.message_id)

self.status.refresh_from_db()

self.assertEqual(
self.status.status, WhatsAppTemplateSendStatus.Status.EVENT_RECEIVED
self.status.status, WhatsAppTemplateSendStatus.Status.ACTION_COMPLETED
)
self.assertEqual(self.status.preferred_channel, "WhatsApp")
self.assertIsNotNone(self.status.event_received_at)
self.assertIsNotNone(self.status.action_completed_at)

mock_flow_start.assert_not_called()
mock_flow_start.assert_called()

@mock.patch("eventstore.tasks.async_create_flow_start")
def test_update_status_sms(self, mock_flow_start):
Expand Down Expand Up @@ -1047,3 +1050,14 @@ def test_starts_flows(self, mock_async_flow_start):
),
],
)

self.status_ready.refresh_from_db()
self.status_expired.refresh_from_db()

self.assertEqual(
self.status_ready.status, WhatsAppTemplateSendStatus.Status.ACTION_COMPLETED
)
self.assertEqual(
self.status_expired.status,
WhatsAppTemplateSendStatus.Status.ACTION_COMPLETED,
)

0 comments on commit f4ff0a0

Please sign in to comment.