feat: add structured message forwarding payloads
This commit is contained in:
@@ -2,6 +2,18 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
import { requireRequestSession } from "@/lib/boss-auth";
|
||||
import { forwardProjectMessage } from "@/lib/boss-data";
|
||||
|
||||
type ForwardBody =
|
||||
| {
|
||||
mode?: "single";
|
||||
targetProjectId?: string;
|
||||
sourceMessageId?: string;
|
||||
}
|
||||
| {
|
||||
mode?: "bundle";
|
||||
targetProjectId?: string;
|
||||
sourceMessageIds?: string[];
|
||||
};
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
context: { params: Promise<{ projectId: string }> },
|
||||
@@ -11,25 +23,60 @@ export async function POST(
|
||||
return NextResponse.json({ ok: false, message: "UNAUTHORIZED" }, { status: 401 });
|
||||
}
|
||||
const { projectId } = await context.params;
|
||||
const body = (await request.json()) as {
|
||||
targetProjectId?: string;
|
||||
note?: string;
|
||||
};
|
||||
const body = (await request.json()) as ForwardBody;
|
||||
|
||||
if (!body.targetProjectId || !body.note) {
|
||||
const mode = body.mode ?? "single";
|
||||
const targetProjectId = body.targetProjectId;
|
||||
const sourceMessageId: string | undefined =
|
||||
"sourceMessageId" in body ? body.sourceMessageId : undefined;
|
||||
const sourceMessageIds: string[] =
|
||||
"sourceMessageIds" in body && Array.isArray(body.sourceMessageIds)
|
||||
? body.sourceMessageIds
|
||||
: [];
|
||||
|
||||
if (!targetProjectId) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: "缺少 targetProjectId 或 note" },
|
||||
{ ok: false, message: "缺少 targetProjectId" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
if (mode === "bundle") {
|
||||
if (sourceMessageIds.length <= 1) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: "bundle 转发至少需要 2 条 sourceMessageIds" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
} else if (!sourceMessageId) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: "single 转发缺少 sourceMessageId" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const message = await forwardProjectMessage({
|
||||
sourceProjectId: projectId,
|
||||
targetProjectId: body.targetProjectId,
|
||||
note: body.note,
|
||||
const result =
|
||||
mode === "bundle"
|
||||
? await forwardProjectMessage({
|
||||
sourceProjectId: projectId,
|
||||
mode: "bundle",
|
||||
targetProjectId,
|
||||
sourceMessageIds,
|
||||
requestedBy: session.account,
|
||||
})
|
||||
: await forwardProjectMessage({
|
||||
sourceProjectId: projectId,
|
||||
mode: "single",
|
||||
targetProjectId,
|
||||
sourceMessageId: sourceMessageId ?? "",
|
||||
requestedBy: session.account,
|
||||
});
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
message: result.message ?? null,
|
||||
approvalRequired: Boolean(result.approvalRequired),
|
||||
approvalReason: result.approvalReason ?? null,
|
||||
});
|
||||
return NextResponse.json({ ok: true, message });
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, message: error instanceof Error ? error.message : "UNKNOWN_ERROR" },
|
||||
|
||||
Reference in New Issue
Block a user