104 lines
5.5 KiB
JavaScript
104 lines
5.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getAccountLedgerMeta = getAccountLedgerMeta;
|
|
exports.getSnapshot = getSnapshot;
|
|
exports.upsertSnapshot = upsertSnapshot;
|
|
exports.listLockedReservationMismatches = listLockedReservationMismatches;
|
|
exports.listInvalidLedgerReferences = listInvalidLedgerReferences;
|
|
exports.checksumBalances = checksumBalances;
|
|
const crypto_1 = require("crypto");
|
|
const connection_1 = require("../db/connection");
|
|
async function getAccountLedgerMeta(accountId) {
|
|
const rows = await (0, connection_1.query)(`SELECT la.id AS account_id,
|
|
la.available_balance::text,
|
|
la.locked_balance::text,
|
|
COALESCE(SUM(le.amount), 0)::text AS entries_sum,
|
|
MAX(le.created_at) AS last_entry_at,
|
|
COUNT(le.id)::text AS entry_count
|
|
FROM ledger_accounts la
|
|
LEFT JOIN ledger_entries le ON le.account_id = la.id
|
|
WHERE la.id = $1
|
|
GROUP BY la.id`, [accountId]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function getSnapshot(accountId) {
|
|
const rows = await (0, connection_1.query)(`SELECT account_id, available_balance::text, locked_balance::text, entries_sum::text,
|
|
last_entry_at, snapshot_at
|
|
FROM ledger_integrity_snapshots WHERE account_id = $1`, [accountId]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function upsertSnapshot(meta) {
|
|
await (0, connection_1.query)(`INSERT INTO ledger_integrity_snapshots (account_id, available_balance, locked_balance, entries_sum, last_entry_at, snapshot_at)
|
|
VALUES ($1, $2, $3, $4, $5, NOW())
|
|
ON CONFLICT (account_id) DO UPDATE SET
|
|
available_balance = EXCLUDED.available_balance,
|
|
locked_balance = EXCLUDED.locked_balance,
|
|
entries_sum = EXCLUDED.entries_sum,
|
|
last_entry_at = EXCLUDED.last_entry_at,
|
|
snapshot_at = NOW()`, [
|
|
meta.account_id,
|
|
meta.available_balance,
|
|
meta.locked_balance,
|
|
meta.entries_sum,
|
|
meta.last_entry_at,
|
|
]);
|
|
}
|
|
/**
|
|
* Main accounts whose locked_balance does not equal SUM(withdrawal_locks).
|
|
* Catches unauthorized available↔locked reshuffles that conserve total balance.
|
|
*/
|
|
async function listLockedReservationMismatches() {
|
|
return (0, connection_1.query)(`SELECT la.id AS account_id,
|
|
la.user_id,
|
|
la.wallet_type_id,
|
|
la.available_balance::text,
|
|
la.locked_balance::text,
|
|
COALESCE((SELECT SUM(le.amount) FROM ledger_entries le WHERE le.account_id = la.id), 0)::text AS entries_sum,
|
|
COALESCE(wl.locks_sum, 0)::text AS locks_sum
|
|
FROM ledger_accounts la
|
|
LEFT JOIN (
|
|
SELECT user_id, wallet_type_id, SUM(amount) AS locks_sum
|
|
FROM withdrawal_locks
|
|
GROUP BY user_id, wallet_type_id
|
|
) wl ON wl.user_id = la.user_id AND wl.wallet_type_id = la.wallet_type_id
|
|
WHERE la.account_kind = 'main'
|
|
AND la.locked_balance <> COALESCE(wl.locks_sum, 0)`);
|
|
}
|
|
/** Credits with missing or broken references to authorized source tables. */
|
|
async function listInvalidLedgerReferences() {
|
|
return (0, connection_1.query)(`SELECT la.id AS account_id, la.user_id, le.id AS entry_id, le.type, le.amount::text,
|
|
CASE
|
|
WHEN le.type = 'test_credit' AND le.reference_type = 'test_wallet_credit'
|
|
AND le.reference_id IS NULL THEN 'missing_reference'
|
|
WHEN le.type = 'test_credit' AND le.reference_type = 'test_wallet_credit'
|
|
AND NOT EXISTS (SELECT 1 FROM test_wallet_credits t WHERE t.id = le.reference_id) THEN 'invalid_test_credit_ref'
|
|
WHEN le.reference_id IS NULL THEN 'missing_reference'
|
|
WHEN le.type IN ('deposit','withdrawal') AND le.reference_type = 'transaction'
|
|
AND NOT EXISTS (SELECT 1 FROM transactions t WHERE t.id = le.reference_id) THEN 'invalid_transaction_ref'
|
|
WHEN le.type IN ('bonus','admin_adjustment') AND le.reference_type = 'ledger_adjustment'
|
|
AND NOT EXISTS (SELECT 1 FROM ledger_adjustments a WHERE a.id = le.reference_id) THEN 'invalid_adjustment_ref'
|
|
WHEN le.type IN ('game_payout','game_refund') AND le.reference_type = 'candle_predict_bet'
|
|
AND NOT EXISTS (SELECT 1 FROM candle_predict_bets b WHERE b.id = le.reference_id) THEN 'invalid_bet_ref'
|
|
ELSE 'unknown'
|
|
END AS issue
|
|
FROM ledger_entries le
|
|
JOIN ledger_accounts la ON la.id = le.account_id
|
|
WHERE le.amount > 0
|
|
AND (
|
|
(le.type = 'test_credit' AND le.reference_type = 'test_wallet_credit'
|
|
AND (le.reference_id IS NULL
|
|
OR NOT EXISTS (SELECT 1 FROM test_wallet_credits t WHERE t.id = le.reference_id)))
|
|
OR (le.type <> 'test_credit' AND le.reference_id IS NULL)
|
|
OR (le.type IN ('deposit','withdrawal') AND le.reference_type = 'transaction'
|
|
AND NOT EXISTS (SELECT 1 FROM transactions t WHERE t.id = le.reference_id))
|
|
OR (le.type IN ('bonus','admin_adjustment') AND le.reference_type = 'ledger_adjustment'
|
|
AND NOT EXISTS (SELECT 1 FROM ledger_adjustments a WHERE a.id = le.reference_id))
|
|
OR (le.type IN ('game_payout','game_refund') AND le.reference_type = 'candle_predict_bet'
|
|
AND NOT EXISTS (SELECT 1 FROM candle_predict_bets b WHERE b.id = le.reference_id))
|
|
)`);
|
|
}
|
|
function checksumBalances(meta) {
|
|
const payload = `${meta.available_balance}|${meta.locked_balance}|${meta.entries_sum}|${meta.entry_count}`;
|
|
return (0, crypto_1.createHash)('sha256').update(payload).digest('hex');
|
|
}
|
|
//# sourceMappingURL=ledgerIntegrityRepository.js.map
|