"""N01 to N05: /me/settings, /me/consent, /me/exports.""" from typing import Annotated from fastapi import APIRouter, Header, Response from app.core.dependencies import CurrentAccount, Db from app.modules.idempotency.service import run_idempotent from app.modules.settings.schemas import ( ConsentIn, ExportEnvelope, ExportIn, SettingsEnvelope, SettingsPatch, ) from app.modules.settings.service import SettingsService router = APIRouter(prefix="/v1", tags=["settings"]) IdempotencyKey = Annotated[str | None, Header(max_length=128)] @router.get("/me/settings", response_model=SettingsEnvelope) async def read_settings(account: CurrentAccount, db: Db) -> SettingsEnvelope: return SettingsEnvelope(settings=await SettingsService(db).get(account)) @router.patch("/me/settings", response_model=SettingsEnvelope) async def update_settings(body: SettingsPatch, account: CurrentAccount, db: Db) -> SettingsEnvelope: return SettingsEnvelope(settings=await SettingsService(db).update(account, body)) @router.post("/me/consent", status_code=201) async def accept_consent(body: ConsentIn, account: CurrentAccount, db: Db, idempotency_key: IdempotencyKey = None): service = SettingsService(db) return await run_idempotent(db, account.id, idempotency_key, body, lambda: service.accept(account, body), status_code=201) @router.delete("/me/consent", status_code=204) async def withdraw_consent(account: CurrentAccount, db: Db) -> Response: await SettingsService(db).withdraw(account) return Response(status_code=204) @router.post("/me/exports", status_code=202, response_model=ExportEnvelope) async def request_export(body: ExportIn, account: CurrentAccount, db: Db, idempotency_key: IdempotencyKey = None): service = SettingsService(db) return await run_idempotent(db, account.id, idempotency_key, body, lambda: service.request_export(account), status_code=202)