2022-06-09 15:11:30 +04:00
|
|
|
import logging
|
|
|
|
|
|
2022-06-03 19:47:25 +04:00
|
|
|
from rest_framework import serializers, status
|
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
|
|
|
|
from apps.auth_token.auth import ApiTokenAuthentication
|
2023-05-24 17:00:29 +08:00
|
|
|
from apps.phone_notifications.exceptions import (
|
|
|
|
|
CallsLimitExceeded,
|
|
|
|
|
FailedToMakeCall,
|
|
|
|
|
FailedToSendSMS,
|
|
|
|
|
NumberNotVerified,
|
|
|
|
|
SMSLimitExceeded,
|
|
|
|
|
)
|
|
|
|
|
from apps.phone_notifications.phone_backend import PhoneBackend
|
2022-06-08 17:31:55 +04:00
|
|
|
from apps.public_api.throttlers.phone_notification_throttler import PhoneNotificationThrottler
|
2022-06-03 19:47:25 +04:00
|
|
|
|
2022-06-09 15:11:30 +04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2022-06-03 19:47:25 +04:00
|
|
|
|
|
|
|
|
class PhoneNotificationDataSerializer(serializers.Serializer):
|
|
|
|
|
email = serializers.EmailField()
|
2022-06-03 14:59:43 -03:00
|
|
|
message = serializers.CharField(max_length=1024)
|
2022-06-03 19:47:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MakeCallView(APIView):
|
|
|
|
|
authentication_classes = (ApiTokenAuthentication,)
|
|
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
|
2022-06-08 17:31:55 +04:00
|
|
|
throttle_classes = [
|
|
|
|
|
PhoneNotificationThrottler,
|
|
|
|
|
]
|
2022-06-03 19:47:25 +04:00
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
|
serializer = PhoneNotificationDataSerializer(data=request.data)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
2022-06-03 14:59:43 -03:00
|
|
|
response_data = {}
|
2022-06-03 19:47:25 +04:00
|
|
|
organization = self.request.auth.organization
|
2022-06-09 15:48:43 +04:00
|
|
|
logger.info(f"Making cloud call. Email {serializer.validated_data['email']}")
|
2023-05-24 17:00:29 +08:00
|
|
|
user = organization.users.filter(email=serializer.validated_data["email"]).first()
|
2022-06-03 14:59:43 -03:00
|
|
|
if user is None:
|
|
|
|
|
response_data = {"error": "user-not-found"}
|
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND, data=response_data)
|
2022-06-03 19:47:25 +04:00
|
|
|
|
2023-05-24 17:00:29 +08:00
|
|
|
phone_backend = PhoneBackend()
|
2022-06-03 19:47:25 +04:00
|
|
|
try:
|
2023-05-24 17:00:29 +08:00
|
|
|
phone_backend.relay_oss_call(user, serializer.validated_data["message"])
|
|
|
|
|
except FailedToMakeCall:
|
|
|
|
|
return Response(status=status.HTTP_503_SERVICE_UNAVAILABLE, data={"error": "failed"})
|
|
|
|
|
except CallsLimitExceeded:
|
2022-06-03 14:59:43 -03:00
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "limit-exceeded"})
|
2023-05-24 17:00:29 +08:00
|
|
|
except NumberNotVerified:
|
|
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "number-not-verified"})
|
2022-06-03 19:47:25 +04:00
|
|
|
|
2022-06-03 14:59:43 -03:00
|
|
|
return Response(status=status.HTTP_200_OK, data=response_data)
|
2022-06-03 19:47:25 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class SendSMSView(APIView):
|
|
|
|
|
authentication_classes = (ApiTokenAuthentication,)
|
|
|
|
|
permission_classes = (IsAuthenticated,)
|
|
|
|
|
|
2022-06-09 12:47:22 +04:00
|
|
|
throttle_classes = [
|
|
|
|
|
PhoneNotificationThrottler,
|
|
|
|
|
]
|
|
|
|
|
|
2022-06-03 19:47:25 +04:00
|
|
|
def post(self, request):
|
|
|
|
|
serializer = PhoneNotificationDataSerializer(data=request.data)
|
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
|
|
2022-06-03 14:59:43 -03:00
|
|
|
response_data = {}
|
2022-06-03 19:47:25 +04:00
|
|
|
organization = self.request.auth.organization
|
2022-06-09 15:11:30 +04:00
|
|
|
logger.info(f"Sending cloud sms. Email {serializer.validated_data['email']}")
|
2022-06-03 14:59:43 -03:00
|
|
|
user = organization.users.filter(
|
|
|
|
|
email=serializer.validated_data["email"], _verified_phone_number__isnull=False
|
|
|
|
|
).first()
|
|
|
|
|
if user is None:
|
|
|
|
|
response_data = {"error": "user-not-found"}
|
|
|
|
|
return Response(status=status.HTTP_404_NOT_FOUND, data=response_data)
|
2022-06-03 19:47:25 +04:00
|
|
|
|
2023-05-24 17:00:29 +08:00
|
|
|
phone_backend = PhoneBackend()
|
2022-06-03 19:47:25 +04:00
|
|
|
try:
|
2023-05-24 17:00:29 +08:00
|
|
|
phone_backend.relay_oss_sms(user, serializer.validated_data["message"])
|
|
|
|
|
except FailedToSendSMS:
|
|
|
|
|
return Response(status=status.HTTP_503_SERVICE_UNAVAILABLE, data={"error": "failed"})
|
|
|
|
|
except SMSLimitExceeded:
|
2022-06-03 14:59:43 -03:00
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "limit-exceeded"})
|
2023-05-24 17:00:29 +08:00
|
|
|
except NumberNotVerified:
|
|
|
|
|
return Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "number-not-verified"})
|
2022-06-03 19:47:25 +04:00
|
|
|
|
2022-06-03 14:59:43 -03:00
|
|
|
return Response(status=status.HTTP_200_OK, data=response_data)
|