# 버그 수정: server/main.py get_user_plan() 조직 구독 조회 오류

## 작업 레벨: Lv.1 (단일 함수 버그 수정)

## 문제
`/home/jay/projects/InsuRo/server/main.py`의 `get_user_plan()` 함수(라인 289-383)에서 organization_subscriptions 테이블 조회 시 **존재하지 않는 컬럼 `user_id`로 필터링**하고 있음.

### 현재 코드 (라인 314-322)
```python
org_res = (
    sb.table("organization_subscriptions")
    .select("subscription_plans(name, features, sort_order)")
    .eq("user_id", user_id)   # ← 버그! user_id 컬럼 없음
    .eq("status", "active")
    .limit(1)
    .execute()
)
```

### organization_subscriptions 테이블 실제 컬럼
- id, organization_id, plan_id, status, started_at, expires_at, created_at, updated_at
- **user_id 컬럼 없음!**

### 결과
- 조직 구독 조회 실패 (except Exception: pass로 에러 삼킴)
- user_subscriptions 폴백 → 조직 구독 유저는 개인 구독 없음
- **무료 플랜(sort_order=1)으로 폴백**
- require_plan("맥스"): 1 < 4 → 403 "이 기능은 맥스 플랜 이상에서 사용할 수 있습니다"

### 영향 범위
- 인포키워드 분석 등 require_plan/require_feature를 사용하는 **모든 API 엔드포인트**에서 조직 구독 유저가 차단됨

## 수정 방법

프론트엔드(use-feature-gate.ts)의 올바른 로직을 참조:
1. profiles 테이블에서 user_id로 organization_id 조회
2. organization_subscriptions에서 organization_id로 필터링

### 수정 대상
- 파일: `/home/jay/projects/InsuRo/server/main.py`
- 함수: `get_user_plan()` (라인 289-383)
- 변경 범위: 조직 구독 조회 부분 (라인 314-326)

### 수정 코드 (참고)
```python
# 1. profiles에서 organization_id 조회
profile_res = (
    sb.table("profiles")
    .select("organization_id")
    .eq("id", user_id)
    .maybe_single()
    .execute()
)

if profile_res.data and profile_res.data.get("organization_id"):
    org_id = profile_res.data["organization_id"]
    # 2. organization_subscriptions에서 organization_id로 조회
    org_res = (
        sb.table("organization_subscriptions")
        .select("subscription_plans(name, features, sort_order)")
        .eq("organization_id", org_id)
        .eq("status", "active")
        .limit(1)
        .execute()
    )
    if org_res.data:
        row = dict(org_res.data[0])
        plan_row = row.get("subscription_plans")
```

## 검증 시나리오
1. Hidden 플랜(조직 구독) 유저로 로그인
2. AI 키워드 분석 > 정보성 키워드 탭에서 "암보험 추천" 입력 후 분석하기 클릭
3. **성공 기준**: "이 기능은 맥스 플랜 이상에서 사용할 수 있습니다" 에러 없이 분석이 시작되어야 함
4. 추가 확인: require_plan/require_feature를 사용하는 다른 엔드포인트도 정상 동작 확인

## 주의사항
- 캐시(_plan_cache) TTL 5분 → 수정 후 캐시 무효화 테스트 필요
- except Exception: pass 패턴은 유지하되, 로깅 추가 권장
- 프론트엔드 변경 없음 (서버만 수정)
