middle 2
This commit is contained in:
60
app/api/channel/delete/route.ts
Normal file
60
app/api/channel/delete/route.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
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 })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
61
app/api/channel/list/route.ts
Normal file
61
app/api/channel/list/route.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
export const runtime = 'nodejs'
|
||||
import { NextResponse } from 'next/server';
|
||||
import { auth } from '@/auth';
|
||||
import { PrismaClient } from '@/app/generated/prisma';
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const email = session.user?.email as string | undefined;
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: '세션 이메일을 찾을 수 없습니다' }, { status: 400 });
|
||||
}
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
try {
|
||||
// Admin: return all handles
|
||||
if (email === 'wsx204@naver.com') {
|
||||
const all = await prisma.handle.findMany({
|
||||
orderBy: { handle: 'asc' },
|
||||
select: { id: true, handle: true, avatar: true }
|
||||
});
|
||||
const items = all.map(h => ({
|
||||
id: h.id,
|
||||
handle: h.handle,
|
||||
createtime: new Date().toISOString(),
|
||||
is_approved: false,
|
||||
icon: h.avatar,
|
||||
}));
|
||||
return NextResponse.json({ items });
|
||||
}
|
||||
|
||||
// Non-admin: handles linked to session user
|
||||
const user = await prisma.user.findFirst({ where: { email }, select: { id: true } });
|
||||
if (!user) {
|
||||
return NextResponse.json({ items: [] });
|
||||
}
|
||||
const linked = await prisma.handle.findMany({
|
||||
where: { users: { some: { id: user.id } } },
|
||||
orderBy: { handle: 'asc' },
|
||||
select: { id: true, handle: true, avatar: true }
|
||||
});
|
||||
const items = linked.map(h => ({
|
||||
id: h.id,
|
||||
handle: h.handle,
|
||||
createtime: new Date().toISOString(),
|
||||
is_approved: false,
|
||||
icon: h.avatar,
|
||||
}));
|
||||
return NextResponse.json({ items });
|
||||
} catch (e) {
|
||||
console.error('list_channel 오류:', e);
|
||||
return NextResponse.json({ error: '조회 실패' }, { status: 500 });
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
33
app/api/channel/mycode/route.ts
Normal file
33
app/api/channel/mycode/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export const runtime = 'nodejs'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { PrismaClient } from '@/app/generated/prisma'
|
||||
|
||||
export async function GET() {
|
||||
console.log('mycode 요청')
|
||||
const session = await auth()
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const email = session.user?.email as string | undefined
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: '세션 이메일을 찾을 수 없습니다' }, { status: 400 })
|
||||
}
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
try {
|
||||
const user = await prisma.user.findFirst({
|
||||
where: { email },
|
||||
select: { RegisgerCode: true },
|
||||
})
|
||||
return NextResponse.json({ registerCode: user?.RegisgerCode ?? null })
|
||||
} catch (e) {
|
||||
console.error('register_code 조회 오류:', e)
|
||||
return NextResponse.json({ error: '조회 실패' }, { status: 500 })
|
||||
} finally {
|
||||
await prisma.$disconnect()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
112
app/api/channel/register/route.ts
Normal file
112
app/api/channel/register/route.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
export const runtime = 'nodejs'
|
||||
import { NextResponse } from 'next/server';
|
||||
import { auth } from '@/auth';
|
||||
import { PrismaClient } from '@/app/generated/prisma';
|
||||
|
||||
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');
|
||||
// 안전하게 DB에서 최신 registerCode 조회 (세션 의존 제거)
|
||||
const prisma = new PrismaClient();
|
||||
const me = await prisma.user.findFirst({ where: { email: session.user?.email ?? '' }, select: { RegisgerCode: true } });
|
||||
const registerCode = me?.RegisgerCode ?? undefined;
|
||||
console.log(`registerCode: ${registerCode} and handle: ${handle}`);
|
||||
// 이미 등록된 채널인지 사전 확인 (현재 로그인 유저 기준)
|
||||
const userRowEarly = await prisma.user.findFirst({ where: { email: session.user?.email ?? '' }, select: { id: true } });
|
||||
if (!userRowEarly) {
|
||||
await prisma.$disconnect();
|
||||
return NextResponse.json({ error: '사용자를 찾을 수 없습니다.' }, { status: 404 });
|
||||
}
|
||||
const alreadyLinkedEarly = await prisma.handle.findFirst({
|
||||
where: { handle: handle ?? '', users: { some: { id: userRowEarly.id } } },
|
||||
select: { id: true },
|
||||
});
|
||||
if (alreadyLinkedEarly) {
|
||||
await prisma.$disconnect();
|
||||
return NextResponse.json({ success: true, message: '이미 등록된 채널입니다.' }, { status: 409 });
|
||||
}
|
||||
if (!handle) {
|
||||
return NextResponse.json({ error: '핸들이 필요합니다' }, { status: 400 });
|
||||
}
|
||||
|
||||
if (!registerCode) {
|
||||
return NextResponse.json({ error: 'can not find register code' }, { status: 400 });
|
||||
}
|
||||
|
||||
console.log('registerCode:', registerCode);
|
||||
|
||||
const upstream = await fetch('http://localhost:9556/isCodeMatch', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ handle: handle, code: registerCode })
|
||||
});
|
||||
|
||||
let data: any = null;
|
||||
const text = await upstream.text();
|
||||
try {
|
||||
data = text ? JSON.parse(text) : null;
|
||||
} catch {
|
||||
data = { message: text };
|
||||
}
|
||||
|
||||
console.log('data:', data);
|
||||
if (data.success) {
|
||||
if (data.foundtext) {
|
||||
if (data.foundtext == registerCode) {
|
||||
const avatarUrl = (data as any)?.avatar as string | undefined;
|
||||
// map user <-> handle (N:M)
|
||||
const email = session.user?.email ?? '';
|
||||
const userRow = await prisma.user.findFirst({ where: { email }, select: { id: true } });
|
||||
if (!userRow) {
|
||||
return NextResponse.json({ error: '사용자를 찾을 수 없습니다.' }, { status: 404 });
|
||||
}
|
||||
|
||||
// ensure handle exists
|
||||
const handleRow = await prisma.handle.upsert({
|
||||
where: { handle: handle },
|
||||
update: { ...(avatarUrl ? { avatar: avatarUrl } : {}) },
|
||||
create: { handle: handle, avatar: avatarUrl ?? '' },
|
||||
});
|
||||
|
||||
// avoid duplicate link
|
||||
const alreadyLinked = await prisma.handle.findFirst({
|
||||
where: { id: handleRow.id, users: { some: { id: userRow.id } } },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!alreadyLinked) {
|
||||
await prisma.user.update({
|
||||
where: { id: userRow.id },
|
||||
data: { handles: { connect: { id: handleRow.id } } },
|
||||
});
|
||||
}
|
||||
|
||||
await prisma.$disconnect();
|
||||
return NextResponse.json({ success: true, message: '채널 매핑 완료' }, { status: 200 });
|
||||
}
|
||||
else{
|
||||
return NextResponse.json({ error: '코드가 일치하지 않습니다.' }, { status: 400 });
|
||||
}
|
||||
} else {
|
||||
return NextResponse.json({ error: '요청 실패' }, { status: 400 });
|
||||
}
|
||||
} else {
|
||||
return NextResponse.json({ error: '요청 실패' }, { status: 400 });
|
||||
}
|
||||
|
||||
|
||||
return NextResponse.json(data ?? {}, { status: upstream.status });
|
||||
} catch (error) {
|
||||
console.error('register_channel 프록시 오류:', error);
|
||||
return NextResponse.json({ error: '업스트림 요청 실패' }, { status: 502 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user