games_app/backend/dist-test-faq/repositories/testWalletRepository.js

103 lines
4.5 KiB
JavaScript

"use strict";
/**
* Test wallet settings and credit request history.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSettings = getSettings;
exports.updateSettings = updateSettings;
exports.countUserCredits = countUserCredits;
exports.getLastCreditAt = getLastCreditAt;
exports.countTestPredictionsInWindow = countTestPredictionsInWindow;
exports.createCreditRecord = createCreditRecord;
exports.listCredits = listCredits;
const connection_1 = require("../db/connection");
const SETTINGS_SELECT = `id, enabled, credit_amount_raw::text, max_requests_per_user, cooldown_hours,
wallet_type_id, prediction_limit_enabled, max_test_predictions_per_window,
test_prediction_window_hours, updated_at`;
async function getSettings() {
const rows = await (0, connection_1.query)(`SELECT ${SETTINGS_SELECT}
FROM test_wallet_settings WHERE id = 1`);
if (!rows[0]) {
throw new Error('Test wallet settings are not configured');
}
return rows[0];
}
async function updateSettings(input) {
const current = await getSettings();
const rows = await (0, connection_1.query)(`UPDATE test_wallet_settings SET
enabled = COALESCE($1, enabled),
credit_amount_raw = COALESCE($2, credit_amount_raw),
max_requests_per_user = COALESCE($3, max_requests_per_user),
cooldown_hours = COALESCE($4, cooldown_hours),
wallet_type_id = COALESCE($5, wallet_type_id),
prediction_limit_enabled = COALESCE($6, prediction_limit_enabled),
max_test_predictions_per_window = COALESCE($7, max_test_predictions_per_window),
test_prediction_window_hours = COALESCE($8, test_prediction_window_hours),
updated_at = NOW()
WHERE id = 1
RETURNING ${SETTINGS_SELECT}`, [
input.enabled ?? null,
input.creditAmountRaw ?? null,
input.maxRequestsPerUser ?? null,
input.cooldownHours ?? null,
input.walletTypeId ?? null,
input.predictionLimitEnabled ?? null,
input.maxTestPredictionsPerWindow ?? null,
input.testPredictionWindowHours ?? null,
]);
return rows[0] ?? current;
}
async function countUserCredits(userId) {
const rows = await (0, connection_1.query)('SELECT COUNT(*)::text AS count FROM test_wallet_credits WHERE user_id = $1', [userId]);
return parseInt(rows[0]?.count ?? '0', 10) || 0;
}
async function getLastCreditAt(userId) {
const rows = await (0, connection_1.query)(`SELECT created_at FROM test_wallet_credits
WHERE user_id = $1
ORDER BY created_at DESC
LIMIT 1`, [userId]);
return rows[0]?.created_at ?? null;
}
/** Sliding-window count of Test Wallet predictions (single + multi + auto). */
async function countTestPredictionsInWindow(userId, windowHours) {
const hours = Math.max(1, Math.floor(windowHours));
const rows = await (0, connection_1.query)(`SELECT COUNT(*)::text AS count, MIN(created_at) AS oldest
FROM candle_predict_bets
WHERE user_id = $1
AND stake_account_kind = 'test'
AND created_at >= NOW() - make_interval(hours => $2)`, [userId, hours]);
return {
count: parseInt(rows[0]?.count ?? '0', 10) || 0,
oldestInWindowAt: rows[0]?.oldest ?? null,
};
}
async function createCreditRecord(input) {
const rows = await (0, connection_1.query)(`INSERT INTO test_wallet_credits (user_id, wallet_type_id, amount_raw)
VALUES ($1, $2, $3)
RETURNING id, user_id, wallet_type_id, amount_raw::text, created_at`, [input.userId, input.walletTypeId, input.amountRaw]);
return rows[0];
}
async function listCredits(options) {
const params = [];
let where = '';
if (options.userId) {
params.push(options.userId);
where = `WHERE twc.user_id = $${params.length}`;
}
const countRows = await (0, connection_1.query)(`SELECT COUNT(*)::text AS count FROM test_wallet_credits twc ${where}`, params);
const total = parseInt(countRows[0]?.count ?? '0', 10) || 0;
params.push(options.limit, options.offset);
const limitIdx = params.length - 1;
const offsetIdx = params.length;
const items = await (0, connection_1.query)(`SELECT twc.id, twc.user_id, twc.wallet_type_id, twc.amount_raw::text, twc.created_at,
u.email_encrypted AS user_email,
wt.code AS wallet_code
FROM test_wallet_credits twc
JOIN wallet_types wt ON wt.id = twc.wallet_type_id
LEFT JOIN users u ON u.id = twc.user_id
${where}
ORDER BY twc.created_at DESC
LIMIT $${limitIdx} OFFSET $${offsetIdx}`, params);
return { items, total };
}
//# sourceMappingURL=testWalletRepository.js.map