deploy first
This commit is contained in:
22
app/api/admin/handle/cpv/route.ts
Normal file
22
app/api/admin/handle/cpv/route.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { PrismaClient } from '@/app/generated/prisma'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const prisma = new PrismaClient()
|
||||
try {
|
||||
const body = await request.json().catch(() => ({})) as any
|
||||
const id = String(body?.id || '')
|
||||
const costPerView = Number(body?.costPerView)
|
||||
if (!id || !Number.isFinite(costPerView)) {
|
||||
return NextResponse.json({ error: 'invalid payload' }, { status: 400 })
|
||||
}
|
||||
await prisma.handle.update({ where: { id }, data: { costPerView } })
|
||||
return NextResponse.json({ ok: true })
|
||||
} catch (e) {
|
||||
return NextResponse.json({ error: 'update failed' }, { status: 500 })
|
||||
} finally {
|
||||
try { await prisma.$disconnect() } catch {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { PrismaClient } from '@/app/generated/prisma';
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const prisma = new PrismaClient();
|
||||
try {
|
||||
const body = await request.json();
|
||||
const id: string | undefined = body?.id;
|
||||
const approve: boolean | undefined = body?.approve;
|
||||
if (!id || typeof approve !== 'boolean') {
|
||||
return NextResponse.json({ error: 'id와 approve(boolean)가 필요합니다' }, { status: 400 });
|
||||
}
|
||||
|
||||
const updated = await prisma.userHandle.update({
|
||||
where: { id },
|
||||
data: { isApproved: approve },
|
||||
select: { id: true, isApproved: true },
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true, item: updated });
|
||||
} catch (e) {
|
||||
console.error('POST /api/admin/user_handles/approve 오류:', e);
|
||||
return NextResponse.json({ error: '업데이트 실패' }, { status: 500 });
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,26 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { PrismaClient } from '@/app/generated/prisma';
|
||||
|
||||
// 모든 Handle을 단순 목록으로 반환합니다.
|
||||
// 관리자 UI 호환을 위해 필드 형태를 맞춥니다.
|
||||
export async function GET() {
|
||||
const prisma = new PrismaClient();
|
||||
try {
|
||||
const handles = await prisma.userHandle.findMany({
|
||||
orderBy: { createtime: 'desc' },
|
||||
select: { id: true, email: true, handle: true, isApproved: true, createtime: true, icon: true }
|
||||
const handles = await prisma.handle.findMany({
|
||||
orderBy: { handle: 'asc' },
|
||||
select: { id: true, handle: true, avatar: true, costPerView: true },
|
||||
});
|
||||
return NextResponse.json({ items: handles });
|
||||
const nowIso = new Date().toISOString();
|
||||
const items = handles.map(h => ({
|
||||
id: h.id,
|
||||
email: '',
|
||||
handle: h.handle,
|
||||
isApproved: true,
|
||||
createtime: nowIso,
|
||||
icon: h.avatar,
|
||||
costPerView: Number(h.costPerView ?? 0),
|
||||
}));
|
||||
return NextResponse.json({ items });
|
||||
} catch (e) {
|
||||
console.error('admin user_handles 오류:', e);
|
||||
return NextResponse.json({ error: '조회 실패' }, { status: 500 });
|
||||
|
||||
@@ -5,44 +5,29 @@ import { PrismaClient } from '@/app/generated/prisma';
|
||||
|
||||
export async function GET() {
|
||||
const session = await auth();
|
||||
if (!session) {
|
||||
if (!session?.user?.email) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
const email = session.user?.email as string | undefined;
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: '세션 이메일을 찾을 수 없습니다' }, { status: 400 });
|
||||
}
|
||||
const email = session.user.email as string;
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
try {
|
||||
// Admin: return all handles
|
||||
let rows;
|
||||
if (email === 'wsx204@naver.com') {
|
||||
const all = await prisma.handle.findMany({
|
||||
// 관리자: 전체 핸들
|
||||
rows = await prisma.handle.findMany({
|
||||
orderBy: { handle: 'asc' },
|
||||
select: { id: true, handle: true, avatar: true }
|
||||
select: { id: true, handle: true, avatar: true },
|
||||
});
|
||||
} else {
|
||||
// 일반 사용자: 자신의 핸들만
|
||||
rows = await prisma.handle.findMany({
|
||||
where: { users: { some: { email } } },
|
||||
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 => ({
|
||||
const items = rows.map(h => ({
|
||||
id: h.id,
|
||||
handle: h.handle,
|
||||
createtime: new Date().toISOString(),
|
||||
|
||||
43
app/api/channel/stats/route.ts
Normal file
43
app/api/channel/stats/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { PrismaClient } from '@/app/generated/prisma'
|
||||
import { auth } from '@/auth'
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const prisma = new PrismaClient()
|
||||
try {
|
||||
const session = await auth()
|
||||
const { searchParams } = new URL(request.url)
|
||||
const handleIdsParam = searchParams.get('handleIds')
|
||||
let handleIds: string[] | null = null
|
||||
if (handleIdsParam) {
|
||||
handleIds = handleIdsParam.split(',').map(s => s.trim()).filter(Boolean)
|
||||
}
|
||||
|
||||
if (!handleIds) {
|
||||
if (!session?.user?.email) return NextResponse.json({ items: [] })
|
||||
const myHandles = await prisma.handle.findMany({
|
||||
where: { users: { some: { email: session.user.email } } },
|
||||
select: { id: true },
|
||||
})
|
||||
handleIds = myHandles.map(h => h.id)
|
||||
}
|
||||
|
||||
const items: Array<{ handleId: string, videoCount: number, views: number }> = []
|
||||
for (const id of handleIds) {
|
||||
const videoCount = await prisma.content.count({ where: { handleId: id } })
|
||||
const agg = await prisma.contentDayView.aggregate({
|
||||
_sum: { views: true },
|
||||
where: { content: { handleId: id } },
|
||||
})
|
||||
items.push({ handleId: id, videoCount, views: Number(agg._sum.views ?? 0) })
|
||||
}
|
||||
|
||||
return NextResponse.json({ items })
|
||||
} catch (e) {
|
||||
return NextResponse.json({ error: 'stats failed' }, { status: 500 })
|
||||
} finally {
|
||||
try { await prisma.$disconnect() } catch {}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -118,7 +118,8 @@ export async function GET(request: Request) {
|
||||
cur.validViews += r.validViews || 0
|
||||
cur.premiumViews += r.premiumViews || 0
|
||||
cur.watchTime += r.watchTime || 0
|
||||
cur.expectedRevenue += Math.max(0, Math.round((r.validViews || 0) * cpv))
|
||||
const effViews = (r.validViews || 0) + (r.premiumViews || 0)
|
||||
cur.expectedRevenue += Math.max(0, Math.round(effViews * cpv))
|
||||
bucket.set(key, cur)
|
||||
}
|
||||
// 빈 버킷 채우기 (연속 타임라인)
|
||||
@@ -189,7 +190,7 @@ export async function GET(request: Request) {
|
||||
const premiumViews = g._sum?.premiumViews ?? 0
|
||||
const watchTime = g._sum?.watchTime ?? 0
|
||||
const cpv = typeof h?.costPerView === 'number' ? h!.costPerView! : 0
|
||||
const expectedRevenue = Math.max(0, Math.round(validViews * cpv))
|
||||
const expectedRevenue = Math.max(0, Math.round((validViews + premiumViews) * cpv))
|
||||
return {
|
||||
id: c?.id ?? g.contentId,
|
||||
subject: c?.subject ?? '',
|
||||
|
||||
Reference in New Issue
Block a user