File size: 1,382 Bytes
05cb41b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
import json
import os
from app.core.rag import rag_engine

router = APIRouter(tags=["Portal Controls"])

class PolicyUpdatePayload(BaseModel):
    policy_json: dict

# --- User Routes ---
@router.get("/user/{member_id}/wallet")
async def get_wallet_limit(member_id: str):
    """Mocks database retrieval of a user's wallet limit."""
    return {
        "member_id": member_id,
        "total_limit": 50000.00,
        "available_limit": 35000.00 # Example remaining balance
    }

# --- Admin Routes ---
@router.get("/admin/policy")
async def get_current_policy():
    """Fetches the current policy JSON for the admin editor."""
    if not os.path.exists(rag_engine.policy_file):
        raise HTTPException(status_code=404, detail="Policy file not found.")
    
    with open(rag_engine.policy_file, "r") as f:
        return json.load(f)

@router.post("/admin/policy/update")
async def update_policy(payload: PolicyUpdatePayload):
    """Overwrites the policy file and instantly rebuilds the AI's RAG memory."""
    with open(rag_engine.policy_file, "w") as f:
        json.dump(payload.policy_json, f, indent=2)
    
    # CRITICAL: Rebuild the local FAISS vector database
    rag_engine.initialize()
    
    return {"status": "success", "message": "Policy updated. AI memory successfully refreshed."}