import { NextResponse } from "next/server"; import type { DashboardActivityResponse } from "@/features/dashboard/types/dashboard.types"; import { hasKisConfig, normalizeTradingEnv } from "@/lib/kis/config"; import { getDomesticDashboardActivity } from "@/lib/kis/dashboard"; import { hasKisApiSession } from "@/app/api/kis/_session"; import { readKisAccountParts, readKisCredentialsFromHeaders, } from "@/app/api/kis/domestic/_shared"; /** * @file app/api/kis/domestic/activity/route.ts * @description 국내주식 주문내역/매매일지 조회 API */ /** * 대시보드 하단(주문내역/매매일지) 조회 API * @returns 주문내역 목록 + 매매일지 목록/요약 * @remarks UI 흐름: DashboardContainer -> useDashboardData -> /api/kis/domestic/activity -> ActivitySection 렌더링 * @see features/dashboard/hooks/use-dashboard-data.ts 대시보드 초기 로드/새로고침에서 호출합니다. * @see features/dashboard/components/ActivitySection.tsx 주문내역/매매일지 섹션 렌더링 */ export async function GET(request: Request) { const hasSession = await hasKisApiSession(); if (!hasSession) { return NextResponse.json({ error: "로그인이 필요합니다." }, { status: 401 }); } const credentials = readKisCredentialsFromHeaders(request.headers); if (!hasKisConfig(credentials)) { return NextResponse.json( { error: "KIS API 키 설정이 필요합니다.", }, { status: 400 }, ); } const account = readKisAccountParts(request.headers); if (!account) { return NextResponse.json( { error: "계좌번호가 필요합니다. 설정에서 계좌번호(예: 12345678-01)를 입력해 주세요.", }, { status: 400 }, ); } try { const result = await getDomesticDashboardActivity(account, credentials); const response: DashboardActivityResponse = { source: "kis", tradingEnv: normalizeTradingEnv(credentials.tradingEnv), orders: result.orders, tradeJournal: result.tradeJournal, journalSummary: result.journalSummary, warnings: result.warnings, fetchedAt: new Date().toISOString(), }; return NextResponse.json(response, { headers: { "cache-control": "no-store", }, }); } catch (error) { const message = error instanceof Error ? error.message : "주문내역/매매일지 조회 중 오류가 발생했습니다."; return NextResponse.json({ error: message }, { status: 500 }); } }