Admin: gestión de sitios de voto (home_votesite)

- lib/admin-votes.ts: listVoteSites / createVoteSite / deleteVoteSite.
- Routes /api/admin/votes (POST) y /api/admin/votes/[id] (DELETE), guard isAdmin.
- Página /admin/votes (AdminVotesManager: crear/listar/borrar) + enlace en dashboard.
- Conecta con la página de votación (/vote-points) ya existente.

Verificado: guardas (307/403), CRUD del write layer OK (con env cargado).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-13 00:03:06 +00:00
parent 0f71e1f4d9
commit 934bee2aec
8 changed files with 182 additions and 2 deletions
+5
View File
@@ -27,6 +27,11 @@ export default async function AdminPage({ params }: { params: Promise<{ locale:
{t('managePrices')}
</Link>
</li>
<li>
<Link href="/admin/votes" className="text-sky-400 hover:underline">
{t('manageVotes')}
</Link>
</li>
</ul>
</main>
)
@@ -0,0 +1,24 @@
import { getTranslations, setRequestLocale } from 'next-intl/server'
import { redirect } from '@/i18n/navigation'
import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { listVoteSites } from '@/lib/admin-votes'
import { AdminVotesManager } from '@/components/AdminVotesManager'
export const dynamic = 'force-dynamic'
export default async function AdminVotesPage({ params }: { params: Promise<{ locale: string }> }) {
const { locale } = await params
setRequestLocale(locale)
const t = await getTranslations('Admin')
const session = await getSession()
if (!session.bnetId) redirect({ href: '/login', locale })
if (!(await isAdmin(session))) redirect({ href: '/', locale })
const sites = await listVoteSites()
return (
<main className="mx-auto max-w-3xl px-4 py-8">
<h1 className="mb-6 text-2xl font-bold text-amber-500">{t('votes')}</h1>
<AdminVotesManager initial={sites} />
</main>
)
}
@@ -0,0 +1,11 @@
import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { deleteVoteSite } from '@/lib/admin-votes'
export async function DELETE(_request: Request, { params }: { params: Promise<{ id: string }> }) {
const session = await getSession()
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
const { id } = await params
await deleteVoteSite(Number(id))
return Response.json({ success: true })
}
+17
View File
@@ -0,0 +1,17 @@
import { getSession } from '@/lib/session'
import { isAdmin } from '@/lib/admin'
import { createVoteSite } from '@/lib/admin-votes'
export async function POST(request: Request) {
const session = await getSession()
if (!(await isAdmin(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
let b: Record<string, unknown> = {}
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
const name = String(b.name ?? '').trim()
const url = String(b.url ?? '').trim()
const imageUrl = String(b.imageUrl ?? '').trim()
const points = Number(b.points)
if (!name || !url || !(points >= 0)) return Response.json({ success: false, error: 'emptyError' })
const id = await createVoteSite(name, url, imageUrl, points)
return Response.json({ success: true, id })
}