Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 23 additions & 59 deletions app/api/routes-b/tags/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}
Loading