2026-04-17 11:32:17 +08:00

48 lines
1.3 KiB
TypeScript

import { NextResponse } from "next/server";
import { createFamilyMessageFromCaregiver } from "@/lib/monitor-data";
import { getOrCreateCaregiverSession } from "@/lib/session";
export async function POST(request: Request) {
const body = (await request.json().catch(() => null)) as
| {
deviceUuid?: string;
content?: string;
importance?: string;
}
| null;
if (!body?.deviceUuid || typeof body.deviceUuid !== "string") {
return NextResponse.json(
{ error: "请先提供目标老人设备码。" },
{ status: 400 },
);
}
if (!body.content || typeof body.content !== "string" || !body.content.trim()) {
return NextResponse.json(
{ error: "留言内容不能为空。" },
{ status: 400 },
);
}
try {
const caregiver = await getOrCreateCaregiverSession();
const message = await createFamilyMessageFromCaregiver({
caregiverId: caregiver.id,
rawDeviceValue: body.deviceUuid,
content: body.content,
importance: typeof body.importance === "string" ? body.importance : undefined,
});
return NextResponse.json({ ok: true, messageId: message.id });
} catch (error) {
return NextResponse.json(
{
error:
error instanceof Error ? error.message : "留言发送失败,请稍后再试。",
},
{ status: 400 },
);
}
}