Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/app/api/habits/[id]/log/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,19 @@ export async function POST(
return NextResponse.json({ toggled: false });
}

await HabitLog.create({
habitId: id,
userId,
date,
});

return NextResponse.json({ toggled: true }, { status: 201 });
try {
await HabitLog.create({
habitId: id,
userId,
date,
});
return NextResponse.json({ toggled: true }, { status: 201 });
} catch (err: unknown) {
// Concurrent request created the log first — the unique habitId+date
// index rejects our insert, but a log now exists either way.
if (err && typeof err === "object" && (err as { code?: number }).code === 11000) {
return NextResponse.json({ toggled: true }, { status: 200 });
}
throw err;
}
}