139 lines
5.3 KiB
JavaScript
139 lines
5.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* List / backfill prediction referral commissions missed when the bonus pool was empty
|
|
* (or otherwise never recorded a payout row).
|
|
*
|
|
* Dry-run (default):
|
|
* npx ts-node --transpile-only src/scripts/backfillMissedPredictionReferralCommissions.ts
|
|
*
|
|
* Apply (pool must be funded first):
|
|
* npx ts-node --transpile-only src/scripts/backfillMissedPredictionReferralCommissions.ts --apply
|
|
*
|
|
* Optional: --since=2026-10-01 (ISO date, inclusive on created_at)
|
|
* Optional: --limit=500
|
|
*
|
|
* VPS:
|
|
* sudo docker compose run --rm --no-deps --build backend \
|
|
* node dist/scripts/backfillMissedPredictionReferralCommissions.js
|
|
* sudo docker compose run --rm --no-deps --build backend \
|
|
* node dist/scripts/backfillMissedPredictionReferralCommissions.js --apply
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
require("./loadEnv");
|
|
const connection_1 = require("../db/connection");
|
|
const referralService_1 = require("../services/referralService");
|
|
function parseArgs(argv) {
|
|
let apply = false;
|
|
let since = null;
|
|
let limit = 500;
|
|
for (const arg of argv) {
|
|
if (arg === '--apply')
|
|
apply = true;
|
|
else if (arg.startsWith('--since='))
|
|
since = arg.slice('--since='.length).trim() || null;
|
|
else if (arg.startsWith('--limit=')) {
|
|
const n = Number(arg.slice('--limit='.length));
|
|
if (Number.isFinite(n) && n > 0)
|
|
limit = Math.min(Math.floor(n), 5000);
|
|
}
|
|
}
|
|
return { apply, since, limit };
|
|
}
|
|
async function listMissed(since, limit) {
|
|
const params = [];
|
|
let sinceClause = '';
|
|
if (since) {
|
|
params.push(since);
|
|
sinceClause = `AND b.created_at >= $${params.length}::timestamptz`;
|
|
}
|
|
params.push(limit);
|
|
const limitPlaceholder = `$${params.length}`;
|
|
return (0, connection_1.query)(`SELECT b.id, b.user_id, b.status, b.stake_raw::text, b.stake_account_kind, b.wallet_type_id,
|
|
u.referred_by_user_id, b.created_at
|
|
FROM candle_predict_bets b
|
|
JOIN users u ON u.id = b.user_id
|
|
CROSS JOIN referral_program_settings s
|
|
WHERE s.id = 1
|
|
AND s.enabled = TRUE
|
|
AND COALESCE(s.prediction_commissions_enabled, TRUE) = TRUE
|
|
AND b.status IN ('won', 'lost')
|
|
AND COALESCE(b.stake_account_kind, 'main') <> 'test'
|
|
AND b.stake_raw > 0
|
|
AND u.referred_by_user_id IS NOT NULL
|
|
AND b.wallet_type_id = s.commission_wallet_type_id
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM referral_commission_payouts p
|
|
WHERE p.candle_predict_bet_id = b.id
|
|
AND p.commission_source = 'prediction'
|
|
)
|
|
${sinceClause}
|
|
ORDER BY b.created_at ASC
|
|
LIMIT ${limitPlaceholder}`, params);
|
|
}
|
|
async function poolBalance() {
|
|
const rows = await (0, connection_1.query)(`SELECT available_balance::text
|
|
FROM platform_bonus_pools
|
|
WHERE wallet_type_id = (
|
|
SELECT commission_wallet_type_id FROM referral_program_settings WHERE id = 1
|
|
)`);
|
|
return rows[0]?.available_balance ?? '0';
|
|
}
|
|
async function main() {
|
|
const { apply, since, limit } = parseArgs(process.argv.slice(2));
|
|
console.log('=== backfillMissedPredictionReferralCommissions ===');
|
|
console.log(`mode=${apply ? 'APPLY' : 'DRY-RUN'} since=${since ?? '(all)'} limit=${limit}`);
|
|
const balance = await poolBalance();
|
|
console.log(`bonus pool available_balance=${balance}`);
|
|
const missed = await listMissed(since, limit);
|
|
console.log(`missed bets (no prediction payout row): ${missed.length}`);
|
|
for (const b of missed) {
|
|
console.log(` ${b.created_at.toISOString()} bet=${b.id} user=${b.user_id} status=${b.status} stake=${b.stake_raw} kind=${b.stake_account_kind ?? 'main'} referrer=${b.referred_by_user_id}`);
|
|
}
|
|
if (!apply) {
|
|
console.log('\nDry-run only. Fund the bonus pool, then re-run with --apply.');
|
|
return;
|
|
}
|
|
if (missed.length === 0) {
|
|
console.log('Nothing to backfill.');
|
|
return;
|
|
}
|
|
if (BigInt(balance) <= 0n) {
|
|
console.error('Refusing --apply: bonus pool balance is 0. Fund the pool first.');
|
|
process.exitCode = 1;
|
|
return;
|
|
}
|
|
let ok = 0;
|
|
let fail = 0;
|
|
for (const b of missed) {
|
|
try {
|
|
await (0, referralService_1.processPredictionReferralCommissions)({
|
|
betId: b.id,
|
|
referredUserId: b.user_id,
|
|
stakeRaw: BigInt(b.stake_raw),
|
|
stakeAccountKind: b.stake_account_kind === 'bonus'
|
|
? 'bonus'
|
|
: b.stake_account_kind === 'test'
|
|
? 'test'
|
|
: 'main',
|
|
walletTypeId: b.wallet_type_id,
|
|
betOutcome: b.status,
|
|
});
|
|
ok += 1;
|
|
console.log(`OK bet=${b.id}`);
|
|
}
|
|
catch (e) {
|
|
fail += 1;
|
|
console.error(`FAIL bet=${b.id}:`, e instanceof Error ? e.message : e);
|
|
}
|
|
}
|
|
const balanceAfter = await poolBalance();
|
|
console.log(`\nDone. processed_ok=${ok} failed=${fail} pool_after=${balanceAfter}`);
|
|
console.log('Re-check payouts: SELECT * FROM referral_commission_payouts ORDER BY created_at DESC LIMIT 50;');
|
|
}
|
|
main()
|
|
.then(() => process.exit(process.exitCode ?? 0))
|
|
.catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|
|
//# sourceMappingURL=backfillMissedPredictionReferralCommissions.js.map
|