Skip to content

Commit

Permalink
commit merge for endpoint custom fields frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanJaeger committed Jul 19, 2023
1 parent 45905e3 commit b6b7b9f
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions chats/apps/api/v1/rooms/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from chats.apps.rooms.models import Room
from chats.celery import app as celery_app

from chats.apps.api.v1.internal.rest_clients.flows_rest_client import FlowRESTClient


class RoomViewset(
mixins.ListModelMixin,
Expand Down Expand Up @@ -230,3 +232,49 @@ def perform_update(self, serializer):
def perform_destroy(self, instance):
instance.notify_room("destroy", callback=True)
super().perform_destroy(instance)

@action(
detail=True,
methods=["PATCH"],
)
def update_custom_fields(self, request, pk=None):
custom_fields_update = request.data
data = {"fields": custom_fields_update}

if pk is None:
return Response(
{"Detail": "No room on the request"}, status.HTTP_400_BAD_REQUEST
)

try:
room = Room.objects.get(uuid=pk, is_active=True)
except:
return Response(
{
"Detail": "Room with the given id was not found, it does not exist or it is deleted"
},
status.HTTP_404_NOT_FOUND,
)

response = FlowRESTClient().create_contact(
project=room.queue.sector.project,
data=data,
contact_id=room.contact.external_id,
)
if response.status_code not in [status.HTTP_200_OK]:
return Response(
{
"Detail": f"[{response.status_code}]\n"
+ f"Error updating custom fields on flows. Exception: {response.content}"
},
status.HTTP_404_NOT_FOUND,
)

old_custom_fields = room.custom_fields
room.custom_fields = {**old_custom_fields, **custom_fields_update}
room.save()

return Response(
{"Detail": "Custom Field edited with success"},
status.HTTP_200_OK,
)

0 comments on commit b6b7b9f

Please sign in to comment.