diff --git a/crm/urls.py b/crm/urls.py index e307a2e..38b4762 100644 --- a/crm/urls.py +++ b/crm/urls.py @@ -1,11 +1,14 @@ from django.urls import path -from crm.views import auth_views, seller_views, customer_views, product_views, wallet_views, debug_views +from crm.views import auth_views, seller_views, customer_views, product_views, wallet_views, debug_views, dashboard_views urlpatterns = [ # Auth path('auth/login/', auth_views.CRMTokenObtainPairView.as_view(), name='token_obtain_pair'), path('auth/me/', auth_views.get_current_admin, name='current_admin'), + # Dashboard + path('dashboard/', dashboard_views.get_dashboard_stats, name='get_dashboard_stats'), + # Sellers path('sellers/', seller_views.list_sellers, name='list_sellers'), path('sellers//', seller_views.get_seller_detail, name='get_seller_detail'), diff --git a/crm/views/dashboard_views.py b/crm/views/dashboard_views.py new file mode 100644 index 0000000..c4fa71b --- /dev/null +++ b/crm/views/dashboard_views.py @@ -0,0 +1,31 @@ +from rest_framework.decorators import api_view, permission_classes +from rest_framework.response import Response +from django.db.models import Sum +from crm.permissions import IsCRMAdmin +from seller_models.models import SupplierProfile, Product +from customer_models.models import Order +from crm.models import RefundRecord + +@api_view(['GET']) +@permission_classes([IsCRMAdmin]) +def get_dashboard_stats(request): + # Calculate stats + total_sellers = SupplierProfile.objects.count() + pending_approvals = SupplierProfile.objects.filter(status='pending_approval').count() + + # Sum completed orders amount for total revenue + total_revenue = Order.objects.filter(payment_status='completed').aggregate(Sum('amount'))['amount__sum'] or 0.00 + + active_products = Product.objects.filter(is_active=True, status='approved').count() + + # Sum refund records + refunds_issued = RefundRecord.objects.aggregate(Sum('refund_amount'))['refund_amount__sum'] or 0.00 + + # Format the stats similarly to the frontend cards + return Response({ + 'total_sellers': total_sellers, + 'pending_approvals': pending_approvals, + 'total_revenue': float(total_revenue), + 'active_products': active_products, + 'refunds_issued': float(refunds_issued) + })