diff --git a/eventstore/tasks.py b/eventstore/tasks.py index 11b5f16c..7221c56e 100644 --- a/eventstore/tasks.py +++ b/eventstore/tasks.py @@ -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 diff --git a/eventstore/tests/test_tasks.py b/eventstore/tests/test_tasks.py index 52e5f122..8b37c4cf 100644 --- a/eventstore/tests/test_tasks.py +++ b/eventstore/tests/test_tasks.py @@ -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() @@ -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() @@ -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):