Skip to content

Commit

Permalink
feat: Add endpoint to list project contact fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro-Meireles committed Jan 28, 2025
1 parent 3dc9c33 commit 349e3a2
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions temba/api/v2/internals/contacts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from weni.internal.authenticators import InternalOIDCAuthentication
from weni.internal.permissions import CanCommunicateInternally

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import exceptions as django_exceptions

from temba.api.v2.internals.contacts.serializers import InternalContactSerializer
from temba.api.v2.internals.views import APIViewMixin
from temba.api.v2.serializers import ContactFieldWriteSerializer
from temba.contacts.models import Contact
from temba.api.v2.serializers import (
ContactFieldReadSerializer,
ContactFieldWriteSerializer,
)
from temba.contacts.models import Contact, ContactField
from temba.orgs.models import Org

User = get_user_model()
Expand Down Expand Up @@ -58,27 +63,57 @@ class InternalContactFieldsEndpoint(APIViewMixin, APIView):
authentication_classes = [InternalOIDCAuthentication]
permission_classes = [IsAuthenticated, CanCommunicateInternally]

def get(self, request, *args, **kwargs):
project_uuid = request.data.get("project")

if not project_uuid:
return Response(
{"error": "Project not provided"}, status=status.HTTP_400_BAD_REQUEST
)
try:
org = Org.objects.get(proj_uuid=project_uuid)
except (Org.DoesNotExist, django_exceptions.ValidationError):
return Response(
{"error": "Project not found"}, status=status.HTTP_404_NOT_FOUND
)

contact_fields = ContactField.user_fields.filter(org=org, is_active=True)
key = self.request.query_params.get("key")

if key:
contact_fields = contact_fields.filter(key=key)

serializer = ContactFieldReadSerializer(contact_fields, many=True)

return Response({"results": serializer.data})

def post(self, request, *args, **kwargs):
project_uuid = request.data.get("project")

if not project_uuid:
return Response({"error": "Project not provided"}, status=401)
return Response(
{"error": "Project not provided"}, status=status.HTTP_400_BAD_REQUEST
)

try:
org = Org.objects.get(proj_uuid=project_uuid)
user = User.objects.get(email=request.user.email)
except Org.DoesNotExist:
return Response({"error": "Project not found"}, status=404)
except (Org.DoesNotExist, django_exceptions.ValidationError):
return Response(
{"error": "Project not found"}, status=status.HTTP_404_NOT_FOUND
)

except User.DoesNotExist:
return Response({"error": "User not found"}, status=404)
return Response(
{"error": "User not found"}, status=status.HTTP_404_NOT_FOUND
)

serializer = ContactFieldWriteSerializer(
data=request.data, context={"request": request, "org": org, "user": user}
)

if serializer.is_valid():
serializer.save()
return Response({"message": "Success"})
return Response(serializer.validated_data)

return Response(serializer.errors, status=400)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit 349e3a2

Please sign in to comment.