Skip to content

Commit

Permalink
Start flow if possible after event received
Browse files Browse the repository at this point in the history
  • Loading branch information
erikh360 committed Jul 25, 2024
1 parent cf93a11 commit 2f7c16f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
9 changes: 9 additions & 0 deletions eventstore/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,15 @@ def update_whatsapp_template_send_status(message_id, preferred_channel=None):
status.preferred_channel = preferred_channel

status.save()

if status.flow_uuid:
extra = status.data
extra["preferred_channel"] = status.preferred_channel
async_create_flow_start(
extra=extra,
flow=str(status.flow_uuid),
contacts=[str(status.contact_uuid)],
)
except WhatsAppTemplateSendStatus.DoesNotExist:
pass

Expand Down
39 changes: 35 additions & 4 deletions eventstore/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,31 @@ def setUp(self):
message_id="test-message-id"
)

def test_update_status_whatsapp(self):
tasks.update_whatsapp_template_send_status.delay(self.status.message_id)
@mock.patch("eventstore.tasks.async_create_flow_start")
def test_update_status_whatsapp(self, mock_flow_start):
"""
If the event is received the status record needs to be updated
"""
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.assertEqual(self.status.preferred_channel, "WhatsApp")
self.assertIsNotNone(self.status.event_received_at)

mock_flow_start.assert_not_called()

@mock.patch("eventstore.tasks.async_create_flow_start")
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"
tasks.update_whatsapp_template_send_status(self.status.message_id)

self.status.refresh_from_db()

Expand All @@ -943,8 +966,15 @@ def test_update_status_whatsapp(self):
self.assertEqual(self.status.preferred_channel, "WhatsApp")
self.assertIsNotNone(self.status.event_received_at)

def test_update_status_sms(self):
tasks.update_whatsapp_template_send_status.delay(self.status.message_id, "SMS")
mock_flow_start.assert_not_called()

@mock.patch("eventstore.tasks.async_create_flow_start")
def test_update_status_sms(self, mock_flow_start):
"""
If the event is received with SMS as the preferred_channel the status record
needs to be updated
"""
tasks.update_whatsapp_template_send_status(self.status.message_id, "SMS")

self.status.refresh_from_db()

Expand All @@ -953,6 +983,7 @@ def test_update_status_sms(self):
)
self.assertEqual(self.status.preferred_channel, "SMS")
self.assertIsNotNone(self.status.event_received_at)
mock_flow_start.assert_not_called()


class ProcessWhatsAppTemplateSendStatusTests(TestCase):
Expand Down

0 comments on commit 2f7c16f

Please sign in to comment.