Add dynamic dashboard API endpoint
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 26s
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 26s
This commit is contained in:
parent
4130bf88d5
commit
da7cf1122b
2 changed files with 35 additions and 1 deletions
|
|
@ -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/<int:pk>/', seller_views.get_seller_detail, name='get_seller_detail'),
|
||||
|
|
|
|||
31
crm/views/dashboard_views.py
Normal file
31
crm/views/dashboard_views.py
Normal file
|
|
@ -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)
|
||||
})
|
||||
Loading…
Reference in a new issue