61 lines
2.0 KiB
TypeScript
61 lines
2.0 KiB
TypeScript
|
|
export const runtime = 'nodejs'
|
||
|
|
import { NextResponse } from 'next/server'
|
||
|
|
import { auth } from '@/auth'
|
||
|
|
import { PrismaClient } from '@/app/generated/prisma'
|
||
|
|
|
||
|
|
// 연결 해제: 세션 사용자 ↔ 주어진 핸들의 매핑만 제거 (핸들 삭제 X)
|
||
|
|
export async function GET(request: Request) {
|
||
|
|
const session = await auth()
|
||
|
|
if (!session) {
|
||
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
const { searchParams } = new URL(request.url)
|
||
|
|
const handle = searchParams.get('handle')
|
||
|
|
if (!handle) {
|
||
|
|
return NextResponse.json({ error: '핸들이 필요합니다' }, { status: 400 })
|
||
|
|
}
|
||
|
|
|
||
|
|
const prisma = new PrismaClient()
|
||
|
|
try {
|
||
|
|
const email = session.user?.email ?? ''
|
||
|
|
const user = await prisma.user.findFirst({ where: { email }, select: { id: true } })
|
||
|
|
if (!user) {
|
||
|
|
return NextResponse.json({ error: '사용자를 찾을 수 없습니다.' }, { status: 404 })
|
||
|
|
}
|
||
|
|
|
||
|
|
const handleRow = await prisma.handle.findUnique({ where: { handle } })
|
||
|
|
if (!handleRow) {
|
||
|
|
return NextResponse.json({ error: '핸들을 찾을 수 없습니다.' }, { status: 404 })
|
||
|
|
}
|
||
|
|
|
||
|
|
// 이미 연결되어 있는지 확인
|
||
|
|
const linked = await prisma.handle.findFirst({
|
||
|
|
where: { id: handleRow.id, users: { some: { id: user.id } } },
|
||
|
|
select: { id: true },
|
||
|
|
})
|
||
|
|
|
||
|
|
if (!linked) {
|
||
|
|
return NextResponse.json({ success: true, message: '이미 연결되지 않은 채널입니다.' }, { status: 200 })
|
||
|
|
}
|
||
|
|
|
||
|
|
// 연결 해제
|
||
|
|
await prisma.user.update({
|
||
|
|
where: { id: user.id },
|
||
|
|
data: { handles: { disconnect: { id: handleRow.id } } },
|
||
|
|
})
|
||
|
|
|
||
|
|
return NextResponse.json({ success: true, message: '채널 연결을 해제했습니다.' }, { status: 200 })
|
||
|
|
} finally {
|
||
|
|
// prisma 종료
|
||
|
|
try { await (prisma as any).$disconnect() } catch {}
|
||
|
|
}
|
||
|
|
} catch (e) {
|
||
|
|
console.error('channel/delete 오류:', e)
|
||
|
|
return NextResponse.json({ error: '요청 처리 실패' }, { status: 500 })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|