diff --git a/app/api/documents.py b/app/api/documents.py index d1f4599..e645065 100644 --- a/app/api/documents.py +++ b/app/api/documents.py @@ -76,6 +76,8 @@ class DocumentResponse(BaseModel): facet_topic: str | None = None facet_year: int | None = None facet_doctype: str | None = None + category: str | None = None + ai_suggestion: dict | None = None extracted_at: datetime | None ai_processed_at: datetime | None embedded_at: datetime | None @@ -93,6 +95,11 @@ class DocumentListResponse(BaseModel): page_size: int +class AcceptSuggestionRequest(BaseModel): + """§1 accept-suggestion 요청 body — stale payload / doc 수정 검출.""" + expected_source_updated_at: datetime + + class DocumentUpdate(BaseModel): title: str | None = None ai_domain: str | None = None @@ -238,7 +245,12 @@ async def list_library_documents( facet_year: int | None = None, facet_doctype: str | None = None, ): - """자료실 문서 목록 (prefix match, title 검색, facet 필터, 정렬)""" + """자료실 문서 목록 (category='library' 기반, prefix match, facet 필터, 정렬) + + §1 재구현: 기존 `user_tags @library/%` 필터 → `category='library'` 필터로 전환. + 백필 정책상 `category='library' ⇔ user_tags has @library/...` 관계가 유지됨. + `path` 지정 시 하위 경로 매칭은 기존처럼 user_tags 기반 유지 (분류 내부 서가 경로). + """ from sqlalchemy import text as sql_text from core.library import LIBRARY_PREFIX, normalize_library_path @@ -252,6 +264,7 @@ async def list_library_documents( query = select(Document).where( Document.deleted_at == None, # noqa: E711 + Document.category == "library", ) if path: @@ -265,15 +278,6 @@ async def list_library_documents( ) """).bindparams(exact=exact, prefix=prefix) ) - else: - query = query.where( - sql_text(""" - EXISTS ( - SELECT 1 FROM jsonb_array_elements_text(documents.user_tags) AS t - WHERE t LIKE '@library/%' - ) - """) - ) if q: query = query.where(Document.title.ilike(f"%{q}%")) @@ -322,14 +326,40 @@ async def list_documents( source: str | None = None, format: str | None = None, review_status: str | None = Query(None, description="pending | approved | rejected"), + category: str | None = Query(None, description="doc_category enum — 지정 시 기본 news/memo 제외 해제"), + has_suggestion: bool | None = Query(None, description="true: ai_suggestion IS NOT NULL"), + proposed_category: str | None = Query(None, description="ai_suggestion.proposed_category 필터"), ): - """문서 목록 조회 (페이지네이션 + 필터, 뉴스/메모 제외)""" + """문서 목록 조회 (페이지네이션 + 필터). + + 기본은 뉴스/메모 제외. `category` 지정 시 해당 카테고리만 반환 (기본 제외 해제). + §2 승인 UI 용: `has_suggestion=true&proposed_category=library` 조합. + """ query = select(Document).where( Document.deleted_at == None, # noqa: E711 - Document.source_channel != "news", - Document.file_type != "note", ) + if category: + # 명시적 카테고리 필터 — 기본 exclude 해제 + query = query.where(Document.category == category) + else: + # 기본 목록: 뉴스/메모 제외 (문서함 용도) + query = query.where( + Document.source_channel != "news", + Document.file_type != "note", + ) + + if has_suggestion is True: + query = query.where(Document.ai_suggestion.isnot(None)) + elif has_suggestion is False: + query = query.where(Document.ai_suggestion.is_(None)) + + if proposed_category: + # ai_suggestion JSONB 의 proposed_category 값 매칭 + query = query.where( + Document.ai_suggestion["proposed_category"].astext == proposed_category + ) + if domain: # prefix 매칭: Industrial_Safety 클릭 시 하위 전부 포함 query = query.where(Document.ai_domain.startswith(domain)) @@ -404,6 +434,8 @@ async def get_document_file( raise HTTPException(status_code=404, detail="파일을 찾을 수 없습니다") # 미디어 타입 매핑 + # HTML5