59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { requireAuth } from '@/lib/auth'
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string; invitationId: string }> }
|
|
) {
|
|
try {
|
|
const user = await requireAuth()
|
|
const { id, invitationId } = await params
|
|
|
|
// Check if user owns this label
|
|
const label = await prisma.label.findUnique({
|
|
where: { id },
|
|
select: { userId: true },
|
|
})
|
|
|
|
if (!label) {
|
|
return NextResponse.json({ error: 'Label not found' }, { status: 404 })
|
|
}
|
|
|
|
if (label.userId !== user.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 })
|
|
}
|
|
|
|
// Check if invitation exists and belongs to this label
|
|
const invitation = await prisma.labelInvitation.findUnique({
|
|
where: { id: invitationId },
|
|
select: { labelId: true, status: true },
|
|
})
|
|
|
|
if (!invitation) {
|
|
return NextResponse.json({ error: 'Invitation not found' }, { status: 404 })
|
|
}
|
|
|
|
if (invitation.labelId !== id) {
|
|
return NextResponse.json({ error: 'Invitation does not belong to this label' }, { status: 403 })
|
|
}
|
|
|
|
if (invitation.status !== 'pending') {
|
|
return NextResponse.json({ error: 'Can only cancel pending invitations' }, { status: 400 })
|
|
}
|
|
|
|
// Delete the invitation
|
|
await prisma.labelInvitation.delete({
|
|
where: { id: invitationId },
|
|
})
|
|
|
|
return NextResponse.json({ message: 'Invitation cancelled successfully' })
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
console.error('Error cancelling invitation:', error)
|
|
return NextResponse.json({ error: 'Failed to cancel invitation' }, { status: 500 })
|
|
}
|
|
}
|