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

146 lines
5.0 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,
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,
withdrawal_network_fee_usdt_micro: config_1.config.withdrawal.networkFeeUsdtMicro,
};
}
const BOOLEAN_KEYS = new Set([
'password_require_uppercase',
'password_require_lowercase',
'password_require_number',
'password_require_special',
'require_email_verification_for_login',
'allow_withdrawals',
]);
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',
'withdrawal_network_fee_usdt_micro',
]);
/**
* 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',
'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',
'withdrawal_network_fee_usdt_micro',
];
/**
* 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