diff --git a/weni/grpc/billing/queries.py b/weni/grpc/billing/queries.py index ca26cb4ab..5ad9d2f3f 100644 --- a/weni/grpc/billing/queries.py +++ b/weni/grpc/billing/queries.py @@ -84,9 +84,13 @@ def incoming_message(cls, org_uuid: str, contact_uuid: str, before: datetime, af .last() ) + if not msg: + return dict(uuid = "", text = "", created_on = "", direction = "", channel_id = 0, channel_type = "", is_valid = False) + channel = Channel.objects.get(id=msg["channel"]) msg["channel_id"] = channel.id msg["channel_type"] = channel.channel_type + msg["is_valid"] = True return msg diff --git a/weni/grpc/billing/serializers.py b/weni/grpc/billing/serializers.py index ddfcffe35..861200dcd 100644 --- a/weni/grpc/billing/serializers.py +++ b/weni/grpc/billing/serializers.py @@ -92,6 +92,7 @@ class MsgDetailSerializer(ProtoSerializer): direction = serializers.CharField() channel_id = serializers.IntegerField() channel_type = serializers.CharField() + is_valid = serializers.BooleanField() class Meta: proto_class = billing_pb2.MsgDetail diff --git a/weni/grpc/billing/services.py b/weni/grpc/billing/services.py index 0a32c517e..6f9c31072 100644 --- a/weni/grpc/billing/services.py +++ b/weni/grpc/billing/services.py @@ -10,6 +10,7 @@ MessageDetailRequestSerializer, ) +from google.protobuf import empty_pb2 class BillingService(generics.GenericService): @@ -50,5 +51,5 @@ def MessageDetail(self, request, context): msg = MessageDetailQuery.incoming_message(org_uuid, contact_uuid, before, after) msg_serializer = MsgDetailSerializer(msg) - return msg_serializer.message + diff --git a/weni/grpc/billing/tests.py b/weni/grpc/billing/tests.py index fa90ae3ea..40f2d6b51 100644 --- a/weni/grpc/billing/tests.py +++ b/weni/grpc/billing/tests.py @@ -14,6 +14,7 @@ from weni.grpc.billing.queries import ActiveContactsQuery from weni.grpc.billing.serializers import BillingRequestSerializer, ActiveContactDetailSerializer from django_grpc_framework.test import FakeRpcError, RPCTransactionTestCase +from google.protobuf import empty_pb2 class ActiveContactsQueryTest(TembaTest): @@ -252,6 +253,28 @@ def test_message_detail(self): self.assertEqual(msg.direction, result.direction) self.assertEqual(channel.id, result.channel_id) self.assertEqual(channel.channel_type, result.channel_type) + self.assertEqual(True, result.is_valid) + + def test_message_detail_fail(self): + user = User.objects.create_user(username="testuser", password="123", email="test@weni.ai") + org = Org.objects.create(name="Temba", timezone="Africa/Kigali", created_by=user, modified_by=user) + + contact = self.create_contact(f"Contact 1", phone=f"+553124826922") + contact.org = org + contact.save(update_fields=["org"]) + + channel = self.create_channel(channel_type="WA", name="channel_test", address="address_test", org=org) + + msg = self.create_outgoing_msg(contact=contact, text="incoming message test", channel=channel, status="F") + + before = tz.now() + after = tz.now() - tz.timedelta(minutes=1) + + result = self.billing_detail_msg( + org_uuid=str(org.uuid), contact_uuid=str(contact.uuid), before=str(before), after=str(after) + ) + + self.assertEqual(result.is_valid, False) def billing_detail_msg(self, **kwargs): return self.stub.MessageDetail(pb2.MessageDetailRequest(**kwargs))