feat(M06): Add Department and UserDepartment models, seed 12 DMH departments, and enable department-level permissions

This commit is contained in:
Atharv1705
2026-11-27 15:07:34 +05:30
parent a79e21aad6
commit c7b3ead367
21 changed files with 1192 additions and 18 deletions
+146
View File
@@ -0,0 +1,146 @@
from typing import List, Optional
from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session
from app.database import get_db
from app.api.deps import get_current_user, require_super_admin
from app.models.user import User
from app.schemas.department import (
DepartmentCreate, DepartmentUpdate, DepartmentResponse,
DepartmentWithMembersResponse, DepartmentMemberResponse,
UserDepartmentCreate, UserDepartmentResponse
)
from app.services import department_service
router = APIRouter(prefix="/departments", tags=["Departments"])
@router.get("", response_model=List[DepartmentResponse])
def get_departments(
is_active: Optional[bool] = Query(None, description="Filter by active status"),
search: Optional[str] = Query(None, description="Search by name or code"),
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Lists all hospital departments with active member counts."""
departments = department_service.list_departments(db, is_active=is_active, search=search)
results = []
for d in departments:
resp = DepartmentResponse(
id=d.id,
code=d.code,
name=d.name,
description=d.description,
is_active=d.is_active,
member_count=len(d.members),
created_at=d.created_at,
updated_at=d.updated_at
)
results.append(resp)
return results
@router.get("/{department_id}", response_model=DepartmentWithMembersResponse)
def get_department_detail(
department_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
"""Retrieves department details along with its member user list."""
dept = department_service.get_department_by_id(db, department_id)
members_data = []
for m in dept.members:
if m.user:
members_data.append(DepartmentMemberResponse(
user_id=m.user.id,
username=m.user.username,
full_name=m.user.full_name,
email=m.user.email,
role=m.user.role,
is_head=m.is_head,
joined_at=m.created_at
))
return DepartmentWithMembersResponse(
id=dept.id,
code=dept.code,
name=dept.name,
description=dept.description,
is_active=dept.is_active,
member_count=len(members_data),
created_at=dept.created_at,
updated_at=dept.updated_at,
members=members_data
)
@router.post("", response_model=DepartmentResponse, status_code=status.HTTP_201_CREATED)
def create_department(
dept_in: DepartmentCreate,
db: Session = Depends(get_db),
current_user: User = Depends(require_super_admin)
):
"""Super Admin: registers a new hospital department."""
dept = department_service.create_department(db, dept_in)
return DepartmentResponse(
id=dept.id,
code=dept.code,
name=dept.name,
description=dept.description,
is_active=dept.is_active,
member_count=0,
created_at=dept.created_at,
updated_at=dept.updated_at
)
@router.put("/{department_id}", response_model=DepartmentResponse)
def update_department(
department_id: int,
dept_in: DepartmentUpdate,
db: Session = Depends(get_db),
current_user: User = Depends(require_super_admin)
):
"""Super Admin: updates department metadata or active status."""
dept = department_service.update_department(db, department_id, dept_in)
return DepartmentResponse(
id=dept.id,
code=dept.code,
name=dept.name,
description=dept.description,
is_active=dept.is_active,
member_count=len(dept.members),
created_at=dept.created_at,
updated_at=dept.updated_at
)
@router.post("/{department_id}/members", response_model=UserDepartmentResponse, status_code=status.HTTP_201_CREATED)
def add_department_member(
department_id: int,
member_in: UserDepartmentCreate,
db: Session = Depends(get_db),
current_user: User = Depends(require_super_admin)
):
"""Super Admin: assigns a user to a department (optional is_head designation)."""
record = department_service.add_user_to_department(
db, dept_id=department_id, user_id=member_in.user_id, is_head=member_in.is_head
)
return UserDepartmentResponse(
id=record.id,
user_id=record.user_id,
department_id=record.department_id,
is_head=record.is_head,
created_at=record.created_at
)
@router.delete("/{department_id}/members/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
def remove_department_member(
department_id: int,
user_id: int,
db: Session = Depends(get_db),
current_user: User = Depends(require_super_admin)
):
"""Super Admin: removes a user from a department."""
department_service.remove_user_from_department(db, dept_id=department_id, user_id=user_id)
return None