Files
tk-factory-services/system3-nonconformance/api/utils/tkuser_client.py
Hyungi Ahn 733bb0cb35 feat: tkuser 통합 관리 서비스 + 전체 시스템 SSO 쿠키 인증 통합
- tkuser 서비스 신규 추가 (API + Web)
  - 사용자/권한/프로젝트/부서/작업자/작업장/설비/작업/휴가 통합 관리
  - 작업장 탭: 공장→작업장 드릴다운 네비게이션 + 구역지도 클릭 연동
  - 작업 탭: 공정(work_types)→작업(tasks) 계층 관리
  - 휴가 탭: 유형 관리 + 연차 배정(근로기준법 자동계산)
- 전 시스템 SSO 쿠키 인증으로 통합 (.technicalkorea.net 공유)
- System 2: 작업 이슈 리포트 기능 강화
- System 3: tkuser API 연동, 페이지 권한 체계 적용
- docker-compose에 tkuser-api, tkuser-web 서비스 추가
- ARCHITECTURE.md, DEPLOYMENT.md 문서 작성

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 13:45:52 +09:00

107 lines
4.4 KiB
Python

import httpx
import os
from fastapi import HTTPException, Request
TKUSER_API_URL = os.getenv("TKUSER_API_URL", "http://tkuser-api:3000")
def get_token_from_request(request: Request) -> str:
"""Request 헤더에서 Bearer 토큰 추출"""
auth = request.headers.get("Authorization", "")
if auth.startswith("Bearer "):
return auth[7:]
return request.cookies.get("sso_token", "")
def _headers(token: str) -> dict:
if not token:
raise HTTPException(status_code=401, detail="인증 토큰이 필요합니다.")
return {"Authorization": f"Bearer {token}"}
def _map_project(data: dict) -> dict:
"""tkuser API 응답을 S3 프론트엔드 형식으로 매핑 (project_id → id)"""
return {
"id": data.get("project_id"),
"job_no": data.get("job_no"),
"project_name": data.get("project_name"),
"is_active": data.get("is_active", True),
"created_at": data.get("created_at"),
}
async def get_projects(token: str, active_only: bool = True) -> list:
"""프로젝트 목록 조회"""
endpoint = "/api/projects/active" if active_only else "/api/projects"
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.get(f"{TKUSER_API_URL}{endpoint}", headers=_headers(token))
if resp.status_code != 200:
raise HTTPException(status_code=resp.status_code, detail="프로젝트 목록 조회 실패")
body = resp.json()
if not body.get("success"):
raise HTTPException(status_code=500, detail=body.get("error", "알 수 없는 오류"))
return [_map_project(p) for p in body.get("data", [])]
async def get_project(token: str, project_id: int) -> dict | None:
"""특정 프로젝트 조회. 없으면 None 반환"""
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.get(
f"{TKUSER_API_URL}/api/projects/{project_id}", headers=_headers(token)
)
if resp.status_code == 404:
return None
if resp.status_code != 200:
raise HTTPException(status_code=resp.status_code, detail="프로젝트 조회 실패")
body = resp.json()
if not body.get("success"):
raise HTTPException(status_code=500, detail=body.get("error", "알 수 없는 오류"))
return _map_project(body.get("data"))
async def create_project(token: str, data: dict) -> dict:
"""프로젝트 생성"""
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.post(
f"{TKUSER_API_URL}/api/projects", headers=_headers(token), json=data
)
if resp.status_code == 409:
raise HTTPException(status_code=400, detail="이미 존재하는 Job No.입니다.")
if resp.status_code not in (200, 201):
raise HTTPException(status_code=resp.status_code, detail="프로젝트 생성 실패")
body = resp.json()
if not body.get("success"):
raise HTTPException(status_code=500, detail=body.get("error", "알 수 없는 오류"))
return _map_project(body.get("data"))
async def update_project(token: str, project_id: int, data: dict) -> dict:
"""프로젝트 수정"""
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.put(
f"{TKUSER_API_URL}/api/projects/{project_id}",
headers=_headers(token),
json=data,
)
if resp.status_code == 404:
raise HTTPException(status_code=404, detail="프로젝트를 찾을 수 없습니다.")
if resp.status_code != 200:
raise HTTPException(status_code=resp.status_code, detail="프로젝트 수정 실패")
body = resp.json()
if not body.get("success"):
raise HTTPException(status_code=500, detail=body.get("error", "알 수 없는 오류"))
return _map_project(body.get("data"))
async def delete_project(token: str, project_id: int) -> dict:
"""프로젝트 삭제 (비활성화)"""
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.delete(
f"{TKUSER_API_URL}/api/projects/{project_id}", headers=_headers(token)
)
if resp.status_code == 404:
raise HTTPException(status_code=404, detail="프로젝트를 찾을 수 없습니다.")
if resp.status_code != 200:
raise HTTPException(status_code=resp.status_code, detail="프로젝트 삭제 실패")
return resp.json()