Example #1
0
    def send_message(self, request):
        logger.info("Sending message based on Camunda message end event")
        serializer = CamundaMessagerSerializer(data=request.data)

        if serializer.is_valid():
            message_name = serializer.validated_data["message_name"]
            case_identification = serializer.validated_data["case_id"]
            process_variables = serializer.validated_data["process_variables"]
            process_variables["endpoint"] = {
                "value": settings.ZAKEN_CONTAINER_HOST
            }

            raw_response = CamundaService().send_message(
                message_name=message_name,
                message_process_variables=process_variables)

            if raw_response.ok:
                response = raw_response.json()[0]
                camunda_id = response["processInstance"]["id"]

                case = Case.objects.get(identification=case_identification)
                case.camunda_id = camunda_id
                case.save()

                logger.info(f"Message send {message_name} ended succesfully")
                return Response(status=status.HTTP_200_OK)

            rendered_content = raw_response.content.decode("UTF-8")
            logger.error(f"FAIL: Message send response:{rendered_content}")
            return Response(status=status.HTTP_400_BAD_REQUEST)
        else:
            logger.error(f"FAIL: Message send {serializer.errors}")
            return Response(status=status.HTTP_400_BAD_REQUEST)
            instance = data["camunda_process_id"]

            try:
                case = Case.objects.get(id=pk)
            except Case.DoesNotExist:
                return Response(
                    data="Camunda process has not started. Case does not exist",
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                )

            response = CamundaService().send_message(
                message_name=instance.camunda_message_name,
                case_identification=case.id)

            try:
                json_response = response.json()[0]
                camunda_process_id = json_response["processInstance"]["id"]
            except Exception:
                return Response(
                    data=
                    f"Camunda process has not started. Json response not valid {str(response.content)}",
                    status=status.HTTP_500_INTERNAL_SERVER_ERROR,
                )

            case.add_camunda_id(camunda_process_id)
            case.save()

            if response:
                return Response(
                    data=f"Process has started {str(response.content)}",
                    status=status.HTTP_200_OK,