games_app/backend/dist/repositories/testWalletRepository.js
2026-06-29 15:01:23 +02:00

82 lines
3.4 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.createCreditRecord = createCreditRecord;
exports.listCredits = listCredits;
const connection_1 = require("../db/connection");
async function getSettings() {
const rows = await (0, connection_1.query)(`SELECT id, enabled, credit_amount_raw::text, max_requests_per_user, cooldown_hours,
wallet_type_id, updated_at
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),
updated_at = NOW()
WHERE id = 1
RETURNING id, enabled, credit_amount_raw::text, max_requests_per_user, cooldown_hours,
wallet_type_id, updated_at`, [
input.enabled ?? null,
input.creditAmountRaw ?? null,
input.maxRequestsPerUser ?? null,
input.cooldownHours ?? null,
input.walletTypeId ?? 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;
}
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