Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility of return Empty #130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions weni/grpc/billing/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions weni/grpc/billing/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion weni/grpc/billing/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
MessageDetailRequestSerializer,
)

from google.protobuf import empty_pb2

class BillingService(generics.GenericService):

Expand Down Expand Up @@ -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

23 changes: 23 additions & 0 deletions weni/grpc/billing/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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="[email protected]")
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))