添加家人绑定
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
import { markFamilyMessagesAsRead } from "@/lib/monitor-data";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const body = (await request.json().catch(() => null)) as
|
||||
| {
|
||||
deviceUuid?: string;
|
||||
messageIds?: unknown;
|
||||
}
|
||||
| null;
|
||||
|
||||
if (!body?.deviceUuid || typeof body.deviceUuid !== "string") {
|
||||
return NextResponse.json(
|
||||
{ error: "deviceUuid 是必填项。" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const messageIds = Array.isArray(body.messageIds)
|
||||
? body.messageIds.filter(
|
||||
(messageId): messageId is string => typeof messageId === "string",
|
||||
)
|
||||
: undefined;
|
||||
|
||||
try {
|
||||
await markFamilyMessagesAsRead(body.deviceUuid, messageIds);
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
error instanceof Error ? error.message : "更新留言状态失败,请稍后再试。",
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
import { listFamilyMessagesForDevice } from "@/lib/monitor-data";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const rawDeviceValue =
|
||||
request.nextUrl.searchParams.get("deviceUuid") ||
|
||||
request.nextUrl.searchParams.get("device") ||
|
||||
"";
|
||||
const take = Number.parseInt(
|
||||
request.nextUrl.searchParams.get("take") || "12",
|
||||
10,
|
||||
);
|
||||
|
||||
if (!rawDeviceValue) {
|
||||
return NextResponse.json(
|
||||
{ error: "deviceUuid 是必填项。" },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const data = await listFamilyMessagesForDevice(rawDeviceValue, take);
|
||||
|
||||
return NextResponse.json({
|
||||
deviceUuid: data.device.deviceUuid,
|
||||
unreadCount: data.unreadCount,
|
||||
summaryText: data.summaryText,
|
||||
messages: data.messages.map((message) => ({
|
||||
id: message.id,
|
||||
content: message.content,
|
||||
importance: message.importance,
|
||||
status: message.status,
|
||||
createdAt: message.createdAt.toISOString(),
|
||||
deliveredAt: message.deliveredAt?.toISOString() || null,
|
||||
readAt: message.readAt?.toISOString() || null,
|
||||
})),
|
||||
});
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
error instanceof Error ? error.message : "读取家属留言失败,请稍后再试。",
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user