Skip to content

Commit

Permalink
refactor: use enums instead of bool
Browse files Browse the repository at this point in the history
  • Loading branch information
Waleed-Mujahid committed Dec 27, 2024
1 parent df4ac66 commit 30be027
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions openedx/features/edly/api/v1/views/user_paid_for_course.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from rest_framework import viewsets
Expand All @@ -7,6 +9,13 @@
from common.djangoapps.util.json_request import JsonResponse


class PaymentStatus(Enum):
PAID = "paid"
UNPAID = "unpaid"
NOT_ENROLLED = "not_enrolled"
ERROR = "error"


class UserPaidForCourseViewSet(viewsets.ViewSet):
"""
**Use Case**
Expand All @@ -27,17 +36,25 @@ class UserPaidForCourseViewSet(viewsets.ViewSet):
The HTTP 200 response has the following values.
* has_user_paid: True if the user has paid for the course, False otherwise.
* status: The status of the user's paid status for the course.
Possible values are:
* "paid": The user has paid for the course.
* "unpaid": The user has not paid for the course.
* "error": An error occurred while retrieving the user's paid status.
"""
permission_classes = [IsAuthenticated]

def retrieve(self, request, pk=None):
"""Get the status of a user's paid status for a given course."""
try:
course_key = CourseKey.from_string(pk)
course_enrollment = CourseEnrollment.get_enrollment(request.user, course_key)
except InvalidKeyError:
return JsonResponse({'has_user_paid': False}, status=406)
enrollment = CourseEnrollment.get_enrollment(request.user, course_key)
if not enrollment:
return JsonResponse({"status": PaymentStatus.NOT_ENROLLED.value}, status=200)

order_exists = enrollment.get_order_attribute_value('order_number')
payment_status = PaymentStatus.PAID if order_exists else PaymentStatus.UNPAID

return JsonResponse({"status": payment_status.value}, status=200)

paid_status = course_enrollment.get_order_attribute_value('order_number')
return JsonResponse({'has_user_paid': bool(paid_status)}, status=200)
except (InvalidKeyError, Exception):
return JsonResponse({"status": PaymentStatus.ERROR.value}, status=406)

0 comments on commit 30be027

Please sign in to comment.