8.4 무료쿠폰: 쿠폰 등록/재고/사용 처리/만료/1인 제한/로그 o
This commit is contained in:
25
src/app/api/coupons/redeem/route.ts
Normal file
25
src/app/api/coupons/redeem/route.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { getUserIdFromRequest } from "@/lib/auth";
|
||||
import { z } from "zod";
|
||||
|
||||
const schema = z.object({ code: z.string().min(1) });
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const userId = getUserIdFromRequest(req);
|
||||
if (!userId) return NextResponse.json({ error: "login required" }, { status: 401 });
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = schema.safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
const coupon = await prisma.coupon.findUnique({ where: { code: parsed.data.code } });
|
||||
if (!coupon || !coupon.active) return NextResponse.json({ error: "invalid coupon" }, { status: 404 });
|
||||
if (coupon.expiresAt && coupon.expiresAt < new Date()) return NextResponse.json({ error: "expired" }, { status: 400 });
|
||||
const used = await prisma.couponRedemption.count({ where: { couponId: coupon.id } });
|
||||
if (coupon.stock > 0 && used >= coupon.stock) return NextResponse.json({ error: "out of stock" }, { status: 400 });
|
||||
const mine = await prisma.couponRedemption.count({ where: { couponId: coupon.id, userId } });
|
||||
if (mine >= coupon.maxPerUser) return NextResponse.json({ error: "quota exceeded" }, { status: 400 });
|
||||
await prisma.couponRedemption.create({ data: { couponId: coupon.id, userId } });
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
|
||||
37
src/app/api/coupons/route.ts
Normal file
37
src/app/api/coupons/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
import { z } from "zod";
|
||||
|
||||
export async function GET() {
|
||||
const now = new Date();
|
||||
const coupons = await prisma.coupon.findMany({
|
||||
where: { active: true, OR: [{ expiresAt: null }, { expiresAt: { gte: now } }] },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { id: true, code: true, title: true, description: true, stock: true, maxPerUser: true, expiresAt: true },
|
||||
});
|
||||
// 사용량 계산
|
||||
const ids = coupons.map((c) => c.id);
|
||||
const agg = await prisma.couponRedemption.groupBy({ by: ["couponId"], where: { couponId: { in: ids } }, _count: { couponId: true } });
|
||||
const usedMap = new Map(agg.map((a) => [a.couponId, a._count.couponId]));
|
||||
const items = coupons.map((c) => ({ ...c, used: usedMap.get(c.id) ?? 0 }));
|
||||
return NextResponse.json({ coupons: items });
|
||||
}
|
||||
|
||||
const createSchema = z.object({
|
||||
code: z.string().min(1),
|
||||
title: z.string().min(1),
|
||||
description: z.string().optional(),
|
||||
stock: z.coerce.number().int().min(0).default(0),
|
||||
maxPerUser: z.coerce.number().int().min(1).default(1),
|
||||
expiresAt: z.coerce.date().optional(),
|
||||
});
|
||||
|
||||
export async function POST(req: Request) {
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const parsed = createSchema.safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
|
||||
const data = await prisma.coupon.create({ data: parsed.data });
|
||||
return NextResponse.json({ coupon: data }, { status: 201 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user