67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { requireAuth, slugify } from '@/lib/auth'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const user = await requireAuth()
|
|
|
|
// Check if user already has a label
|
|
const existingLabel = await prisma.label.findUnique({
|
|
where: { userId: user.id },
|
|
})
|
|
|
|
if (existingLabel) {
|
|
return NextResponse.json({ error: 'User already has a label profile' }, { status: 400 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { name, description, logoUrl, website } = body
|
|
|
|
if (!name) {
|
|
return NextResponse.json({ error: 'Name is required' }, { status: 400 })
|
|
}
|
|
|
|
const slug = slugify(name)
|
|
|
|
const label = await prisma.label.create({
|
|
data: {
|
|
userId: user.id,
|
|
name,
|
|
slug,
|
|
description,
|
|
logoUrl,
|
|
website,
|
|
},
|
|
include: {
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
username: true,
|
|
displayName: true,
|
|
},
|
|
},
|
|
_count: {
|
|
select: {
|
|
artists: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
// Update user role to label
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: { role: 'label' },
|
|
})
|
|
|
|
return NextResponse.json(label, { status: 201 })
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
console.error('Error creating label:', error)
|
|
return NextResponse.json({ error: 'Failed to create label' }, { status: 500 })
|
|
}
|
|
}
|