games_app/backend/dist/utils/adminUserListQuery.js
2026-07-02 09:21:05 +02:00

190 lines
7.0 KiB
JavaScript

"use strict";
/**
* Shared query parsing for admin user list and export endpoints.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.ADMIN_USER_EXPORT_COLUMNS = exports.ADMIN_USER_SORT_FIELDS = void 0;
exports.parseBoolQuery = parseBoolQuery;
exports.parseIntQuery = parseIntQuery;
exports.parseAgeQuery = parseAgeQuery;
exports.parseDashboardPeriodBounds = parseDashboardPeriodBounds;
exports.parseAdminUserListOptions = parseAdminUserListOptions;
exports.sanitizeExportColumns = sanitizeExportColumns;
exports.ADMIN_USER_SORT_FIELDS = [
'created_at',
'id',
'role_name',
'is_enabled',
'email_verified',
'country_of_residence',
'main_balance_raw',
'bonus_balance_raw',
'total_bonus_received_raw',
'prediction_count',
'total_stake_raw',
'total_payout_raw',
'net_pnl_raw',
'last_login_at',
'last_activity_at',
];
function parseBoolQuery(value) {
if (value === true || value === 'true' || value === '1')
return true;
if (value === false || value === 'false' || value === '0')
return false;
return undefined;
}
function parseIntQuery(value) {
if (value === undefined || value === null || String(value).trim() === '')
return undefined;
const n = parseInt(String(value), 10);
return Number.isFinite(n) ? n : undefined;
}
function parseAgeQuery(value) {
if (value === undefined || value === null || String(value).trim() === '')
return undefined;
const n = parseInt(String(value), 10);
if (!Number.isFinite(n) || n < 0 || n > 150)
return undefined;
return n;
}
function q(source, key) {
const v = source[key];
return v === undefined || v === null ? '' : String(v);
}
function parseIdList(source) {
const raw = source['ids'] ?? source['selectedIds'] ?? source['userIds'];
if (Array.isArray(raw)) {
const ids = raw.map(String).map((s) => s.trim()).filter(Boolean);
return ids.length ? ids : undefined;
}
if (typeof raw === 'string' && raw.trim()) {
const ids = raw.split(',').map((s) => s.trim()).filter(Boolean);
return ids.length ? ids : undefined;
}
return undefined;
}
function parseActivityFilter(value) {
if (value === 'active_in_period' ||
value === 'inactive_before_period' ||
value === 'online_now' ||
value === 'online_app' ||
value === 'staff') {
return value;
}
return undefined;
}
/** Normalizes dashboard/report date query params to UTC bounds. */
function parseDashboardPeriodBounds(source) {
const fromRaw = q(source, 'from').trim();
const toRaw = q(source, 'to').trim();
if (!fromRaw && !toRaw)
return {};
const from = fromRaw.includes('T') ? fromRaw : `${fromRaw}T00:00:00.000Z`;
const to = toRaw.includes('T') ? toRaw : `${toRaw}T23:59:59.999Z`;
return {
...(fromRaw ? { from } : {}),
...(toRaw ? { to } : {}),
};
}
function parseAdminUserListOptions(source, overrides) {
const search = q(source, 'search');
const role = q(source, 'role');
const sortBy = q(source, 'sortBy');
const sortDir = q(source, 'sortDir');
const limit = Math.min(parseInt(q(source, 'limit') || '50', 10) || 50, 100);
const offset = Math.max(parseInt(q(source, 'offset') || '0', 10) || 0, 0);
const cursor = q(source, 'cursor').trim() || undefined;
const includeTotalParam = parseBoolQuery(q(source, 'includeTotal') || undefined);
const includeTotal = includeTotalParam ?? (!cursor && offset === 0);
const accountStatus = q(source, 'accountStatus');
const activityFilter = parseActivityFilter(q(source, 'activityFilter'));
const periodBounds = parseDashboardPeriodBounds(source);
const registrationInPeriod = parseBoolQuery(q(source, 'registrationInPeriod') || undefined);
const minAge = parseAgeQuery(q(source, 'minAge') || undefined);
const maxAge = parseAgeQuery(q(source, 'maxAge') || undefined);
return {
search: search.trim() || undefined,
roleName: role === 'admin' || role === 'moderator' || role === 'user' || role === 'super_admin'
? role
: undefined,
emailVerified: parseBoolQuery(q(source, 'emailVerified') || undefined),
country: q(source, 'country').trim() || undefined,
inactiveSinceDays: parseIntQuery(q(source, 'inactiveSinceDays') || undefined),
activityFilter,
activityPeriodFrom: activityFilter ? periodBounds.from : undefined,
activityPeriodTo: activityFilter ? periodBounds.to : undefined,
minMainBalance: q(source, 'minMainBalance') || undefined,
maxMainBalance: q(source, 'maxMainBalance') || undefined,
minBonusReceived: q(source, 'minBonusReceived') || undefined,
maxBonusReceived: q(source, 'maxBonusReceived') || undefined,
accountStatus: accountStatus === 'active' || accountStatus === 'disabled' || accountStatus === 'suspended'
? accountStatus
: undefined,
hasReceivedBonus: parseBoolQuery(q(source, 'hasReceivedBonus') || undefined),
createdFrom: registrationInPeriod ? periodBounds.from : q(source, 'createdFrom') || undefined,
createdTo: registrationInPeriod ? periodBounds.to : q(source, 'createdTo') || undefined,
minAge,
maxAge,
totpEnabled: parseBoolQuery(q(source, 'totpEnabled') || undefined),
underSurveillance: parseBoolQuery(q(source, 'underSurveillance') || undefined),
sortBy: exports.ADMIN_USER_SORT_FIELDS.includes(sortBy)
? sortBy
: undefined,
sortDir: sortDir.toLowerCase() === 'asc' || sortDir.toLowerCase() === 'desc'
? sortDir.toLowerCase()
: undefined,
limit,
offset,
cursor,
includeTotal,
userIds: parseIdList(source),
...overrides,
};
}
exports.ADMIN_USER_EXPORT_COLUMNS = [
'id',
'email',
'full_name',
'account_status',
'role_name',
'country_of_residence',
'nationality',
'date_of_birth',
'age_category',
'referral_code',
'first_deposit_at',
'first_deposit_transaction_id',
'email_verified',
'main_balance_raw',
'bonus_balance_raw',
'total_bonus_received_raw',
'prediction_count',
'net_pnl_raw',
'total_stake_raw',
'total_payout_raw',
'created_at',
'last_login_at',
'last_activity_at',
'is_enabled',
'withdrawals_suspended',
'under_surveillance',
'totp_enabled',
'is_account_non_expired',
'is_account_non_locked',
'is_credentials_non_expired',
];
function sanitizeExportColumns(columns) {
if (!Array.isArray(columns) || columns.length === 0) {
return [...exports.ADMIN_USER_EXPORT_COLUMNS];
}
const allowed = new Set(exports.ADMIN_USER_EXPORT_COLUMNS);
const out = [];
for (const c of columns) {
if (typeof c === 'string' && allowed.has(c) && !out.includes(c)) {
out.push(c);
}
}
return out.length > 0 ? out : [...exports.ADMIN_USER_EXPORT_COLUMNS];
}
//# sourceMappingURL=adminUserListQuery.js.map