869ce07871
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>
126 lines
4.3 KiB
TypeScript
126 lines
4.3 KiB
TypeScript
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)
|
|
}
|