send-gift: permitir pagar con PV (puntos de voto), más caro que PD
Solo en /send-gift se añade una 4ª forma de pago: PV (vote points, gratis por votar). Cuesta más que PD para preservar su valor: coste_PV = coste_PD × VP_PRICE_FACTOR (=2, configurable en lib/pay-with-dpoints). - lib/dpoints: getVPointsBalance, creditVPoints y spendVPoints (débito atómico del campo vp, con FOR UPDATE), espejo de las de PD. - lib/pay-with-dpoints: VP_PRICE_FACTOR, vpointsCost() y payServiceWithVPoints() (descuenta PV, ejecuta el envío y reembolsa si falla). - gift/checkout: rama provider='vp' (además de 'pd'). - service-success: la entrega inmediata cubre 'pd' y 'vp'. - PaymentMethodSelect: opción VP opcional (solo si el form pasa vpBalance); muestra coste en PV, insuficiencia y el saldo VP. - SendGiftForm + página: pasan el saldo VP. - i18n Pay: vp, vpCost, insufficientVp, balanceVp, errors.insufficientVp (es/en). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -40,6 +40,73 @@ export async function getDPointsBalance(accountId: number): Promise<number> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Saldo de PV (vote points, ganados al votar) de una cuenta. 0 si no tiene fila. */
|
||||
export async function getVPointsBalance(accountId: number): Promise<number> {
|
||||
if (!accountId) return 0
|
||||
try {
|
||||
const [rows] = await db(DB.default).query<RowDataPacket[]>(
|
||||
'SELECT vp FROM home_api_points WHERE accountID = ?',
|
||||
[accountId],
|
||||
)
|
||||
return rows[0] ? Number(rows[0].vp) : 0
|
||||
} catch {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
/** Acredita PV (vote points) a una cuenta (upsert). Se usa para reembolsos. */
|
||||
export async function creditVPoints(accountId: number, points: number): Promise<boolean> {
|
||||
if (!accountId || !(points > 0)) return false
|
||||
try {
|
||||
await db(DB.default).query(
|
||||
'INSERT INTO home_api_points (accountID, vp, dp) VALUES (?, ?, 0) ON DUPLICATE KEY UPDATE vp = vp + ?',
|
||||
[accountId, points, points],
|
||||
)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Descuenta `points` PV del saldo de una cuenta de forma atómica (bloqueo de
|
||||
* fila). Devuelve `insufficientFunds` si no hay saldo suficiente. Si la acción
|
||||
* posterior falla, reembolsa con `creditVPoints`.
|
||||
*/
|
||||
export async function spendVPoints(
|
||||
accountId: number,
|
||||
points: number,
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
if (!accountId || !Number.isInteger(points) || points <= 0) {
|
||||
return { success: false, error: 'invalidRequest' }
|
||||
}
|
||||
const conn = await db(DB.default).getConnection()
|
||||
try {
|
||||
await conn.beginTransaction()
|
||||
const [src] = await conn.query<RowDataPacket[]>(
|
||||
'SELECT vp FROM home_api_points WHERE accountID = ? FOR UPDATE',
|
||||
[accountId],
|
||||
)
|
||||
const balance = src[0] ? Number(src[0].vp) : 0
|
||||
if (balance < points) {
|
||||
await conn.rollback()
|
||||
return { success: false, error: 'insufficientFunds' }
|
||||
}
|
||||
await conn.query('UPDATE home_api_points SET vp = vp - ? WHERE accountID = ?', [points, accountId])
|
||||
await conn.commit()
|
||||
return { success: true }
|
||||
} catch {
|
||||
try {
|
||||
await conn.rollback()
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
return { success: false, error: 'genericError' }
|
||||
} finally {
|
||||
conn.release()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfiere `points` PD desde una cuenta de origen a una de destino de forma
|
||||
* atómica (transacción con bloqueo de fila del origen). Es irreversible.
|
||||
|
||||
Reference in New Issue
Block a user