From 53d4bc45093500d0c92342327d6cb3e8b6362a8d Mon Sep 17 00:00:00 2001 From: OnyemaAnthony Date: Sat, 28 Mar 2026 21:14:20 +0100 Subject: [PATCH] implement api for tag deletion --- app/api/routes-b/tags/[id]/route.ts | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 app/api/routes-b/tags/[id]/route.ts diff --git a/app/api/routes-b/tags/[id]/route.ts b/app/api/routes-b/tags/[id]/route.ts new file mode 100644 index 0000000..8672fbf --- /dev/null +++ b/app/api/routes-b/tags/[id]/route.ts @@ -0,0 +1,38 @@ +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 }> } +) { + 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 user = await prisma.user.findUnique({ where: { privyId: claims.userId } }) + if (!user) { + return NextResponse.json({ error: 'User not found' }, { status: 404 }) + } + + 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 }) + } catch (error) { + return NextResponse.json({ error: 'Internal server error' }, { status: 500 }) + } +}