"""Account preferences: read the effective value, change the own value, bind the team.""" from __future__ import annotations from fastapi import APIRouter, Depends from fastapi.responses import JSONResponse from psycopg import Connection from app.access.policy import audit from app.api.deps import Principal, get_conn, get_principal from app.api.schemas import PreferenceIn from app.preferences import preference_state, store_preference router = APIRouter() _NO_STORE = {"Cache-Control": "no-store"} @router.get("/api/v1/auth/preferences/{key}") def get_preference(key: str, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): return JSONResponse(preference_state(conn, principal.account_id, key), headers=_NO_STORE) @router.put("/api/v1/auth/preferences/{key}") def put_preference( key: str, body: PreferenceIn, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): state = store_preference(conn, principal.account_id, key, body.value, body.team_mode) audit( conn, principal.account_id, None, "preference_changed", "account", principal.account_id, detail={"key": key, "value": body.value, "team_mode": body.team_mode}, ) return JSONResponse(state, headers=_NO_STORE)