diff --git a/app/api/routes-b/tags/[id]/route.ts b/app/api/routes-b/tags/[id]/route.ts index 36854cb..8672fbf 100644 --- a/app/api/routes-b/tags/[id]/route.ts +++ b/app/api/routes-b/tags/[id]/route.ts @@ -2,73 +2,37 @@ import { NextRequest, NextResponse } from 'next/server' import { prisma } from '@/lib/db' import { verifyAuthToken } from '@/lib/auth' +// ── DELETE /api/routes-b/tags/[id] — remove a tag and all its invoice associations ── export async function DELETE( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { - const authToken = request.headers.get('authorization')?.replace('Bearer ', '') - const claims = await verifyAuthToken(authToken || '') - if (!claims) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) - - const user = await prisma.user.findUnique({ where: { privyId: claims.userId } }) - if (!user) return NextResponse.json({ error: 'User not found' }, { status: 404 }) - - const { id } = await params - - const tag = await prisma.tag.findUnique({ where: { id } }) - if (!tag) return NextResponse.json({ error: 'Tag not found' }, { status: 404 }) - if (tag.userId !== user.id) return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) - - await prisma.tag.delete({ where: { id } }) - - return new NextResponse(null, { status: 204 }) -} - -export async function PATCH( - request: NextRequest, - { params }: { params: Promise<{ id: string }> } -) { - const authToken = request.headers.get('authorization')?.replace('Bearer ', '') - const claims = await verifyAuthToken(authToken || '') - if (!claims) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) - - const user = await prisma.user.findUnique({ where: { privyId: claims.userId } }) - if (!user) return NextResponse.json({ error: 'User not found' }, { status: 404 }) - - const { id } = await params - - const tag = await prisma.tag.findUnique({ where: { id } }) - if (!tag) return NextResponse.json({ error: 'Tag not found' }, { status: 404 }) - if (tag.userId !== user.id) return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) + try { + const { id } = await params + const authToken = request.headers.get('authorization')?.replace('Bearer ', '') + const claims = await verifyAuthToken(authToken || '') + if (!claims) { + return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) + } - const body = await request.json().catch(() => ({})) - const { name, color } = body + const user = await prisma.user.findUnique({ where: { privyId: claims.userId } }) + if (!user) { + return NextResponse.json({ error: 'User not found' }, { status: 404 }) + } - if (name !== undefined && (typeof name !== 'string' || name.trim() === '')) { - return NextResponse.json({ error: 'Name must be a non-empty string' }, { status: 400 }) - } + const tag = await prisma.tag.findUnique({ where: { id } }) + if (!tag) { + return NextResponse.json({ error: 'Tag not found' }, { status: 404 }) + } - if (name && name.trim() !== tag.name) { - const existingTag = await prisma.tag.findUnique({ - where: { - userId_name: { - userId: user.id, - name: name.trim(), - }, - }, - }) - if (existingTag) { - return NextResponse.json({ error: 'Tag name already used' }, { status: 409 }) + if (tag.userId !== user.id) { + return NextResponse.json({ error: 'Forbidden' }, { status: 403 }) } - } - const updatedTag = await prisma.tag.update({ - where: { id }, - data: { - ...(name ? { name: name.trim() } : {}), - ...(color ? { color } : {}), - }, - }) + await prisma.tag.delete({ where: { id } }) - return NextResponse.json(updatedTag) + return new NextResponse(null, { status: 204 }) + } catch (error) { + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) + } }