8.4 무료쿠폰: 쿠폰 등록/재고/사용 처리/만료/1인 제한/로그 o
This commit is contained in:
@@ -220,6 +220,7 @@ model User {
|
||||
adminNotifications AdminNotification[] @relation("AdminNotificationCreator")
|
||||
ipBlocksCreated IPBlock[] @relation("IPBlockCreator")
|
||||
postViewLogs PostViewLog[] @relation("PostViewLogUser")
|
||||
couponRedemptions CouponRedemption[]
|
||||
|
||||
@@index([status])
|
||||
@@index([createdAt])
|
||||
@@ -583,3 +584,35 @@ model PasswordResetToken {
|
||||
@@index([userId, expiresAt])
|
||||
@@map("password_reset_tokens")
|
||||
}
|
||||
|
||||
// 쿠폰
|
||||
model Coupon {
|
||||
id String @id @default(cuid())
|
||||
code String @unique
|
||||
title String
|
||||
description String?
|
||||
stock Int @default(0) // 총 재고
|
||||
maxPerUser Int @default(1) // 1인당 제한
|
||||
expiresAt DateTime?
|
||||
active Boolean @default(true)
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
redemptions CouponRedemption[]
|
||||
|
||||
@@index([active, expiresAt])
|
||||
@@map("coupons")
|
||||
}
|
||||
|
||||
model CouponRedemption {
|
||||
id String @id @default(cuid())
|
||||
couponId String
|
||||
userId String
|
||||
createdAt DateTime @default(now())
|
||||
|
||||
coupon Coupon @relation(fields: [couponId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [userId], onDelete: Cascade)
|
||||
|
||||
@@unique([couponId, userId])
|
||||
@@index([userId, createdAt])
|
||||
@@map("coupon_redemptions")
|
||||
}
|
||||
|
||||
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 });
|
||||
}
|
||||
|
||||
|
||||
33
src/app/coupons/page.tsx
Normal file
33
src/app/coupons/page.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
"use client";
|
||||
import useSWR from "swr";
|
||||
|
||||
const fetcher = (url: string) => fetch(url).then((r) => r.json());
|
||||
|
||||
export default function CouponsPage() {
|
||||
const { data, mutate } = useSWR<{ coupons: { id: string; code: string; title: string; description?: string; stock: number; maxPerUser: number; expiresAt?: string; used: number }[] }>("/api/coupons", fetcher);
|
||||
async function redeem(code: string) {
|
||||
await fetch("/api/coupons/redeem", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ code }) });
|
||||
mutate();
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
<h1>무료쿠폰</h1>
|
||||
<ul style={{ display: "flex", flexDirection: "column", gap: 8 }}>
|
||||
{(data?.coupons ?? []).map((c) => (
|
||||
<li key={c.id} style={{ border: "1px solid #eee", borderRadius: 8, padding: 12 }}>
|
||||
<div style={{ display: "flex", justifyContent: "space-between" }}>
|
||||
<div>
|
||||
<strong>{c.title}</strong>
|
||||
<div style={{ opacity: 0.8 }}>{c.description}</div>
|
||||
<div style={{ fontSize: 12, opacity: 0.7 }}>코드: {c.code} · 소진: {c.used}/{c.stock || "∞"}</div>
|
||||
</div>
|
||||
<button onClick={() => redeem(c.code)}>사용하기</button>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,8 +54,8 @@
|
||||
[특수 페이지(카테고리)]
|
||||
8.1 출석부: 데일리 체크인/중복 방지/포인트 지급/누적 통계 o
|
||||
8.2 포인트안내: 정책 안내 페이지(에디터 연동/버전 이력) o
|
||||
8.3 회원랭킹: 기간별 랭킹 집계/캐싱/페이지네이션/정렬 옵션
|
||||
8.4 무료쿠폰: 쿠폰 등록/재고/사용 처리/만료/1인 제한/로그
|
||||
8.3 회원랭킹: 기간별 랭킹 집계/캐싱/페이지네이션/정렬 옵션 o
|
||||
8.4 무료쿠폰: 쿠폰 등록/재고/사용 처리/만료/1인 제한/로그 o
|
||||
8.5 월간집계: 월별 지표 산출 배치/차트/다운로드(CSV)
|
||||
8.6 주변 제휴업체: 위치 기반 목록/지도/필터(거리/카테고리)
|
||||
8.7 제휴문의: 접수 폼/관리자 승인 워크플로우/알림
|
||||
|
||||
Reference in New Issue
Block a user