games_app/backend/src/services/settingsService.ts
2026-08-24 13:41:54 +02:00

443 lines
9.4 KiB
TypeScript

/**
* System settings service.
* Applies validation and business rules for admin configuration updates.
*/
import * as settingsRepository from '../repositories/settingsRepository';
import { SystemSettings } from '../models';
import * as auditLogRepository from '../repositories/auditLogRepository';
import { diffFields, pickFields } from '../utils/auditDiff';
function clampInt(value: number, min: number, max: number): number {
return Math.min(max, Math.max(min, value));
}
function validateUpdates(updates: Partial<SystemSettings>): void {
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, 60_000, 3_600_000);
}
if (updates.rate_limit_max !== undefined) {
updates.rate_limit_max = clampInt(updates.rate_limit_max, 10, 10_000);
}
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, 1_000_000);
}
if (updates.large_withdrawal_threshold_usd !== undefined) {
updates.large_withdrawal_threshold_usd = clampInt(
updates.large_withdrawal_threshold_usd,
1,
1_000_000
);
}
if (updates.candle_predict_engine_tick_ms !== undefined) {
updates.candle_predict_engine_tick_ms = clampInt(
updates.candle_predict_engine_tick_ms,
1000,
60_000
);
}
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,
10_000,
600_000
);
}
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.withdrawal_network_fee_usdt_micro !== undefined) {
updates.withdrawal_network_fee_usdt_micro = clampInt(
updates.withdrawal_network_fee_usdt_micro,
0,
100_000_000
);
}
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);
}
}
/**
* Returns the current merged system settings (DB values with defaults).
* @returns Password policy, token expiry, and runtime ops configuration
*/
export async function getSettings(): Promise<SystemSettings> {
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
*/
export function parseSettingsUpdates(body: Record<string, unknown>): Partial<SystemSettings> {
const updates: Partial<SystemSettings> = {};
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.withdrawal_network_fee_usdt_micro === 'number') {
updates.withdrawal_network_fee_usdt_micro = body.withdrawal_network_fee_usdt_micro;
}
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;
}
return updates;
}
/**
* Persists settings changes and returns the updated configuration.
* @param updates Partial settings from [parseSettingsUpdates]
* @returns Full settings after update
*/
export async function updateSettings(
updates: Partial<SystemSettings>,
actor?: { userId: string; ipAddress?: string; userAgent?: string }
): Promise<SystemSettings> {
validateUpdates(updates);
const changedKeys = Object.keys(updates) as (keyof SystemSettings)[];
if (changedKeys.length === 0) {
return settingsRepository.getSettings();
}
const beforeSettings = await settingsRepository.getSettings();
const before = pickFields(beforeSettings as unknown as Record<string, unknown>, changedKeys);
await settingsRepository.updateSettings(updates);
const afterSettings = await settingsRepository.getSettings();
const after = pickFields(afterSettings as unknown as Record<string, unknown>, 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;
}