전체적인 리팩토링
This commit is contained in:
25
app/api/autotrade/sessions/active/route.ts
Normal file
25
app/api/autotrade/sessions/active/route.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import {
|
||||
AUTOTRADE_API_ERROR_CODE,
|
||||
createAutotradeErrorResponse,
|
||||
getAutotradeSession,
|
||||
getAutotradeUserId,
|
||||
} from "@/app/api/autotrade/_shared";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const userId = await getAutotradeUserId(request.headers);
|
||||
if (!userId) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 401,
|
||||
code: AUTOTRADE_API_ERROR_CODE.AUTH_REQUIRED,
|
||||
message: "로그인이 필요합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const session = getAutotradeSession(userId);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
session: session && session.runtimeState === "RUNNING" ? session : null,
|
||||
});
|
||||
}
|
||||
73
app/api/autotrade/sessions/heartbeat/route.ts
Normal file
73
app/api/autotrade/sessions/heartbeat/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
AUTOTRADE_API_ERROR_CODE,
|
||||
createAutotradeErrorResponse,
|
||||
getAutotradeSession,
|
||||
getAutotradeUserId,
|
||||
readJsonBody,
|
||||
sanitizeAutotradeError,
|
||||
upsertAutotradeSession,
|
||||
} from "@/app/api/autotrade/_shared";
|
||||
|
||||
const heartbeatRequestSchema = z.object({
|
||||
sessionId: z.string().uuid(),
|
||||
leaderTabId: z.string().trim().min(1).max(100).optional(),
|
||||
});
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const userId = await getAutotradeUserId(request.headers);
|
||||
if (!userId) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 401,
|
||||
code: AUTOTRADE_API_ERROR_CODE.AUTH_REQUIRED,
|
||||
message: "로그인이 필요합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const rawBody = await readJsonBody(request);
|
||||
const parsed = heartbeatRequestSchema.safeParse(rawBody);
|
||||
if (!parsed.success) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 400,
|
||||
code: AUTOTRADE_API_ERROR_CODE.INVALID_REQUEST,
|
||||
message: parsed.error.issues[0]?.message ?? "heartbeat 요청값이 올바르지 않습니다.",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const session = getAutotradeSession(userId);
|
||||
if (!session || session.runtimeState !== "RUNNING") {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 404,
|
||||
code: AUTOTRADE_API_ERROR_CODE.SESSION_NOT_FOUND,
|
||||
message: "실행 중인 자동매매 세션이 없습니다.",
|
||||
});
|
||||
}
|
||||
|
||||
if (session.sessionId !== parsed.data.sessionId) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 409,
|
||||
code: AUTOTRADE_API_ERROR_CODE.CONFLICT,
|
||||
message: "세션 식별자가 일치하지 않습니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const updated = upsertAutotradeSession({
|
||||
...session,
|
||||
lastHeartbeatAt: new Date().toISOString(),
|
||||
leaderTabId: parsed.data.leaderTabId ?? session.leaderTabId,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
session: updated,
|
||||
});
|
||||
} catch (error) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 500,
|
||||
code: AUTOTRADE_API_ERROR_CODE.INTERNAL,
|
||||
message: sanitizeAutotradeError(error, "heartbeat 처리 중 오류가 발생했습니다."),
|
||||
});
|
||||
}
|
||||
}
|
||||
77
app/api/autotrade/sessions/start/route.ts
Normal file
77
app/api/autotrade/sessions/start/route.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
AUTOTRADE_API_ERROR_CODE,
|
||||
createAutotradeErrorResponse,
|
||||
getAutotradeUserId,
|
||||
hasAutotradeKisRuntimeHeaders,
|
||||
readJsonBody,
|
||||
sanitizeAutotradeError,
|
||||
upsertAutotradeSession,
|
||||
} from "@/app/api/autotrade/_shared";
|
||||
|
||||
const startRequestSchema = z.object({
|
||||
symbol: z.string().trim().regex(/^\d{6}$/),
|
||||
leaderTabId: z.string().trim().min(1).max(100),
|
||||
effectiveAllocationAmount: z.number().int().positive(),
|
||||
effectiveDailyLossLimit: z.number().int().positive(),
|
||||
strategySummary: z.string().trim().min(1).max(320),
|
||||
});
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const userId = await getAutotradeUserId(request.headers);
|
||||
if (!userId) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 401,
|
||||
code: AUTOTRADE_API_ERROR_CODE.AUTH_REQUIRED,
|
||||
message: "로그인이 필요합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
if (!hasAutotradeKisRuntimeHeaders(request.headers)) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 400,
|
||||
code: AUTOTRADE_API_ERROR_CODE.CREDENTIAL_REQUIRED,
|
||||
message: "자동매매 시작에는 KIS 인증 헤더가 필요합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const rawBody = await readJsonBody(request);
|
||||
const parsed = startRequestSchema.safeParse(rawBody);
|
||||
if (!parsed.success) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 400,
|
||||
code: AUTOTRADE_API_ERROR_CODE.INVALID_REQUEST,
|
||||
message: parsed.error.issues[0]?.message ?? "세션 시작 입력값이 올바르지 않습니다.",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const now = new Date().toISOString();
|
||||
const session = upsertAutotradeSession({
|
||||
userId,
|
||||
sessionId: crypto.randomUUID(),
|
||||
symbol: parsed.data.symbol,
|
||||
runtimeState: "RUNNING",
|
||||
leaderTabId: parsed.data.leaderTabId,
|
||||
startedAt: now,
|
||||
lastHeartbeatAt: now,
|
||||
endedAt: null,
|
||||
stopReason: null,
|
||||
effectiveAllocationAmount: parsed.data.effectiveAllocationAmount,
|
||||
effectiveDailyLossLimit: parsed.data.effectiveDailyLossLimit,
|
||||
strategySummary: parsed.data.strategySummary,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
session,
|
||||
});
|
||||
} catch (error) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 500,
|
||||
code: AUTOTRADE_API_ERROR_CODE.INTERNAL,
|
||||
message: sanitizeAutotradeError(error, "자동매매 세션 시작 중 오류가 발생했습니다."),
|
||||
});
|
||||
}
|
||||
}
|
||||
78
app/api/autotrade/sessions/stop/route.ts
Normal file
78
app/api/autotrade/sessions/stop/route.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
AUTOTRADE_API_ERROR_CODE,
|
||||
createAutotradeErrorResponse,
|
||||
getAutotradeSession,
|
||||
getAutotradeUserId,
|
||||
readJsonBody,
|
||||
sanitizeAutotradeError,
|
||||
stopAutotradeSession,
|
||||
} from "@/app/api/autotrade/_shared";
|
||||
import type { AutotradeStopReason } from "@/features/autotrade/types/autotrade.types";
|
||||
|
||||
const stopRequestSchema = z.object({
|
||||
sessionId: z.string().uuid().optional(),
|
||||
reason: z
|
||||
.enum([
|
||||
"browser_exit",
|
||||
"external_leave",
|
||||
"manual",
|
||||
"emergency",
|
||||
"heartbeat_timeout",
|
||||
])
|
||||
.optional(),
|
||||
});
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const userId = await getAutotradeUserId(request.headers);
|
||||
if (!userId) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 401,
|
||||
code: AUTOTRADE_API_ERROR_CODE.AUTH_REQUIRED,
|
||||
message: "로그인이 필요합니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const rawBody = await readJsonBody(request);
|
||||
const parsed = stopRequestSchema.safeParse(rawBody ?? {});
|
||||
if (!parsed.success) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 400,
|
||||
code: AUTOTRADE_API_ERROR_CODE.INVALID_REQUEST,
|
||||
message: parsed.error.issues[0]?.message ?? "세션 종료 입력값이 올바르지 않습니다.",
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const session = getAutotradeSession(userId);
|
||||
if (!session) {
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
session: null,
|
||||
});
|
||||
}
|
||||
|
||||
if (parsed.data.sessionId && parsed.data.sessionId !== session.sessionId) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 409,
|
||||
code: AUTOTRADE_API_ERROR_CODE.CONFLICT,
|
||||
message: "세션 식별자가 일치하지 않습니다.",
|
||||
});
|
||||
}
|
||||
|
||||
const reason: AutotradeStopReason = parsed.data.reason ?? "manual";
|
||||
const stopped = stopAutotradeSession(userId, reason);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
session: stopped,
|
||||
});
|
||||
} catch (error) {
|
||||
return createAutotradeErrorResponse({
|
||||
status: 500,
|
||||
code: AUTOTRADE_API_ERROR_CODE.INTERNAL,
|
||||
message: sanitizeAutotradeError(error, "세션 종료 중 오류가 발생했습니다."),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user