8.1 출석부: 데일리 체크인/중복 방지/포인트 지급/누적 통계 o
This commit is contained in:
28
src/app/api/attendance/route.ts
Normal file
28
src/app/api/attendance/route.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { getUserIdFromRequest } from "@/lib/auth";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const userId = getUserIdFromRequest(req);
|
||||
if (!userId) return NextResponse.json({ today: null, count: 0 });
|
||||
const start = new Date(); start.setHours(0,0,0,0);
|
||||
const end = new Date(); end.setHours(23,59,59,999);
|
||||
const today = await prisma.pointTransaction.findFirst({
|
||||
where: { userId, reason: "attendance", createdAt: { gte: start, lte: end } },
|
||||
});
|
||||
const count = await prisma.pointTransaction.count({ where: { userId, reason: "attendance" } });
|
||||
return NextResponse.json({ today: !!today, count });
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const userId = getUserIdFromRequest(req);
|
||||
if (!userId) return NextResponse.json({ error: "login required" }, { status: 401 });
|
||||
const start = new Date(); start.setHours(0,0,0,0);
|
||||
const end = new Date(); end.setHours(23,59,59,999);
|
||||
const exists = await prisma.pointTransaction.findFirst({ where: { userId, reason: "attendance", createdAt: { gte: start, lte: end } } });
|
||||
if (exists) return NextResponse.json({ ok: true, duplicated: true });
|
||||
await prisma.pointTransaction.create({ data: { userId, amount: 10, reason: "attendance" } });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user