60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
import logging
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.database import get_db
|
|
from app.models import BotTrade, Subscription
|
|
from app.schemas import BotTrade as BotTradeSchema, UserResponse
|
|
|
|
router = APIRouter()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _trade_to_schema(trade: BotTrade) -> BotTradeSchema:
|
|
return BotTradeSchema(
|
|
id=trade.id,
|
|
asset=trade.asset,
|
|
side=trade.side,
|
|
entry_price=trade.entry_price,
|
|
exit_price=trade.exit_price or 0.0,
|
|
pnl_usd=trade.pnl_usd or 0.0,
|
|
hold_seconds=trade.hold_seconds or 0,
|
|
trigger_post_id=trade.trigger_post_id or 0,
|
|
opened_at=trade.opened_at.isoformat(),
|
|
closed_at=trade.closed_at.isoformat() if trade.closed_at else "",
|
|
)
|
|
|
|
|
|
@router.get("/user/{wallet}", response_model=UserResponse)
|
|
async def get_user(wallet: str, db: AsyncSession = Depends(get_db)):
|
|
wallet = wallet.lower().strip()
|
|
|
|
result = await db.execute(
|
|
select(Subscription).where(Subscription.wallet_address == wallet)
|
|
)
|
|
sub = result.scalar_one_or_none()
|
|
if sub is None:
|
|
raise HTTPException(status_code=404, detail="Wallet not found")
|
|
|
|
# Personal trades (all — open and closed)
|
|
trades_result = await db.execute(
|
|
select(BotTrade)
|
|
.where(BotTrade.wallet_address == wallet)
|
|
.order_by(BotTrade.opened_at.desc())
|
|
.limit(50)
|
|
)
|
|
trades = trades_result.scalars().all()
|
|
|
|
# Mask hl_api_key: show only last 4 chars if set
|
|
hl_api_key_set = bool(sub.hl_api_key)
|
|
|
|
return UserResponse(
|
|
wallet_address=sub.wallet_address,
|
|
active=sub.active,
|
|
subscribed_at=sub.subscribed_at.isoformat() if sub.subscribed_at else None,
|
|
hl_api_key_set=hl_api_key_set,
|
|
trades=[_trade_to_schema(t) for t in trades],
|
|
)
|