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

203 lines
7.2 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.invalidateSettingsCache = invalidateSettingsCache;
exports.getSettings = getSettings;
exports.updateSettings = updateSettings;
const connection_1 = require("../db/connection");
const config_1 = require("../config");
/**
* Repository for system-wide configuration settings.
* Reads and updates password policy, token expiry, and runtime ops from the database.
*/
const SETTINGS_CACHE_MS = 5000;
let settingsCache = null;
function buildDefaults() {
return {
password_min_length: 8,
password_require_uppercase: true,
password_require_lowercase: true,
password_require_number: true,
password_require_special: true,
access_token_expiry: '15m',
refresh_token_expiry: '7d',
require_email_verification_for_login: true,
allow_withdrawals: config_1.config.withdrawal.allowWithdrawals,
network_usdt_trc20_deposit_enabled: true,
network_usdt_trc20_withdraw_enabled: true,
network_usdt_bep20_deposit_enabled: true,
network_usdt_bep20_withdraw_enabled: true,
rate_limit_window_ms: config_1.config.rateLimit.windowMs,
rate_limit_max: config_1.config.rateLimit.max,
login_rate_limit_max: config_1.config.rateLimit.loginMax,
daily_withdrawal_limit_usd: config_1.config.withdrawal.dailyLimitUsd,
large_withdrawal_threshold_usd: config_1.config.withdrawal.largeThresholdUsd,
candle_predict_engine_tick_ms: config_1.config.candlePredict.engineTickMs,
candle_predict_backfill_max_per_tick: config_1.config.candlePredict.backfillMaxPerTick,
candle_predict_backfill_interval_ms: config_1.config.candlePredict.backfillIntervalMs,
candle_predict_backfill_seed_max: config_1.config.candlePredict.backfillSeedMax,
engine_audit_log_retention_days: 7,
candle_predict_retention_days: 30,
candle_predict_retention_keep_last: 5000,
withdrawal_network_fee_usdt_micro: config_1.config.withdrawal.networkFeeUsdtMicro,
transfer_min_remaining_usdt_micro: 0,
require_admin_2fa: true,
require_withdrawal_2fa: true,
max_concurrent_sessions: 10,
login_max_failed_attempts: 5,
login_lockout_minutes: 15,
login_captcha_after_failures: 3,
sybil_auto_suspend_accounts: true,
login_risk_auto_suspend_accounts: false,
login_risk_auto_suspend_threshold: 70,
support_message_edit_window_minutes: 15,
support_message_delete_window_minutes: 15,
};
}
const BOOLEAN_KEYS = new Set([
'password_require_uppercase',
'password_require_lowercase',
'password_require_number',
'password_require_special',
'require_email_verification_for_login',
'allow_withdrawals',
'network_usdt_trc20_deposit_enabled',
'network_usdt_trc20_withdraw_enabled',
'network_usdt_bep20_deposit_enabled',
'network_usdt_bep20_withdraw_enabled',
'require_admin_2fa',
'require_withdrawal_2fa',
'sybil_auto_suspend_accounts',
'login_risk_auto_suspend_accounts',
]);
const INTEGER_KEYS = new Set([
'password_min_length',
'rate_limit_window_ms',
'rate_limit_max',
'login_rate_limit_max',
'daily_withdrawal_limit_usd',
'large_withdrawal_threshold_usd',
'candle_predict_engine_tick_ms',
'candle_predict_backfill_max_per_tick',
'candle_predict_backfill_interval_ms',
'candle_predict_backfill_seed_max',
'engine_audit_log_retention_days',
'candle_predict_retention_days',
'candle_predict_retention_keep_last',
'withdrawal_network_fee_usdt_micro',
'transfer_min_remaining_usdt_micro',
'login_max_failed_attempts',
'login_lockout_minutes',
'login_captcha_after_failures',
'max_concurrent_sessions',
'login_risk_auto_suspend_threshold',
'support_message_edit_window_minutes',
'support_message_delete_window_minutes',
]);
/**
* Converts a raw string setting value to its typed representation.
* @param key - Setting key that determines the target type.
* @param value - Raw string value stored in the database.
* @returns Parsed value as number, boolean, or string.
*/
function parseValue(key, value) {
if (BOOLEAN_KEYS.has(key))
return value === 'true';
if (INTEGER_KEYS.has(key))
return parseInt(value, 10) || 0;
return value;
}
/** Clears the in-memory settings cache (call after updates). */
function invalidateSettingsCache() {
settingsCache = null;
}
/**
* Loads all system settings, merging database values with env-backed defaults.
* @returns Complete system settings object with typed values.
*/
async function getSettings() {
if (settingsCache && Date.now() - settingsCache.at < SETTINGS_CACHE_MS) {
return settingsCache.data;
}
const rows = await (0, connection_1.query)('SELECT key, value FROM system_settings');
const result = buildDefaults();
for (const row of rows) {
result[row.key] = parseValue(row.key, row.value);
}
settingsCache = { at: Date.now(), data: result };
return result;
}
const UPDATABLE_KEYS = [
'password_min_length',
'password_require_uppercase',
'password_require_lowercase',
'password_require_number',
'password_require_special',
'access_token_expiry',
'refresh_token_expiry',
'require_email_verification_for_login',
'allow_withdrawals',
'network_usdt_trc20_deposit_enabled',
'network_usdt_trc20_withdraw_enabled',
'network_usdt_bep20_deposit_enabled',
'network_usdt_bep20_withdraw_enabled',
'rate_limit_window_ms',
'rate_limit_max',
'login_rate_limit_max',
'daily_withdrawal_limit_usd',
'large_withdrawal_threshold_usd',
'candle_predict_engine_tick_ms',
'candle_predict_backfill_max_per_tick',
'candle_predict_backfill_interval_ms',
'candle_predict_backfill_seed_max',
'engine_audit_log_retention_days',
'candle_predict_retention_days',
'candle_predict_retention_keep_last',
'withdrawal_network_fee_usdt_micro',
'transfer_min_remaining_usdt_micro',
'require_admin_2fa',
'login_max_failed_attempts',
'login_lockout_minutes',
'login_captcha_after_failures',
'require_withdrawal_2fa',
'sybil_auto_suspend_accounts',
'login_risk_auto_suspend_accounts',
'login_risk_auto_suspend_threshold',
'max_concurrent_sessions',
'support_message_edit_window_minutes',
'support_message_delete_window_minutes',
];
/**
* Persists one or more system setting updates.
* Only keys present in the updates object are written; others are left unchanged.
* @param updates - Partial settings object with values to save.
* @returns Resolves when all provided settings have been upserted.
*/
async function updateSettings(updates) {
for (const key of UPDATABLE_KEYS) {
const value = updates[key];
if (value === undefined)
continue;
const str = typeof value === 'boolean' ? (value ? 'true' : 'false') : String(value);
await (0, connection_1.query)(`INSERT INTO system_settings (key, value, updated_at) VALUES ($1, $2, NOW())
ON CONFLICT (key) DO UPDATE SET value = $2, updated_at = NOW()`, [key, str]);
}
invalidateSettingsCache();
}
//# sourceMappingURL=settingsRepository.js.map