File size: 36,242 Bytes
0217894 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 | from typing import Optional, List
from uuid import UUID
from datetime import datetime, timedelta
from fastapi import APIRouter, Depends, HTTPException, status, Query
from pydantic import BaseModel, EmailStr
from sqlalchemy import select, func, or_, desc, asc
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload, aliased
import bcrypt
import jwt
from Backend.database.connection import get_db
from Backend.database.models import Department, Member, Issue, Escalation, Classification, IssueEvent, IssueImage
from Backend.core.config import settings
from Backend.core.logging import get_logger
from Backend.core.schemas import IssueResponse, IssueState
from Backend.utils.storage import get_upload_url
logger = get_logger(__name__)
router = APIRouter()
SECRET_KEY = settings.supabase_jwt_secret
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_HOURS = 24
def hash_password(password: str) -> str:
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
def verify_password(password: str, password_hash: str) -> bool:
return bcrypt.checkpw(password.encode(), password_hash.encode())
def create_access_token(member_id: UUID, role: str) -> str:
expire = datetime.utcnow() + timedelta(hours=ACCESS_TOKEN_EXPIRE_HOURS)
payload = {
"sub": str(member_id),
"role": role,
"exp": expire,
"iat": datetime.utcnow(),
}
return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)
class LoginRequest(BaseModel):
email: str
password: str
expected_role: Optional[str] = None
class LoginResponse(BaseModel):
access_token: str
token_type: str = "bearer"
user: dict
from fastapi.security import OAuth2PasswordBearer
from jwt import PyJWTError
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/admin/login")
async def get_current_user(token: str = Depends(oauth2_scheme), db: AsyncSession = Depends(get_db)):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
member_id: str = payload.get("sub")
if member_id is None:
raise HTTPException(status_code=401, detail="Invalid authentication credentials")
except PyJWTError:
raise HTTPException(status_code=401, detail="Invalid authentication credentials")
member = await db.get(Member, UUID(member_id))
if member is None:
raise HTTPException(status_code=401, detail="User not found")
return member
async def get_current_active_user(current_user: Member = Depends(get_current_user)):
if not current_user.is_active:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
async def get_current_admin(current_user: Member = Depends(get_current_active_user)):
if current_user.role != "admin":
raise HTTPException(status_code=403, detail="Not authorized")
return current_user
@router.post("/login", response_model=LoginResponse)
async def staff_login(
data: LoginRequest,
db: AsyncSession = Depends(get_db),
):
member = await db.execute(
select(Member).where(Member.email == data.email, Member.is_active == True)
)
member = member.scalar_one_or_none()
if not member or not member.password_hash:
raise HTTPException(status_code=401, detail="Invalid email or password")
if not verify_password(data.password, member.password_hash):
raise HTTPException(status_code=401, detail="Invalid email or password")
if data.expected_role:
if data.expected_role == "admin" and member.role != "admin":
raise HTTPException(status_code=403, detail="Access denied. You are not an admin.")
if data.expected_role == "worker" and member.role == "admin":
raise HTTPException(status_code=403, detail="Admins should login as Admin, not Worker.")
access_token = create_access_token(member.id, member.role)
return LoginResponse(
access_token=access_token,
user={
"id": str(member.id),
"name": member.name,
"email": member.email,
"role": member.role,
"department_id": str(member.department_id) if member.department_id else None,
},
)
class DepartmentCreate(BaseModel):
name: str
code: str
description: Optional[str] = None
categories: Optional[str] = None
default_sla_hours: int = 48
escalation_email: Optional[str] = None
class DepartmentUpdate(BaseModel):
name: Optional[str] = None
description: Optional[str] = None
categories: Optional[str] = None
default_sla_hours: Optional[int] = None
escalation_email: Optional[str] = None
is_active: Optional[bool] = None
class DepartmentResponse(BaseModel):
id: UUID
name: str
code: str
description: Optional[str]
categories: Optional[str]
default_sla_hours: int
escalation_email: Optional[str]
is_active: bool
member_count: int = 0
class Config:
from_attributes = True
class MemberInvite(BaseModel):
department_id: UUID
name: str
email: str
phone: Optional[str] = None
role: str = "officer"
city: Optional[str] = None
locality: Optional[str] = None
max_workload: int = 10
send_invite: bool = True
class MemberCreate(BaseModel):
department_id: UUID
name: str
email: str
phone: Optional[str] = None
role: str = "worker"
city: Optional[str] = None
locality: Optional[str] = None
max_workload: int = 10
password: str
class MemberUpdate(BaseModel):
name: Optional[str] = None
email: Optional[str] = None
phone: Optional[str] = None
role: Optional[str] = None
city: Optional[str] = None
locality: Optional[str] = None
max_workload: Optional[int] = None
is_active: Optional[bool] = None
password: Optional[str] = None
class MemberResponse(BaseModel):
id: UUID
department_id: Optional[UUID]
name: str
email: str
phone: Optional[str]
role: str
city: Optional[str]
locality: Optional[str]
is_active: bool
current_workload: int
max_workload: int
invite_status: Optional[str] = None
class Config:
from_attributes = True
@router.post("/departments", response_model=DepartmentResponse, status_code=status.HTTP_201_CREATED)
async def create_department(
data: DepartmentCreate,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
existing = await db.execute(select(Department).where(Department.code == data.code))
if existing.scalar_one_or_none():
raise HTTPException(status_code=400, detail="Department code already exists")
department = Department(
name=data.name,
code=data.code.upper(),
description=data.description,
categories=data.categories,
default_sla_hours=data.default_sla_hours,
escalation_email=data.escalation_email,
)
db.add(department)
await db.flush()
await db.refresh(department)
return DepartmentResponse(
id=department.id,
name=department.name,
code=department.code,
description=department.description,
categories=department.categories,
default_sla_hours=department.default_sla_hours,
escalation_email=department.escalation_email,
is_active=department.is_active,
member_count=0,
)
@router.get("/departments", response_model=list[DepartmentResponse])
async def list_departments(
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
query = select(Department).order_by(Department.name)
result = await db.execute(query)
departments = result.scalars().all()
response = []
for dept in departments:
member_count = await db.execute(
select(func.count(Member.id)).where(Member.department_id == dept.id)
)
count = member_count.scalar() or 0
response.append(DepartmentResponse(
id=dept.id,
name=dept.name,
code=dept.code,
description=dept.description,
categories=dept.categories,
default_sla_hours=dept.default_sla_hours,
escalation_email=dept.escalation_email,
is_active=dept.is_active,
member_count=count,
))
return response
@router.get("/departments/{department_id}", response_model=DepartmentResponse)
async def get_department(
department_id: UUID,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
department = await db.get(Department, department_id)
if not department:
raise HTTPException(status_code=404, detail="Department not found")
member_count = await db.execute(
select(func.count(Member.id)).where(Member.department_id == department.id)
)
count = member_count.scalar() or 0
return DepartmentResponse(
id=department.id,
name=department.name,
code=department.code,
description=department.description,
categories=department.categories,
default_sla_hours=department.default_sla_hours,
escalation_email=department.escalation_email,
is_active=department.is_active,
member_count=count,
)
@router.patch("/departments/{department_id}", response_model=DepartmentResponse)
async def update_department(
department_id: UUID,
data: DepartmentUpdate,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
department = await db.get(Department, department_id)
if not department:
raise HTTPException(status_code=404, detail="Department not found")
update_data = data.model_dump(exclude_unset=True)
for key, value in update_data.items():
setattr(department, key, value)
await db.flush()
member_count = await db.execute(
select(func.count(Member.id)).where(Member.department_id == department.id)
)
count = member_count.scalar() or 0
return DepartmentResponse(
id=department.id,
name=department.name,
code=department.code,
description=department.description,
categories=department.categories,
default_sla_hours=department.default_sla_hours,
escalation_email=department.escalation_email,
is_active=department.is_active,
member_count=count,
)
@router.delete("/departments/{department_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_department(
department_id: UUID,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
department = await db.get(Department, department_id)
if not department:
raise HTTPException(status_code=404, detail="Department not found")
await db.delete(department)
await db.flush()
@router.post("/members/invite", status_code=status.HTTP_201_CREATED)
async def invite_member(
data: MemberInvite,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
department = await db.get(Department, data.department_id)
if not department:
raise HTTPException(status_code=404, detail="Department not found")
existing = await db.execute(select(Member).where(Member.email == data.email))
if existing.scalar_one_or_none():
raise HTTPException(status_code=400, detail="Email already exists")
invite_result = None
if data.send_invite:
invite_result = await supabase_auth.invite_user(
email=data.email,
redirect_to=f"{settings.frontend_url}/auth/callback"
)
member = Member(
department_id=data.department_id,
name=data.name,
email=data.email,
phone=data.phone,
role=data.role,
city=data.city,
locality=data.locality,
max_workload=data.max_workload,
)
db.add(member)
await db.flush()
await db.refresh(member)
return {
"member": MemberResponse(
id=member.id,
department_id=member.department_id,
name=member.name,
email=member.email,
phone=member.phone,
role=member.role,
city=member.city,
locality=member.locality,
is_active=member.is_active,
current_workload=member.current_workload,
max_workload=member.max_workload,
invite_status="sent" if invite_result and invite_result.get("success") else "not_sent",
),
"invite": invite_result,
"message": f"Member created. {'Invite email sent!' if invite_result and invite_result.get('success') else 'No invite sent.'}",
}
@router.post("/members", response_model=MemberResponse, status_code=status.HTTP_201_CREATED)
async def create_member(
data: MemberCreate,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
department = await db.get(Department, data.department_id)
if not department:
raise HTTPException(status_code=404, detail="Department not found")
existing = await db.execute(select(Member).where(Member.email == data.email))
if existing.scalar_one_or_none():
raise HTTPException(status_code=400, detail="Email already exists")
member = Member(
department_id=data.department_id,
name=data.name,
email=data.email,
phone=data.phone,
role=data.role,
city=data.city,
locality=data.locality,
max_workload=data.max_workload,
password_hash=hash_password(data.password),
)
db.add(member)
await db.flush()
await db.refresh(member)
return MemberResponse(
id=member.id,
department_id=member.department_id,
name=member.name,
email=member.email,
phone=member.phone,
role=member.role,
city=member.city,
locality=member.locality,
is_active=member.is_active,
current_workload=member.current_workload,
max_workload=member.max_workload,
)
@router.post("/members/{member_id}/send-invite")
async def send_member_invite(
member_id: UUID,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
member = await db.get(Member, member_id)
if not member:
raise HTTPException(status_code=404, detail="Member not found")
if not settings.frontend_url:
raise HTTPException(status_code=500, detail="FRONTEND_URL not configured")
result = await supabase_auth.invite_user(
email=member.email,
redirect_to=f"{settings.frontend_url}/auth/callback"
)
if result.get("success"):
return {
"success": True,
"message": f"Invite sent to {member.email}",
"member_id": str(member.id),
}
else:
raise HTTPException(
status_code=400,
detail=result.get("message", "Failed to send invite")
)
@router.post("/members/{member_id}/magic-link")
async def send_magic_link(
member_id: UUID,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
member = await db.get(Member, member_id)
if not member:
raise HTTPException(status_code=404, detail="Member not found")
if not settings.frontend_url:
raise HTTPException(status_code=500, detail="FRONTEND_URL not configured")
result = await supabase_auth.send_magic_link(
email=member.email,
redirect_to=f"{settings.frontend_url}/auth/callback"
)
if result.get("success"):
return {
"success": True,
"message": f"Magic link sent to {member.email}",
}
else:
raise HTTPException(
status_code=400,
detail=result.get("message", "Failed to send magic link")
)
@router.get("/members", response_model=list[MemberResponse])
async def list_members(
department_id: Optional[UUID] = None,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
query = select(Member).order_by(Member.name)
if department_id:
query = query.where(Member.department_id == department_id)
result = await db.execute(query)
members = result.scalars().all()
return [
MemberResponse(
id=m.id,
department_id=m.department_id,
name=m.name,
email=m.email,
phone=m.phone,
role=m.role,
city=m.city,
locality=m.locality,
is_active=m.is_active,
current_workload=m.current_workload,
max_workload=m.max_workload,
)
for m in members
]
@router.get("/members/{member_id}", response_model=MemberResponse)
async def get_member(
member_id: UUID,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
member = await db.get(Member, member_id)
if not member:
raise HTTPException(status_code=404, detail="Member not found")
return MemberResponse(
id=member.id,
department_id=member.department_id,
name=member.name,
email=member.email,
phone=member.phone,
role=member.role,
city=member.city,
locality=member.locality,
is_active=member.is_active,
current_workload=member.current_workload,
max_workload=member.max_workload,
)
@router.patch("/members/{member_id}", response_model=MemberResponse)
async def update_member(
member_id: UUID,
data: MemberUpdate,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
member = await db.get(Member, member_id)
if not member:
raise HTTPException(status_code=404, detail="Member not found")
update_data = data.model_dump(exclude_unset=True)
for key, value in update_data.items():
setattr(member, key, value)
await db.flush()
return MemberResponse(
id=member.id,
department_id=member.department_id,
name=member.name,
email=member.email,
phone=member.phone,
role=member.role,
city=member.city,
locality=member.locality,
is_active=member.is_active,
current_workload=member.current_workload,
max_workload=member.max_workload,
)
@router.delete("/members/{member_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_member(
member_id: UUID,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
member = await db.get(Member, member_id)
if not member:
raise HTTPException(status_code=404, detail="Member not found")
await db.delete(member)
await db.flush()
@router.get("/stats")
async def get_admin_stats(
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
from Backend.database.models import Issue, Classification
from datetime import datetime, timedelta
dept_count = await db.execute(select(func.count(Department.id)))
member_count = await db.execute(select(func.count(Member.id)))
issue_count = await db.execute(select(func.count(Issue.id)))
pending_count = await db.execute(
select(func.count(Issue.id)).where(Issue.state.in_(["reported", "validated", "assigned"]))
)
resolved_count = await db.execute(
select(func.count(Issue.id)).where(Issue.state.in_(["resolved", "closed", "verified"]))
)
verification_count = await db.execute(
select(func.count(Issue.id)).where(Issue.state == "pending_verification")
)
category_query = (
select(
Classification.primary_category,
func.count(Classification.id).label("count")
)
.group_by(Classification.primary_category)
.order_by(func.count(Classification.id).desc())
.limit(6)
)
category_result = await db.execute(category_query)
categories = category_result.all()
issues_by_category = [{"name": cat or "Unknown", "value": cnt} for cat, cnt in categories]
today = datetime.utcnow().date()
day_names = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
issues_activity = []
for i in range(6, -1, -1):
day = today - timedelta(days=i)
day_start = datetime.combine(day, datetime.min.time())
day_end = datetime.combine(day, datetime.max.time())
reported_q = await db.execute(
select(func.count(Issue.id)).where(
Issue.created_at >= day_start,
Issue.created_at <= day_end
)
)
resolved_q = await db.execute(
select(func.count(Issue.id)).where(
Issue.resolved_at >= day_start,
Issue.resolved_at <= day_end
)
)
issues_activity.append({
"name": day_names[day.weekday()],
"reported": reported_q.scalar() or 0,
"resolved": resolved_q.scalar() or 0
})
return {
"departments": dept_count.scalar() or 0,
"members": member_count.scalar() or 0,
"total_issues": issue_count.scalar() or 0,
"pending_issues": pending_count.scalar() or 0,
"resolved_issues": resolved_count.scalar() or 0,
"verification_needed": verification_count.scalar() or 0,
"issues_by_category": issues_by_category,
"issues_activity": issues_activity,
}
@router.get("/stats/heatmap")
async def get_issue_heatmap(
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
"""
Returns city-aggregated issue counts for heatmap visualization.
"""
query = (
select(
Issue.city,
func.count(Issue.id).label("count"),
func.avg(Issue.priority).label("priority_avg")
)
.where(Issue.state.notin_(["closed", "resolved", "verified"]))
.where(Issue.city.isnot(None))
.group_by(Issue.city)
.order_by(func.count(Issue.id).desc())
)
result = await db.execute(query)
rows = result.all()
heatmap_data = []
for city, count, priority_avg in rows:
heatmap_data.append({
"city": city or "Unknown",
"count": count,
"priority_avg": round(float(priority_avg or 3), 1)
})
return heatmap_data
@router.get("/stats/escalations", response_model=list[dict])
async def get_escalation_alerts(
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
"""
Returns a list of currently escalated issues with details.
"""
query = (
select(Issue, Escalation)
.join(Escalation, Issue.id == Escalation.issue_id)
.where(Issue.state == "escalated")
.order_by(Escalation.created_at.desc())
)
result = await db.execute(query)
rows = result.all()
alerts = []
for issue, esc in rows:
alerts.append({
"issue_id": issue.id,
"category": issue.classification.primary_category if issue.classification else "Unknown",
"priority": issue.priority,
"escalated_at": esc.created_at,
"level": esc.to_level,
"reason": esc.reason,
"city": issue.city,
"locality": issue.locality
})
class ManualReviewRequest(BaseModel):
status: str
reason: Optional[str] = None
class AdminIssueListItem(BaseModel):
id: UUID
description: Optional[str]
state: str
priority: Optional[int]
city: Optional[str]
created_at: datetime
updated_at: datetime
department: Optional[str]
assigned_to: Optional[str]
category: Optional[str]
sla_deadline: Optional[datetime]
thumbnail: Optional[str]
class Config:
from_attributes = True
def issue_to_response(issue: Issue) -> IssueResponse:
image_urls = []
annotated_urls = []
for img in issue.images:
image_urls.append(get_upload_url(img.file_path))
if img.annotated_path:
annotated_urls.append(get_upload_url(img.annotated_path))
proof_image_url = None
if issue.proof_image_path:
proof_image_url = get_upload_url(issue.proof_image_path)
return IssueResponse(
id=issue.id,
description=issue.description,
latitude=issue.latitude,
longitude=issue.longitude,
state=IssueState(issue.state),
priority=issue.priority,
category=issue.classification.primary_category if issue.classification else None,
confidence=issue.classification.primary_confidence if issue.classification else None,
image_urls=image_urls,
annotated_urls=annotated_urls,
proof_image_url=proof_image_url,
validation_source=issue.validation_source,
is_duplicate=issue.is_duplicate,
parent_issue_id=issue.parent_issue_id,
city=issue.city,
locality=issue.locality,
full_address=issue.full_address,
sla_hours=issue.sla_hours,
sla_deadline=issue.sla_deadline,
created_at=issue.created_at,
updated_at=issue.updated_at,
)
@router.get("/issues", response_model=dict)
async def list_admin_issues(
page: int = Query(1, ge=1),
limit: int = Query(20, ge=1, le=100),
status: Optional[str] = None,
priority: Optional[int] = None,
department_id: Optional[UUID] = None,
worker_id: Optional[UUID] = None,
search: Optional[str] = None,
sort_by: str = "created_at",
sort_order: str = "desc",
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
query = (
select(Issue)
.options(
selectinload(Issue.department),
selectinload(Issue.assigned_member),
selectinload(Issue.classification),
selectinload(Issue.images)
)
)
if status:
statuses = status.split(",")
query = query.where(Issue.state.in_(statuses))
if priority is not None:
query = query.where(Issue.priority == priority)
if department_id:
query = query.where(Issue.department_id == department_id)
if worker_id:
query = query.where(Issue.assigned_member_id == worker_id)
if search:
search_filter = or_(
Issue.description.ilike(f"%{search}%"),
Issue.city.ilike(f"%{search}%"),
Issue.locality.ilike(f"%{search}%"),
Issue.id.cast(String).ilike(f"%{search}%")
)
query = query.where(search_filter)
sort_column = getattr(Issue, sort_by, Issue.created_at)
if sort_order == "asc":
query = query.order_by(asc(sort_column))
else:
query = query.order_by(desc(sort_column))
total_query = select(func.count()).select_from(query.subquery())
total_result = await db.execute(total_query)
total = total_result.scalar_one()
query = query.offset((page - 1) * limit).limit(limit)
result = await db.execute(query)
issues = result.scalars().all()
items = []
for issue in issues:
thumb = None
if issue.images and len(issue.images) > 0:
thumb = get_upload_url(issue.images[0].file_path)
items.append(AdminIssueListItem(
id=issue.id,
description=issue.description,
state=issue.state,
priority=issue.priority,
city=issue.city,
created_at=issue.created_at,
updated_at=issue.updated_at,
department=issue.department.name if issue.department else None,
assigned_to=issue.assigned_member.name if issue.assigned_member else None,
category=issue.classification.primary_category if issue.classification else None,
sla_deadline=issue.sla_deadline,
thumbnail=thumb
))
return {
"items": items,
"total": total,
"page": page,
"limit": limit,
"pages": (total + limit - 1) // limit
}
@router.get("/issues/{issue_id}/details")
async def get_admin_issue_details(
issue_id: UUID,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
query = (
select(Issue)
.options(
selectinload(Issue.department),
selectinload(Issue.classification),
selectinload(Issue.images),
selectinload(Issue.events),
selectinload(Issue.duplicates)
)
.where(Issue.id == issue_id)
)
result = await db.execute(query)
issue = result.scalar_one_or_none()
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
worker = None
if issue.assigned_member_id:
worker = await db.get(Member, issue.assigned_member_id)
return {
"issue": issue_to_response(issue),
"department": {
"id": issue.department.id,
"name": issue.department.name
} if issue.department else None,
"worker": {
"id": worker.id,
"name": worker.name,
"email": worker.email,
"workload": worker.current_workload
} if worker else None,
"events": [
{
"id": e.id,
"type": e.event_type,
"agent": e.agent_name,
"data": e.event_data,
"created_at": e.created_at
} for e in sorted(issue.events, key=lambda x: x.created_at, reverse=True)
],
"duplicates": [
{
"id": d.id,
"created_at": d.created_at,
"status": d.state
} for d in issue.duplicates
]
}
@router.get("/workers/performance")
async def get_worker_performance(
department_id: Optional[UUID] = None,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_active_user),
):
q = select(Member).where(Member.role == "worker")
if department_id:
q = q.where(Member.department_id == department_id)
res = await db.execute(q)
workers = res.scalars().all()
performance_data = []
for w in workers:
resolved_count = await db.execute(
select(func.count(Issue.id)).where(
Issue.assigned_member_id == w.id,
Issue.state.in_(["resolved", "closed"])
)
)
resolved = resolved_count.scalar() or 0
performance_data.append({
"id": w.id,
"name": w.name,
"active": w.is_active,
"current_load": w.current_workload,
"max_load": w.max_workload,
"resolved_total": resolved,
"efficiency": round(resolved / (max(1, (datetime.utcnow() - w.created_at).days / 7)), 1)
})
return performance_data
@router.patch("/issues/{issue_id}", response_model=IssueResponse)
async def update_issue_details(
issue_id: UUID,
data: dict,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
issue = await db.get(Issue, issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
if "priority" in data:
issue.priority = data["priority"]
if "assigned_member_id" in data:
new_worker_id = data["assigned_member_id"]
if new_worker_id:
worker = await db.get(Member, UUID(new_worker_id))
if not worker:
raise HTTPException(status_code=400, detail="Worker not found")
issue.assigned_member_id = worker.id
issue.state = "assigned"
worker.current_workload += 1
else:
issue.assigned_member_id = None
await db.commit()
await db.refresh(issue)
return issue_to_response(issue)
class ResolutionReviewRequest(BaseModel):
action: str
comment: Optional[str] = None
@router.post("/issues/{issue_id}/approve_resolution")
async def approve_resolution(
issue_id: UUID,
data: ResolutionReviewRequest,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
issue = await db.get(Issue, issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
if issue.state != "pending_verification":
raise HTTPException(status_code=400, detail="Issue is not pending verification.")
if data.action == "approve":
issue.state = "resolved"
issue.completed_at = datetime.utcnow()
if data.comment:
issue.resolution_notes = (issue.resolution_notes or "") + f"\nAdmin Note: {data.comment}"
if issue.assigned_member_id:
worker = await db.get(Member, issue.assigned_member_id)
if worker and worker.current_workload > 0:
worker.current_workload -= 1
await db.commit()
return {"message": "Issue resolution approved and marked as resolved."}
elif data.action == "reject":
issue.state = "in_progress"
if data.comment:
issue.resolution_notes = (issue.resolution_notes or "") + f"\n[REJECTED]: {data.comment}"
await db.commit()
return {"message": "Issue resolution rejected. Sent back to worker."}
else:
raise HTTPException(status_code=400, detail="Invalid action.")
@router.post("/issues/{issue_id}/review")
async def review_issue(
issue_id: UUID,
data: ManualReviewRequest,
db: AsyncSession = Depends(get_db),
current_user: Member = Depends(get_current_admin),
):
"""
Manually review an issue.
- If REJECTED: Mark as rejected.
- If APPROVED: Mark as assigned and auto-assign to a worker.
"""
issue = await db.get(Issue, issue_id)
if not issue:
raise HTTPException(status_code=404, detail="Issue not found")
if data.status == "rejected":
issue.state = "rejected"
issue.resolution_notes = data.reason or "Rejected during manual review."
await db.commit()
return {"message": "Issue rejected successfully"}
elif data.status == "approved":
query = select(Member).where(Member.role == "worker", Member.is_active == True).order_by(Member.current_workload.asc())
if issue.department_id:
query = query.where(Member.department_id == issue.department_id)
result = await db.execute(query)
Workers = result.scalars().all()
selected_worker = None
if not Workers:
issue.state = "verified"
issue.resolution_notes = "Verified but no workers available for auto-assignment."
else:
selected_worker = Workers[0]
issue.assigned_member_id = selected_worker.id
issue.state = "assigned"
selected_worker.current_workload += 1
db.add(selected_worker)
await db.commit()
return {
"message": f"Issue approved. {'Assigned to ' + selected_worker.name if selected_worker else 'No worker available, queued as verified.'}",
"assigned_to": str(selected_worker.id) if selected_worker else None
}
else:
raise HTTPException(status_code=400, detail="Invalid status. Use 'approved' or 'rejected'.")
|