games_app/backend/dist/services/settingsService.js
2026-11-13 18:22:05 +02:00

301 lines
14 KiB
JavaScript

"use strict";
/**
* System settings service.
* Applies validation and business rules for admin configuration updates.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSettings = getSettings;
exports.parseSettingsUpdates = parseSettingsUpdates;
exports.updateSettings = updateSettings;
const settingsRepository = __importStar(require("../repositories/settingsRepository"));
const auditLogRepository = __importStar(require("../repositories/auditLogRepository"));
const auditDiff_1 = require("../utils/auditDiff");
const gasReserve_1 = require("../utils/gasReserve");
function clampInt(value, min, max) {
return Math.min(max, Math.max(min, value));
}
function validateUpdates(updates) {
if (updates.password_min_length !== undefined) {
updates.password_min_length = clampInt(updates.password_min_length, 6, 128);
}
if (updates.rate_limit_window_ms !== undefined) {
updates.rate_limit_window_ms = clampInt(updates.rate_limit_window_ms, 60000, 3600000);
}
if (updates.rate_limit_max !== undefined) {
updates.rate_limit_max = clampInt(updates.rate_limit_max, 10, 10000);
}
if (updates.login_rate_limit_max !== undefined) {
updates.login_rate_limit_max = clampInt(updates.login_rate_limit_max, 1, 100);
}
if (updates.daily_withdrawal_limit_usd !== undefined) {
updates.daily_withdrawal_limit_usd = clampInt(updates.daily_withdrawal_limit_usd, 1, 1000000);
}
if (updates.large_withdrawal_threshold_usd !== undefined) {
updates.large_withdrawal_threshold_usd = clampInt(updates.large_withdrawal_threshold_usd, 1, 1000000);
}
if (updates.candle_predict_engine_tick_ms !== undefined) {
updates.candle_predict_engine_tick_ms = clampInt(updates.candle_predict_engine_tick_ms, 1000, 60000);
}
if (updates.candle_predict_backfill_max_per_tick !== undefined) {
updates.candle_predict_backfill_max_per_tick = clampInt(updates.candle_predict_backfill_max_per_tick, 1, 50);
}
if (updates.candle_predict_backfill_interval_ms !== undefined) {
updates.candle_predict_backfill_interval_ms = clampInt(updates.candle_predict_backfill_interval_ms, 10000, 600000);
}
if (updates.candle_predict_backfill_seed_max !== undefined) {
updates.candle_predict_backfill_seed_max = clampInt(updates.candle_predict_backfill_seed_max, 5, 120);
}
if (updates.engine_audit_log_retention_days !== undefined) {
updates.engine_audit_log_retention_days = clampInt(updates.engine_audit_log_retention_days, 0, 365);
}
if (updates.candle_predict_retention_days !== undefined) {
updates.candle_predict_retention_days = clampInt(updates.candle_predict_retention_days, 0, 3650);
}
if (updates.candle_predict_retention_keep_last !== undefined) {
const raw = updates.candle_predict_retention_keep_last;
updates.candle_predict_retention_keep_last =
raw <= 0 ? 0 : clampInt(raw, 100, 100000);
}
if (updates.withdrawal_network_fee_usdt_micro !== undefined) {
updates.withdrawal_network_fee_usdt_micro = clampInt(updates.withdrawal_network_fee_usdt_micro, 0, 100000000);
}
if (updates.withdrawal_auto_retry_interval_minutes !== undefined) {
updates.withdrawal_auto_retry_interval_minutes = clampInt(updates.withdrawal_auto_retry_interval_minutes, 1, 1440);
}
if (updates.transfer_min_remaining_usdt_micro !== undefined) {
updates.transfer_min_remaining_usdt_micro = clampInt(updates.transfer_min_remaining_usdt_micro, 0, 1000000000000);
}
if (updates.login_max_failed_attempts !== undefined) {
updates.login_max_failed_attempts = clampInt(updates.login_max_failed_attempts, 0, 50);
}
if (updates.login_lockout_minutes !== undefined) {
updates.login_lockout_minutes = clampInt(updates.login_lockout_minutes, 0, 24 * 60);
}
if (updates.login_captcha_after_failures !== undefined) {
updates.login_captcha_after_failures = clampInt(updates.login_captcha_after_failures, 0, 20);
}
if (updates.max_concurrent_sessions !== undefined) {
updates.max_concurrent_sessions = clampInt(updates.max_concurrent_sessions, 0, 100);
}
if (updates.support_message_edit_window_minutes !== undefined) {
updates.support_message_edit_window_minutes = clampInt(updates.support_message_edit_window_minutes, 0, 10080);
}
if (updates.support_message_delete_window_minutes !== undefined) {
updates.support_message_delete_window_minutes = clampInt(updates.support_message_delete_window_minutes, 0, 10080);
}
}
/**
* Returns the current merged system settings (DB values with defaults).
* @returns Password policy, token expiry, and runtime ops configuration
*/
async function getSettings() {
return settingsRepository.getSettings();
}
/**
* Builds a partial settings update from an untyped HTTP request body.
* Only recognized keys with valid types are included.
* @param body Raw JSON body from the client
* @returns Partial settings safe to pass to the repository
*/
function parseSettingsUpdates(body) {
const updates = {};
if (typeof body.password_min_length === 'number') {
updates.password_min_length = body.password_min_length;
}
if (typeof body.password_require_uppercase === 'boolean') {
updates.password_require_uppercase = body.password_require_uppercase;
}
if (typeof body.password_require_lowercase === 'boolean') {
updates.password_require_lowercase = body.password_require_lowercase;
}
if (typeof body.password_require_number === 'boolean') {
updates.password_require_number = body.password_require_number;
}
if (typeof body.password_require_special === 'boolean') {
updates.password_require_special = body.password_require_special;
}
if (typeof body.access_token_expiry === 'string') {
updates.access_token_expiry = body.access_token_expiry;
}
if (typeof body.refresh_token_expiry === 'string') {
updates.refresh_token_expiry = body.refresh_token_expiry;
}
if (typeof body.require_email_verification_for_login === 'boolean') {
updates.require_email_verification_for_login = body.require_email_verification_for_login;
}
if (typeof body.allow_withdrawals === 'boolean') {
updates.allow_withdrawals = body.allow_withdrawals;
}
if (typeof body.network_usdt_trc20_deposit_enabled === 'boolean') {
updates.network_usdt_trc20_deposit_enabled = body.network_usdt_trc20_deposit_enabled;
}
if (typeof body.network_usdt_trc20_withdraw_enabled === 'boolean') {
updates.network_usdt_trc20_withdraw_enabled = body.network_usdt_trc20_withdraw_enabled;
}
if (typeof body.network_usdt_bep20_deposit_enabled === 'boolean') {
updates.network_usdt_bep20_deposit_enabled = body.network_usdt_bep20_deposit_enabled;
}
if (typeof body.network_usdt_bep20_withdraw_enabled === 'boolean') {
updates.network_usdt_bep20_withdraw_enabled = body.network_usdt_bep20_withdraw_enabled;
}
if (typeof body.rate_limit_window_ms === 'number') {
updates.rate_limit_window_ms = body.rate_limit_window_ms;
}
if (typeof body.rate_limit_max === 'number') {
updates.rate_limit_max = body.rate_limit_max;
}
if (typeof body.login_rate_limit_max === 'number') {
updates.login_rate_limit_max = body.login_rate_limit_max;
}
if (typeof body.daily_withdrawal_limit_usd === 'number') {
updates.daily_withdrawal_limit_usd = body.daily_withdrawal_limit_usd;
}
if (typeof body.large_withdrawal_threshold_usd === 'number') {
updates.large_withdrawal_threshold_usd = body.large_withdrawal_threshold_usd;
}
if (typeof body.candle_predict_engine_tick_ms === 'number') {
updates.candle_predict_engine_tick_ms = body.candle_predict_engine_tick_ms;
}
if (typeof body.candle_predict_backfill_max_per_tick === 'number') {
updates.candle_predict_backfill_max_per_tick = body.candle_predict_backfill_max_per_tick;
}
if (typeof body.candle_predict_backfill_interval_ms === 'number') {
updates.candle_predict_backfill_interval_ms = body.candle_predict_backfill_interval_ms;
}
if (typeof body.candle_predict_backfill_seed_max === 'number') {
updates.candle_predict_backfill_seed_max = body.candle_predict_backfill_seed_max;
}
if (typeof body.engine_audit_log_retention_days === 'number') {
updates.engine_audit_log_retention_days = body.engine_audit_log_retention_days;
}
if (typeof body.candle_predict_retention_days === 'number') {
updates.candle_predict_retention_days = body.candle_predict_retention_days;
}
if (typeof body.candle_predict_retention_keep_last === 'number') {
updates.candle_predict_retention_keep_last = body.candle_predict_retention_keep_last;
}
if (typeof body.withdrawal_network_fee_usdt_micro === 'number') {
updates.withdrawal_network_fee_usdt_micro = body.withdrawal_network_fee_usdt_micro;
}
if (typeof body.transfer_min_remaining_usdt_micro === 'number') {
updates.transfer_min_remaining_usdt_micro = body.transfer_min_remaining_usdt_micro;
}
if (typeof body.hot_wallet_bnb_gas_reserve === 'string' || typeof body.hot_wallet_bnb_gas_reserve === 'number') {
updates.hot_wallet_bnb_gas_reserve = (0, gasReserve_1.normalizeGasReserveInput)(body.hot_wallet_bnb_gas_reserve, 'BSC');
}
if (typeof body.hot_wallet_trx_gas_reserve === 'string' || typeof body.hot_wallet_trx_gas_reserve === 'number') {
updates.hot_wallet_trx_gas_reserve = (0, gasReserve_1.normalizeGasReserveInput)(body.hot_wallet_trx_gas_reserve, 'TRON');
}
if (typeof body.withdrawal_auto_retry_enabled === 'boolean') {
updates.withdrawal_auto_retry_enabled = body.withdrawal_auto_retry_enabled;
}
if (typeof body.withdrawal_auto_retry_interval_minutes === 'number') {
updates.withdrawal_auto_retry_interval_minutes = body.withdrawal_auto_retry_interval_minutes;
}
if (typeof body.require_admin_2fa === 'boolean') {
updates.require_admin_2fa = body.require_admin_2fa;
}
if (typeof body.require_withdrawal_2fa === 'boolean') {
updates.require_withdrawal_2fa = body.require_withdrawal_2fa;
}
if (typeof body.max_concurrent_sessions === 'number') {
updates.max_concurrent_sessions = body.max_concurrent_sessions;
}
if (typeof body.login_max_failed_attempts === 'number') {
updates.login_max_failed_attempts = body.login_max_failed_attempts;
}
if (typeof body.login_lockout_minutes === 'number') {
updates.login_lockout_minutes = body.login_lockout_minutes;
}
if (typeof body.login_captcha_after_failures === 'number') {
updates.login_captcha_after_failures = body.login_captcha_after_failures;
}
if (typeof body.support_message_edit_window_minutes === 'number') {
updates.support_message_edit_window_minutes = body.support_message_edit_window_minutes;
}
if (typeof body.support_message_delete_window_minutes === 'number') {
updates.support_message_delete_window_minutes = body.support_message_delete_window_minutes;
}
return updates;
}
/**
* Persists settings changes and returns the updated configuration.
* @param updates Partial settings from [parseSettingsUpdates]
* @returns Full settings after update
*/
async function updateSettings(updates, actor) {
validateUpdates(updates);
const changedKeys = Object.keys(updates);
if (changedKeys.length === 0) {
return settingsRepository.getSettings();
}
const beforeSettings = await settingsRepository.getSettings();
const before = (0, auditDiff_1.pickFields)(beforeSettings, changedKeys);
await settingsRepository.updateSettings(updates);
const afterSettings = await settingsRepository.getSettings();
const after = (0, auditDiff_1.pickFields)(afterSettings, changedKeys);
if (actor) {
await auditLogRepository.create({
userId: actor.userId,
action: 'settings_updated',
resourceType: 'system_settings',
ipAddress: actor.ipAddress ?? null,
userAgent: actor.userAgent ?? null,
metadata: {
changed: changedKeys,
before,
after,
},
});
}
return afterSettings;
}
//# sourceMappingURL=settingsService.js.map