123 lines
3.7 KiB
TypeScript
123 lines
3.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { requireAuth } from '@/lib/auth'
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string; invitationId: string }> }
|
|
) {
|
|
try {
|
|
const user = await requireAuth()
|
|
const { id, invitationId } = await params
|
|
|
|
// Check if user owns this artist
|
|
const artist = await prisma.artist.findUnique({
|
|
where: { id },
|
|
select: { userId: true, labelId: true },
|
|
})
|
|
|
|
if (!artist) {
|
|
return NextResponse.json({ error: 'Artist not found' }, { status: 404 })
|
|
}
|
|
|
|
if (artist.userId !== user.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { response } = body
|
|
|
|
if (!response || !['accept', 'decline'].includes(response)) {
|
|
return NextResponse.json({ error: 'Response must be "accept" or "decline"' }, { status: 400 })
|
|
}
|
|
|
|
// Check if invitation exists and belongs to this artist
|
|
const invitation = await prisma.labelInvitation.findUnique({
|
|
where: { id: invitationId },
|
|
select: { artistId: true, status: true, expiresAt: true, labelId: true },
|
|
})
|
|
|
|
if (!invitation) {
|
|
return NextResponse.json({ error: 'Invitation not found' }, { status: 404 })
|
|
}
|
|
|
|
if (invitation.artistId !== id) {
|
|
return NextResponse.json({ error: 'Invitation does not belong to this artist' }, { status: 403 })
|
|
}
|
|
|
|
if (invitation.status !== 'pending') {
|
|
return NextResponse.json({ error: 'Invitation is no longer pending' }, { status: 400 })
|
|
}
|
|
|
|
// Check if invitation has expired
|
|
if (new Date() > invitation.expiresAt) {
|
|
await prisma.labelInvitation.update({
|
|
where: { id: invitationId },
|
|
data: { status: 'expired' },
|
|
})
|
|
return NextResponse.json({ error: 'Invitation has expired' }, { status: 400 })
|
|
}
|
|
|
|
if (response === 'accept') {
|
|
// Check if artist already has a label
|
|
if (artist.labelId) {
|
|
return NextResponse.json({ error: 'Artist already has a label' }, { status: 400 })
|
|
}
|
|
|
|
// Update artist's labelId and invitation status in a transaction
|
|
await prisma.$transaction([
|
|
prisma.artist.update({
|
|
where: { id },
|
|
data: { labelId: invitation.labelId },
|
|
}),
|
|
prisma.labelInvitation.update({
|
|
where: { id: invitationId },
|
|
data: { status: 'accepted' },
|
|
}),
|
|
])
|
|
|
|
const updatedInvitation = await prisma.labelInvitation.findUnique({
|
|
where: { id: invitationId },
|
|
include: {
|
|
label: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
logoUrl: true,
|
|
description: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(updatedInvitation)
|
|
} else {
|
|
// Decline invitation
|
|
const updatedInvitation = await prisma.labelInvitation.update({
|
|
where: { id: invitationId },
|
|
data: { status: 'declined' },
|
|
include: {
|
|
label: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
logoUrl: true,
|
|
description: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(updatedInvitation)
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
console.error('Error responding to invitation:', error)
|
|
return NextResponse.json({ error: 'Failed to respond to invitation' }, { status: 500 })
|
|
}
|
|
}
|