Copy overhaul + qr designs
This commit is contained in:
@@ -4,6 +4,7 @@ import { db } from '@/lib/db';
|
||||
import { z } from 'zod';
|
||||
import { csrfProtection } from '@/lib/csrf';
|
||||
import { rateLimit, getClientIdentifier, RateLimits } from '@/lib/rateLimit';
|
||||
import { DYNAMIC_QR_LIMITS } from '@/lib/plans';
|
||||
|
||||
const updateQRSchema = z.object({
|
||||
title: z.string().min(1).optional(),
|
||||
@@ -103,6 +104,39 @@ export async function PATCH(
|
||||
return NextResponse.json({ error: 'QR code not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Reactivating a paused code consumes a slot again. Without this check,
|
||||
// pause -> create a new one -> unpause would quietly put the user over
|
||||
// their plan limit.
|
||||
if (
|
||||
data.status === 'ACTIVE' &&
|
||||
existing.status === 'PAUSED' &&
|
||||
existing.type === 'DYNAMIC'
|
||||
) {
|
||||
const user = await db.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { plan: true },
|
||||
});
|
||||
const limit =
|
||||
DYNAMIC_QR_LIMITS[(user?.plan ?? 'FREE') as keyof typeof DYNAMIC_QR_LIMITS] ??
|
||||
DYNAMIC_QR_LIMITS.FREE;
|
||||
const activeCount = await db.qRCode.count({
|
||||
where: { userId, type: 'DYNAMIC', status: 'ACTIVE' },
|
||||
});
|
||||
|
||||
if (activeCount >= limit) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Limit reached',
|
||||
message: `You have ${activeCount} of ${limit} dynamic QR codes active. Pause another one first, or upgrade to reactivate this code.`,
|
||||
currentCount: activeCount,
|
||||
limit,
|
||||
plan: user?.plan ?? 'FREE',
|
||||
},
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Static QR codes cannot be edited
|
||||
if (existing.type === 'STATIC' && data.content) {
|
||||
return NextResponse.json(
|
||||
@@ -119,6 +153,7 @@ export async function PATCH(
|
||||
...(data.content && { content: data.content }),
|
||||
...(data.tags && { tags: data.tags }),
|
||||
...(data.style && { style: data.style }),
|
||||
...(data.status && { status: data.status }),
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user