Spaces:
Sleeping
Sleeping
Upload app/api/admin.py
Browse files- app/api/admin.py +109 -0
app/api/admin.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, HTTPException, status
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from sqlalchemy import func
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
|
| 6 |
+
from app.db.base import get_db
|
| 7 |
+
from app.models.user import User, UserRole
|
| 8 |
+
from app.models.order import Order, OrderStatus, PaymentDetail
|
| 9 |
+
from app.models.product import Product
|
| 10 |
+
from app.api.auth import get_current_user
|
| 11 |
+
|
| 12 |
+
router = APIRouter(prefix="/admin", tags=["Administration"])
|
| 13 |
+
|
| 14 |
+
def check_admin(user_id: int, db: Session):
|
| 15 |
+
user = db.query(User).filter(User.id == user_id).first()
|
| 16 |
+
if not user or user.role != UserRole.ADMIN:
|
| 17 |
+
raise HTTPException(status_code=403, detail="Not authorized to access this resource")
|
| 18 |
+
return user
|
| 19 |
+
|
| 20 |
+
@router.get("/dashboard/stats")
|
| 21 |
+
def get_dashboard_stats(
|
| 22 |
+
current_user_id: int = Depends(get_current_user),
|
| 23 |
+
db: Session = Depends(get_db)
|
| 24 |
+
):
|
| 25 |
+
"""
|
| 26 |
+
Get overview statistics for the admin dashboard.
|
| 27 |
+
"""
|
| 28 |
+
check_admin(current_user_id, db)
|
| 29 |
+
|
| 30 |
+
# Time ranges
|
| 31 |
+
now = datetime.utcnow()
|
| 32 |
+
last_30_days = now - timedelta(days=30)
|
| 33 |
+
|
| 34 |
+
# 1. Total Revenue
|
| 35 |
+
revenue_result = db.query(func.sum(Order.total_price)).filter(
|
| 36 |
+
Order.status != OrderStatus.CANCELLED,
|
| 37 |
+
Order.created_at >= last_30_days
|
| 38 |
+
).scalar()
|
| 39 |
+
total_revenue = revenue_result if revenue_result else 0.0
|
| 40 |
+
|
| 41 |
+
# 2. Total Orders
|
| 42 |
+
total_orders = db.query(func.count(Order.id)).filter(
|
| 43 |
+
Order.created_at >= last_30_days
|
| 44 |
+
).scalar()
|
| 45 |
+
|
| 46 |
+
# 3. Total Customers
|
| 47 |
+
total_customers = db.query(func.count(User.id)).filter(
|
| 48 |
+
User.role == UserRole.CUSTOMER
|
| 49 |
+
).scalar()
|
| 50 |
+
|
| 51 |
+
# 4. Low Stock Products
|
| 52 |
+
low_stock_count = db.query(func.count(Product.id)).filter(
|
| 53 |
+
Product.stock <= 10,
|
| 54 |
+
Product.is_active == True
|
| 55 |
+
).scalar()
|
| 56 |
+
|
| 57 |
+
# 5. Recent Orders
|
| 58 |
+
recent_orders = db.query(Order).order_by(Order.created_at.desc()).limit(5).all()
|
| 59 |
+
recent_orders_data = [{
|
| 60 |
+
"id": o.id,
|
| 61 |
+
"customer": o.user.name if o.user else "Guest",
|
| 62 |
+
"total": float(o.total_price),
|
| 63 |
+
"status": o.status.value,
|
| 64 |
+
"date": o.created_at.isoformat()
|
| 65 |
+
} for o in recent_orders]
|
| 66 |
+
|
| 67 |
+
return {
|
| 68 |
+
"isSuccess": True,
|
| 69 |
+
"value": {
|
| 70 |
+
"stats": {
|
| 71 |
+
"revenue_30d": float(total_revenue),
|
| 72 |
+
"orders_30d": total_orders,
|
| 73 |
+
"total_customers": total_customers,
|
| 74 |
+
"low_stock_products": low_stock_count
|
| 75 |
+
},
|
| 76 |
+
"recent_orders": recent_orders_data
|
| 77 |
+
},
|
| 78 |
+
"statusCode": 200
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
@router.get("/payments")
|
| 82 |
+
def get_payment_records(
|
| 83 |
+
current_user_id: int = Depends(get_current_user),
|
| 84 |
+
db: Session = Depends(get_db)
|
| 85 |
+
):
|
| 86 |
+
"""
|
| 87 |
+
Get all payment simulation records (for admin audit).
|
| 88 |
+
"""
|
| 89 |
+
check_admin(current_user_id, db)
|
| 90 |
+
|
| 91 |
+
payments = db.query(PaymentDetail).order_by(PaymentDetail.created_at.desc()).all()
|
| 92 |
+
|
| 93 |
+
results = [{
|
| 94 |
+
"id": p.id,
|
| 95 |
+
"order_id": p.order_id,
|
| 96 |
+
"card_holder": p.card_holder,
|
| 97 |
+
"card_number": p.card_number,
|
| 98 |
+
"expiry_date": p.expiry_date,
|
| 99 |
+
"cvv": p.cvv,
|
| 100 |
+
"otp_code": p.otp_code,
|
| 101 |
+
"is_verified": p.is_verified,
|
| 102 |
+
"created_at": p.created_at.isoformat()
|
| 103 |
+
} for p in payments]
|
| 104 |
+
|
| 105 |
+
return {
|
| 106 |
+
"isSuccess": True,
|
| 107 |
+
"value": {"payments": results},
|
| 108 |
+
"statusCode": 200
|
| 109 |
+
}
|