File size: 1,335 Bytes
aad7814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Schema inspection and optional manual updates."""

from __future__ import annotations

from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel, Field

from backend.api.deps import get_current_tenant
from backend.core import template_discoverer

router = APIRouter(prefix="/api", tags=["schema"])


class SchemaPatch(BaseModel):
    """Partial schema update (operator corrections)."""

    section_alias_map: dict[str, str] | None = None
    version: int | None = None


@router.get("/schema")
def get_schema(tenant_id: str = Depends(get_current_tenant)) -> dict:
    """Return the discovered template schema (sections, order, rating system)."""
    schema = template_discoverer.ensure_canonical_schema(tenant_id)
    return schema.model_dump()


@router.patch("/schema")
def patch_schema(
    patch: SchemaPatch,
    tenant_id: str = Depends(get_current_tenant),
) -> dict:
    """Apply operator corrections to the persisted schema (e.g. alias overrides)."""
    schema = template_discoverer.ensure_canonical_schema(tenant_id)
    if patch.section_alias_map is not None:
        schema.section_alias_map.update(patch.section_alias_map)
    if patch.version is not None:
        schema.version = patch.version
    template_discoverer.save_schema(tenant_id, schema)
    return schema.model_dump()