mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-10-05 13:35:23 +00:00
feat(admin): add AdminTopupCreateView for admin top-up functionality ✨
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m33s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m33s
This commit is contained in:
@@ -11,6 +11,7 @@ from .views import (
|
||||
TopupDetailAPIView,
|
||||
CancelTopupView,
|
||||
ListWalletTransactionView,
|
||||
AdminTopupCreateView,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
@@ -37,6 +38,7 @@ urlpatterns = [
|
||||
VerifyTopupPaymentAPIView.as_view(),
|
||||
name="verify-topup-payment",
|
||||
),
|
||||
path("admin-topup/", AdminTopupCreateView.as_view(), name="admin-topup"),
|
||||
path(
|
||||
"topup/<str:pk>/cancel/",
|
||||
CancelTopupView.as_view(),
|
||||
|
@@ -595,6 +595,47 @@ class CancelTopupView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||
return super().update(request, *args, **kwargs)
|
||||
|
||||
|
||||
class AdminTopupCreateView(StaffEditorPermissionMixin, generics.CreateAPIView):
|
||||
queryset = Topup.objects.all().select_related("user")
|
||||
serializer_class = TopupSerializer
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
data = request.data
|
||||
user_id = data.get("user_id")
|
||||
amount = data.get("amount")
|
||||
if not getattr(request.user, "is_admin", False):
|
||||
return Response(
|
||||
{"message": "You are not authorized to perform this action."},
|
||||
status=status.HTTP_403_FORBIDDEN,
|
||||
)
|
||||
if not user_id:
|
||||
return Response(
|
||||
{"message": "user_id is required."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
if not amount:
|
||||
return Response(
|
||||
{"message": "amount is required."},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
user = User.objects.filter(id=user_id).first()
|
||||
if not user:
|
||||
return Response(
|
||||
{"message": "User not found."},
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
topup = Topup.objects.create(amount=amount, user=user)
|
||||
description = f"Topup of {amount} MVR (Cash)"
|
||||
WalletTransaction.objects.create(
|
||||
user=user,
|
||||
amount=amount,
|
||||
transaction_type="TOPUP",
|
||||
description=description,
|
||||
)
|
||||
serializer = TopupSerializer(topup)
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
|
||||
class ListWalletTransactionView(StaffEditorPermissionMixin, generics.ListAPIView):
|
||||
serializer_class = WalletTransactionSerializer
|
||||
queryset = WalletTransaction.objects.all().select_related("user")
|
||||
|
Reference in New Issue
Block a user