-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathroute.ts
More file actions
52 lines (45 loc) · 1.48 KB
/
Copy pathroute.ts
File metadata and controls
52 lines (45 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { withAuth } from "@/lib/server-auth";
import { logger } from "@/lib/logger";
import { parseBody } from "@/lib/validation";
import { applyCouponBodySchema } from "@/lib/validation/schemas/coupons";
export const POST = withAuth(async (request: NextRequest, _context, _user) => {
try {
const parsed = await parseBody(request, applyCouponBodySchema);
if (!parsed.success) return parsed.response;
const { code, cartTotal } = parsed.data;
const coupon = await prisma.coupon.findUnique({
where: { code: code.toUpperCase() },
});
if (!coupon) {
return NextResponse.json({ error: "Coupon not found" }, { status: 404 });
}
if (coupon.expiresAt && coupon.expiresAt < new Date()) {
return NextResponse.json(
{ error: "Coupon has expired" },
{ status: 400 }
);
}
if (coupon.usedCount >= coupon.maxUses) {
return NextResponse.json(
{ error: "Coupon has already been used" },
{ status: 400 }
);
}
const discountedTotal = cartTotal * (1 - coupon.discount);
return NextResponse.json({
discountedTotal,
discountPercent: coupon.discount * 100,
});
} catch (error) {
logger.error(
{ err: error, route: "/api/coupon/apply" },
"Error applying coupon"
);
return NextResponse.json(
{ error: "Failed to apply coupon" },
{ status: 500 }
);
}
});