2026-10-16 07:40:29 -06:00
|
|
|
"""Departments and their stations (read-only in phase 1)."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
|
|
|
|
|
|
from .deps import Db, Principal, current_principal, get_db
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api", tags=["departments"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/departments")
|
|
|
|
|
def departments(_: Principal = Depends(current_principal), d: Db = Depends(get_db)) -> list[dict[str, Any]]: # noqa: B008
|
|
|
|
|
conn = d.conn
|
|
|
|
|
rows = conn.execute(
|
2026-10-16 09:31:45 -06:00
|
|
|
"SELECT code, slug, name, kind::text, weld FROM departments WHERE active ORDER BY sort, code"
|
2026-10-16 07:40:29 -06:00
|
|
|
).fetchall()
|
|
|
|
|
stations = conn.execute(
|
2026-10-16 09:31:45 -06:00
|
|
|
"SELECT dept_code, id, name FROM stations WHERE active ORDER BY dept_code, sort, name"
|
2026-10-16 07:40:29 -06:00
|
|
|
).fetchall()
|
2026-10-16 09:31:45 -06:00
|
|
|
by_dept: dict[str, list[dict[str, Any]]] = {}
|
|
|
|
|
for code, sid, name in stations:
|
|
|
|
|
by_dept.setdefault(code, []).append({"id": int(sid), "name": name})
|
|
|
|
|
return [
|
|
|
|
|
{"code": r[0], "slug": r[1], "name": r[2], "kind": r[3], "weld": bool(r[4]), "stations": by_dept.get(r[0], [])}
|
|
|
|
|
for r in rows
|
|
|
|
|
]
|