Lock completed past checkins

This commit is contained in:
biss committed 2026-09-16 22:20:01 +08:00
1 parent c8c330ea2d
commit 7e9f12423c
3 files changed
+6 -2

No files matched your search

+3
View File
@@ -4,6 +4,7 @@ import { progress } from "../../../db/schema";
const user = (r: Request) => r.headers.get("oai-authenticated-user-id");
const todayInChina = () => new Intl.DateTimeFormat("en-CA", { timeZone: "Asia/Shanghai" }).format(new Date());
const parseTasks = (value: string) => { try { return JSON.parse(value) as string[]; } catch { return []; } };
export async function GET(r: Request) {
const id = user(r);
@@ -21,6 +22,8 @@ export async function PUT(r: Request) {
if (b.date! > today) return Response.json({ error: "未来日期暂不能打卡。" }, { status: 400 });
try {
const db = getDb(), now = new Date().toISOString(), checkinType = b.date! < today ? "makeup" : "normal";
const existing = await db.select().from(progress).where(and(eq(progress.userId, id), eq(progress.planDate, b.date!)));
if (b.date! < today && existing[0] && parseTasks(existing[0].doneTasks).some(task => !b.doneTasks!.includes(task))) return Response.json({ error: "过去日期已完成的项目不能取消,只能补打未完成项目。" }, { status: 400 });
const values = { userId: id, planDate: b.date!, doneTasks: JSON.stringify(b.doneTasks), mood: (b.mood || "").slice(0, 20), note: (b.note || "").slice(0, 1000), checkinType, checkedAt: now, updatedAt: now };
await db.insert(progress).values(values).onConflictDoUpdate({ target: [progress.userId, progress.planDate], set: values });
const row = await db.select().from(progress).where(and(eq(progress.userId, id), eq(progress.planDate, b.date!)));