Files

140 lines
4.5 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import Session
from sqlalchemy import func
from app.models.department import Department, UserDepartment
from app.models.user import User
from app.schemas.department import DepartmentCreate, DepartmentUpdate
from app.core.exceptions import NotFoundException, ConflictException, ValidationException
def list_departments(
db: Session,
is_active: Optional[bool] = None,
search: Optional[str] = None
) -> List[Department]:
query = db.query(Department)
if is_active is not None:
query = query.filter(Department.is_active == is_active)
if search:
term = f"%{search.strip()}%"
query = query.filter((Department.name.ilike(term)) | (Department.code.ilike(term)))
return query.order_by(Department.name.asc()).all()
def get_department_by_id(db: Session, dept_id: int) -> Department:
dept = db.query(Department).filter(Department.id == dept_id).first()
if not dept:
raise NotFoundException(f"Department with ID {dept_id} not found")
return dept
def get_department_by_code(db: Session, code: str) -> Optional[Department]:
return db.query(Department).filter(func.upper(Department.code) == code.strip().upper()).first()
def create_department(db: Session, dept_in: DepartmentCreate) -> Department:
clean_code = dept_in.code.strip().upper()
existing_code = get_department_by_code(db, clean_code)
if existing_code:
raise ConflictException(f"Department with code '{clean_code}' already exists")
existing_name = db.query(Department).filter(Department.name.ilike(dept_in.name.strip())).first()
if existing_name:
raise ConflictException(f"Department with name '{dept_in.name.strip()}' already exists")
dept = Department(
code=clean_code,
name=dept_in.name.strip(),
description=dept_in.description.strip() if dept_in.description else None,
is_active=True
)
db.add(dept)
db.commit()
db.refresh(dept)
return dept
def update_department(db: Session, dept_id: int, dept_in: DepartmentUpdate) -> Department:
dept = get_department_by_id(db, dept_id)
if dept_in.code is not None:
clean_code = dept_in.code.strip().upper()
existing = db.query(Department).filter(
func.upper(Department.code) == clean_code,
Department.id != dept_id
).first()
if existing:
raise ConflictException(f"Department with code '{clean_code}' already exists")
dept.code = clean_code
if dept_in.name is not None:
clean_name = dept_in.name.strip()
existing = db.query(Department).filter(
Department.name.ilike(clean_name),
Department.id != dept_id
).first()
if existing:
raise ConflictException(f"Department with name '{clean_name}' already exists")
dept.name = clean_name
if dept_in.description is not None:
dept.description = dept_in.description.strip() if dept_in.description else None
if dept_in.is_active is not None:
dept.is_active = dept_in.is_active
db.commit()
db.refresh(dept)
return dept
def add_user_to_department(
db: Session,
dept_id: int,
user_id: int,
is_head: bool = False
) -> UserDepartment:
dept = get_department_by_id(db, dept_id)
user = db.query(User).filter(User.id == user_id).first()
if not user:
raise NotFoundException(f"User with ID {user_id} not found")
existing = db.query(UserDepartment).filter(
UserDepartment.department_id == dept_id,
UserDepartment.user_id == user_id
).first()
if existing:
existing.is_head = is_head
db.commit()
db.refresh(existing)
return existing
member = UserDepartment(
user_id=user_id,
department_id=dept_id,
is_head=is_head
)
db.add(member)
db.commit()
db.refresh(member)
return member
def remove_user_from_department(db: Session, dept_id: int, user_id: int) -> bool:
member = db.query(UserDepartment).filter(
UserDepartment.department_id == dept_id,
UserDepartment.user_id == user_id
).first()
if not member:
raise NotFoundException(f"User {user_id} is not a member of department {dept_id}")
db.delete(member)
db.commit()
return True
def get_user_department_ids(db: Session, user_id: int) -> List[int]:
memberships = db.query(UserDepartment.department_id).filter(UserDepartment.user_id == user_id).all()
return [m[0] for m in memberships]