68 lines
2.8 KiB
JavaScript
68 lines
2.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getPoolBalance = getPoolBalance;
|
|
exports.listPools = listPools;
|
|
exports.fundPool = fundPool;
|
|
exports.debitPool = debitPool;
|
|
exports.creditPool = creditPool;
|
|
const connection_1 = require("../db/connection");
|
|
async function getPoolBalance(walletTypeId) {
|
|
const rows = await (0, connection_1.query)('SELECT available_balance::text FROM platform_bonus_pools WHERE wallet_type_id = $1', [walletTypeId]);
|
|
return rows[0]?.available_balance ?? '0';
|
|
}
|
|
async function listPools() {
|
|
return (0, connection_1.query)('SELECT wallet_type_id, available_balance::text FROM platform_bonus_pools ORDER BY wallet_type_id');
|
|
}
|
|
/** Admin funds the platform bonus pool (off-chain treasury allocation). */
|
|
async function fundPool(walletTypeId, amount) {
|
|
const client = await (0, connection_1.getClient)();
|
|
try {
|
|
await (0, connection_1.beginAuthorizedTransaction)(client);
|
|
const row = await client.query('SELECT available_balance FROM platform_bonus_pools WHERE wallet_type_id = $1 FOR UPDATE', [walletTypeId]);
|
|
if (!row.rows[0])
|
|
throw new Error('Bonus pool not found for wallet type');
|
|
const newBalance = BigInt(row.rows[0].available_balance) + amount;
|
|
await client.query('UPDATE platform_bonus_pools SET available_balance = $1, updated_at = NOW() WHERE wallet_type_id = $2', [newBalance.toString(), walletTypeId]);
|
|
await client.query('COMMIT');
|
|
}
|
|
catch (e) {
|
|
await client.query('ROLLBACK');
|
|
throw e;
|
|
}
|
|
finally {
|
|
client.release();
|
|
}
|
|
}
|
|
async function debitPool(walletTypeId, amount) {
|
|
const client = await (0, connection_1.getClient)();
|
|
try {
|
|
await (0, connection_1.beginAuthorizedTransaction)(client);
|
|
const row = await client.query('SELECT available_balance FROM platform_bonus_pools WHERE wallet_type_id = $1 FOR UPDATE', [walletTypeId]);
|
|
if (!row.rows[0]) {
|
|
await client.query('ROLLBACK');
|
|
return false;
|
|
}
|
|
const available = BigInt(row.rows[0].available_balance);
|
|
if (available < amount) {
|
|
await client.query('ROLLBACK');
|
|
return false;
|
|
}
|
|
await client.query('UPDATE platform_bonus_pools SET available_balance = $1, updated_at = NOW() WHERE wallet_type_id = $2', [(available - amount).toString(), walletTypeId]);
|
|
await client.query('COMMIT');
|
|
return true;
|
|
}
|
|
catch (e) {
|
|
await client.query('ROLLBACK');
|
|
throw e;
|
|
}
|
|
finally {
|
|
client.release();
|
|
}
|
|
}
|
|
/** Credit pool when reversing a rejected/expired pending referral commission. */
|
|
async function creditPool(walletTypeId, amount) {
|
|
if (amount <= 0n)
|
|
return;
|
|
await fundPool(walletTypeId, amount);
|
|
}
|
|
//# sourceMappingURL=bonusPoolRepository.js.map
|