feat: 已接受契约持久化 — 保存/加载/自动展示
- PATCH /api/blindbox/plan: 接受契约时更新状态为 accepted - GET /api/blindbox/plan: 查询房间内最近一次已接受的计划 - 进入房间时自动加载已接受计划并展示行程视图 - 修复整个周末想法不足时重复活动的问题(不足则只生成一天)
This commit is contained in:
@@ -190,16 +190,18 @@ export const POST = apiHandler(async (req) => {
|
||||
]
|
||||
: [at];
|
||||
|
||||
// Select ideas per day, avoiding duplicates across days when possible
|
||||
// Select ideas per day — skip extra days when ideas run out
|
||||
const dayIdeas: TaggedIdea[][] = [];
|
||||
const usedIds = new Set<string>();
|
||||
for (const dayConfig of dayConfigs) {
|
||||
const remaining = taggedIdeas.filter((i) => !usedIds.has(i.id));
|
||||
const pool = remaining.length >= 2 ? remaining : taggedIdeas;
|
||||
const selected = selectIdeasForSlots(pool, dayConfig.endHour - dayConfig.startHour);
|
||||
if (remaining.length < 2) break;
|
||||
const selected = selectIdeasForSlots(remaining, dayConfig.endHour - dayConfig.startHour);
|
||||
for (const idea of selected) usedIds.add(idea.id);
|
||||
dayIdeas.push(selected);
|
||||
}
|
||||
// Trim to actual days generated (may be fewer than requested for "整个周末")
|
||||
const actualDayConfigs = dayConfigs.slice(0, dayIdeas.length);
|
||||
|
||||
const allSelected = dayIdeas.flat();
|
||||
if (allSelected.length === 0) {
|
||||
@@ -260,7 +262,7 @@ export const POST = apiHandler(async (req) => {
|
||||
|
||||
// Generate schedule for each day (parallel AI calls)
|
||||
const schedules = await Promise.all(
|
||||
dayConfigs.map((dayConfig, idx) => {
|
||||
actualDayConfigs.map((dayConfig, idx) => {
|
||||
const ideas = dayIdeas[idx];
|
||||
const ctx: ScheduleContext = {
|
||||
ideas: ideas.map((i) => ({
|
||||
@@ -282,7 +284,7 @@ export const POST = apiHandler(async (req) => {
|
||||
const days = schedules
|
||||
.map((schedule, idx) =>
|
||||
schedule
|
||||
? { date: dayConfigs[idx].date, items: schedule.items, summary: schedule.summary }
|
||||
? { date: actualDayConfigs[idx].date, items: schedule.items, summary: schedule.summary }
|
||||
: null,
|
||||
)
|
||||
.filter((d) => d !== null);
|
||||
@@ -308,3 +310,47 @@ export const POST = apiHandler(async (req) => {
|
||||
createdAt: plan.createdAt,
|
||||
});
|
||||
});
|
||||
|
||||
export const PATCH = apiHandler(async (req) => {
|
||||
const { planId, userId } = await req.json();
|
||||
requireUserId(userId);
|
||||
if (!planId) throw new ApiError("planId 不能为空");
|
||||
|
||||
const plan = await prisma.weekendPlan.findUnique({ where: { id: planId } });
|
||||
if (!plan) throw new ApiError("计划不存在", 404);
|
||||
if (plan.userId !== userId) throw new ApiError("只能接受自己的计划", 403);
|
||||
|
||||
await prisma.weekendPlan.update({
|
||||
where: { id: planId },
|
||||
data: { status: "accepted" },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
});
|
||||
|
||||
export const GET = apiHandler(async (req) => {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const roomId = searchParams.get("roomId");
|
||||
const userId = searchParams.get("userId");
|
||||
|
||||
requireUserId(userId);
|
||||
if (!roomId) throw new ApiError("roomId 不能为空");
|
||||
|
||||
const plan = await prisma.weekendPlan.findFirst({
|
||||
where: { roomId, userId: userId!, status: "accepted" },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, planData: true, createdAt: true },
|
||||
});
|
||||
|
||||
if (!plan) return NextResponse.json({ plan: null });
|
||||
|
||||
const parsed = JSON.parse(plan.planData);
|
||||
|
||||
return NextResponse.json({
|
||||
plan: {
|
||||
id: plan.id,
|
||||
days: parsed.days,
|
||||
createdAt: plan.createdAt,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user