Añadir /points-history (Historial de PD y PV) con Stripe y SumUp
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) <noreply@anthropic.com>
This commit is contained in:
@@ -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 (
|
||||
<PageShell title="Historial de PD y PV">
|
||||
<ServiceBox>
|
||||
<br />
|
||||
<p>Aquí verás los consumos de PD y PV más recientes que ha tenido la cuenta.</p>
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
<p>Si observas algún movimiento sospechoso, puedes contactar con el equipo de Nova WoW.</p>
|
||||
<br />
|
||||
</ServiceBox>
|
||||
|
||||
<div className="box-content">
|
||||
<div className="title-box-content">
|
||||
<h2>Historial de PD y PV</h2>
|
||||
</div>
|
||||
<div className="body-box-content">
|
||||
<p className="centered">
|
||||
Saldo actual: <span className="yellow-info">{balances.dp} PD</span> ·{' '}
|
||||
<span className="purple-info">{balances.pv} PV</span>
|
||||
</p>
|
||||
<br />
|
||||
{movements.length === 0 ? (
|
||||
<table className="max-center-table-aligned2">
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>
|
||||
<span>No hay movimientos</span>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
) : (
|
||||
<table className="max-center-table-aligned2">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Fecha</th>
|
||||
<th>Concepto</th>
|
||||
<th>Método</th>
|
||||
<th>PD</th>
|
||||
<th>PV</th>
|
||||
<th>Importe</th>
|
||||
<th>Estado</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{movements.map((m, i) => (
|
||||
<tr key={i}>
|
||||
<td>{fmt(m.date)}</td>
|
||||
<td>{m.concept}</td>
|
||||
<td className={methodClass(m.method)}>{m.method}</td>
|
||||
<td className="yellow-info">{m.pd != null ? `+${m.pd}` : '—'}</td>
|
||||
<td className="purple-info">{m.pv != null ? `+${m.pv}` : '—'}</td>
|
||||
<td>{m.amount != null ? `${m.amount.toFixed(2)} €` : '—'}</td>
|
||||
<td>{m.status}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</PageShell>
|
||||
)
|
||||
}
|
||||
@@ -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<RowDataPacket[]>(
|
||||
'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<PointsMovement[]> {
|
||||
if (!accountId) return []
|
||||
const movements: PointsMovement[] = []
|
||||
|
||||
// 1) Pagos (Stripe + SumUp).
|
||||
try {
|
||||
const [rows] = await db(DB.default).query<RowDataPacket[]>(
|
||||
'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<RowDataPacket[]>(
|
||||
'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<RowDataPacket[]>(
|
||||
'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)
|
||||
}
|
||||
Reference in New Issue
Block a user