From 869ce078712beefc4acadf636ce975d798ea48b9 Mon Sep 17 00:00:00 2001 From: adevopg Date: Tue, 14 Jul 2026 14:07:02 +0000 Subject: [PATCH] =?UTF-8?q?A=C3=B1adir=20/points-history=20(Historial=20de?= =?UTF-8?q?=20PD=20y=20PV)=20con=20Stripe=20y=20SumUp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Historial unificado por cuenta que combina las fuentes reales: home_stripelog (pagos Stripe mode test/live + SumUp mode sumup), home_votelog (PV de votos) y home_promoredemption (PD/PV por código). Detecta compras de PD ("N PD") vs servicios pagados en €. lib/points-history.ts (getPointsHistory + getPointsBalances) y página con el diseño (info + tabla Fecha/Concepto/Método/PD/PV/Importe/Estado + saldo actual). El Django original solo tenía un stub. Co-Authored-By: Claude Opus 4.8 (1M context) --- web-next/app/[locale]/points-history/page.tsx | 103 +++++++++++++++ web-next/lib/points-history.ts | 125 ++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 web-next/app/[locale]/points-history/page.tsx create mode 100644 web-next/lib/points-history.ts diff --git a/web-next/app/[locale]/points-history/page.tsx b/web-next/app/[locale]/points-history/page.tsx new file mode 100644 index 0000000..8739918 --- /dev/null +++ b/web-next/app/[locale]/points-history/page.tsx @@ -0,0 +1,103 @@ +import type { Metadata } from 'next' +import { setRequestLocale } from 'next-intl/server' +import { redirect } from '@/i18n/navigation' +import { getSession } from '@/lib/session' +import { getPointsHistory, getPointsBalances, type PointsMovement } from '@/lib/points-history' +import { PageShell } from '@/components/PageShell' +import { ServiceBox } from '@/components/ServiceBox' + +export const dynamic = 'force-dynamic' + +export const metadata: Metadata = { title: 'Historial de PD y PV' } + +/** Fecha en formato DD-MM-YYYY HH:MM. */ +function fmt(d: Date): string { + const p = (n: number) => String(n).padStart(2, '0') + return `${p(d.getDate())}-${p(d.getMonth() + 1)}-${d.getFullYear()} ${p(d.getHours())}:${p(d.getMinutes())}` +} + +function methodClass(method: PointsMovement['method']): string { + if (method === 'SumUp') return 'green-info' + if (method === 'Stripe') return 'purple-info' + return 'second-brown' +} + +export default async function PointsHistoryPage({ params }: { params: Promise<{ locale: string }> }) { + const { locale } = await params + setRequestLocale(locale) + + const session = await getSession() + if (!session.bnetId) redirect({ href: '/login', locale }) + if (!session.username) redirect({ href: '/select-account', locale }) + + const [movements, balances] = await Promise.all([ + getPointsHistory(session.accountId!), + getPointsBalances(session.accountId!), + ]) + + return ( + + +
+

Aquí verás los consumos de PD y PV más recientes que ha tenido la cuenta.

+

+ Con esta información podrás tener control de todo lo que has adquirido así como también chequear que toda la + actividad sea normal. +

+

Si observas algún movimiento sospechoso, puedes contactar con el equipo de Nova WoW.

+
+
+ +
+
+

Historial de PD y PV

+
+
+

+ Saldo actual: {balances.dp} PD ·{' '} + {balances.pv} PV +

+
+ {movements.length === 0 ? ( + + + + + + +
+ No hay movimientos +
+ ) : ( + + + + + + + + + + + + + + {movements.map((m, i) => ( + + + + + + + + + + ))} + +
FechaConceptoMétodoPDPVImporteEstado
{fmt(m.date)}{m.concept}{m.method}{m.pd != null ? `+${m.pd}` : '—'}{m.pv != null ? `+${m.pv}` : '—'}{m.amount != null ? `${m.amount.toFixed(2)} €` : '—'}{m.status}
+ )} +
+
+
+ ) +} diff --git a/web-next/lib/points-history.ts b/web-next/lib/points-history.ts new file mode 100644 index 0000000..56e12d5 --- /dev/null +++ b/web-next/lib/points-history.ts @@ -0,0 +1,125 @@ +import type { RowDataPacket } from 'mysql2' +import { db, DB } from './db' + +/** Un movimiento del historial de PD/PV (pagos, votos, promociones). */ +export interface PointsMovement { + date: Date + concept: string + method: 'Stripe' | 'SumUp' | 'Voto' | 'Promoción' + pd: number | null // PD del movimiento (+), null si no aplica + pv: number | null // PV del movimiento (+), null si no aplica + amount: number | null // importe en € (pagos), null si no aplica + status: string +} + +/** Saldos actuales de PD y PV de una cuenta de juego. */ +export async function getPointsBalances(accountId: number): Promise<{ dp: number; pv: number }> { + if (!accountId) return { dp: 0, pv: 0 } + try { + const [rows] = await db(DB.default).query( + 'SELECT dp, vp FROM home_api_points WHERE accountID = ?', + [accountId], + ) + return rows[0] ? { dp: Number(rows[0].dp), pv: Number(rows[0].vp) } : { dp: 0, pv: 0 } + } catch { + return { dp: 0, pv: 0 } + } +} + +/** PD acreditados por un pago (si es una compra de PD), o null si es un servicio/ítem. */ +function purchasedPD(productName: string, service: string | null, metadata: string | null): number | null { + const m = /(\d+)\s*PD\b/i.exec(productName || '') + if (m) return Number(m[1]) + if (service === 'dpoints') { + try { + const p = metadata ? JSON.parse(metadata).points : null + return p ? Number(p) : null + } catch { + return null + } + } + return null +} + +/** + * Historial unificado de PD/PV de una cuenta, combinando las fuentes reales: + * - `home_stripelog`: pagos por **Stripe** (mode test/live) y **SumUp** (mode sumup). + * - `home_votelog` (+ `home_votesite`): PV ganados al votar. + * - `home_promoredemption` (+ `home_promocode`): PD/PV canjeados por código. + * Se ordena por fecha descendente. Cada consulta es tolerante a fallos (devuelve + * [] si la tabla no existe) para no romper la página. + */ +export async function getPointsHistory(accountId: number, limit = 100): Promise { + if (!accountId) return [] + const movements: PointsMovement[] = [] + + // 1) Pagos (Stripe + SumUp). + try { + const [rows] = await db(DB.default).query( + 'SELECT product_name, amount, mode, service, metadata, fulfilled, timestamp ' + + 'FROM home_stripelog WHERE account_id = ? ORDER BY timestamp DESC LIMIT ?', + [accountId, limit], + ) + for (const r of rows) { + const service = r.service ? String(r.service) : null + movements.push({ + date: new Date(r.timestamp), + concept: String(r.product_name || '—'), + method: r.mode === 'sumup' ? 'SumUp' : 'Stripe', + pd: purchasedPD(String(r.product_name || ''), service, r.metadata ? String(r.metadata) : null), + pv: null, + amount: Number(r.amount), + status: r.fulfilled ? 'Entregado' : 'Pendiente', + }) + } + } catch { + /* tabla ausente: se omite */ + } + + // 2) Votos (PV). + try { + const [rows] = await db(DB.default).query( + 'SELECT v.created_at, s.name, s.points FROM home_votelog v ' + + 'JOIN home_votesite s ON s.id = v.vote_site_id WHERE v.account_id = ? ORDER BY v.created_at DESC LIMIT ?', + [accountId, limit], + ) + for (const r of rows) { + movements.push({ + date: new Date(r.created_at), + concept: `Voto en ${r.name}`, + method: 'Voto', + pd: null, + pv: Number(r.points) || 0, + amount: null, + status: 'Acreditado', + }) + } + } catch { + /* tabla ausente: se omite */ + } + + // 3) Promociones (PD/PV). + try { + const [rows] = await db(DB.default).query( + 'SELECT r.redeemed_at, c.code, c.pd, c.pv FROM home_promoredemption r ' + + 'JOIN home_promocode c ON c.id = r.promo_id WHERE r.account_id = ? ORDER BY r.redeemed_at DESC LIMIT ?', + [accountId, limit], + ) + for (const r of rows) { + movements.push({ + date: new Date(r.redeemed_at), + concept: `Código ${r.code}`, + method: 'Promoción', + pd: Number(r.pd) > 0 ? Number(r.pd) : null, + pv: Number(r.pv) > 0 ? Number(r.pv) : null, + amount: null, + status: 'Acreditado', + }) + } + } catch { + /* tabla ausente: se omite */ + } + + movements.sort((a, b) => b.date.getTime() - a.date.getTime()) + return movements.slice(0, limit) +}