206 lines
7.0 KiB
TypeScript
206 lines
7.0 KiB
TypeScript
/**
|
|
* Shared query parsing for admin user list and export endpoints.
|
|
*/
|
|
|
|
import { RoleName } from '../models';
|
|
import type { AdminSortBy, UserActivityFilter } from '../repositories/userRepository';
|
|
import type { AdminListUsersOptions } from '../services/adminService';
|
|
|
|
export const ADMIN_USER_SORT_FIELDS: AdminSortBy[] = [
|
|
'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',
|
|
];
|
|
|
|
export function parseBoolQuery(value: string | boolean | undefined): boolean | undefined {
|
|
if (value === true || value === 'true' || value === '1') return true;
|
|
if (value === false || value === 'false' || value === '0') return false;
|
|
return undefined;
|
|
}
|
|
|
|
export function parseIntQuery(value: string | undefined): number | undefined {
|
|
if (value === undefined || value === null || String(value).trim() === '') return undefined;
|
|
const n = parseInt(String(value), 10);
|
|
return Number.isFinite(n) ? n : undefined;
|
|
}
|
|
|
|
export function parseAgeQuery(value: string | undefined): number | undefined {
|
|
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;
|
|
}
|
|
|
|
type QuerySource = Record<string, unknown> | { [key: string]: string | undefined };
|
|
|
|
function q(source: QuerySource, key: string): string {
|
|
const v = source[key];
|
|
return v === undefined || v === null ? '' : String(v);
|
|
}
|
|
|
|
function parseIdList(source: QuerySource): string[] | undefined {
|
|
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: string): UserActivityFilter | undefined {
|
|
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. */
|
|
export function parseDashboardPeriodBounds(source: QuerySource): {
|
|
from?: string;
|
|
to?: string;
|
|
} {
|
|
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 } : {}),
|
|
};
|
|
}
|
|
|
|
export function parseAdminUserListOptions(
|
|
source: QuerySource,
|
|
overrides?: Partial<AdminListUsersOptions>
|
|
): AdminListUsersOptions {
|
|
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 as RoleName) : 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: ADMIN_USER_SORT_FIELDS.includes(sortBy as AdminSortBy)
|
|
? (sortBy as AdminSortBy)
|
|
: undefined,
|
|
sortDir:
|
|
sortDir.toLowerCase() === 'asc' || sortDir.toLowerCase() === 'desc'
|
|
? (sortDir.toLowerCase() as 'asc' | 'desc')
|
|
: undefined,
|
|
limit,
|
|
offset,
|
|
cursor,
|
|
includeTotal,
|
|
userIds: parseIdList(source),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
export const 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',
|
|
] as const;
|
|
|
|
export type AdminUserExportColumn = (typeof ADMIN_USER_EXPORT_COLUMNS)[number];
|
|
|
|
export function sanitizeExportColumns(columns: unknown): AdminUserExportColumn[] {
|
|
if (!Array.isArray(columns) || columns.length === 0) {
|
|
return [...ADMIN_USER_EXPORT_COLUMNS];
|
|
}
|
|
const allowed = new Set<string>(ADMIN_USER_EXPORT_COLUMNS);
|
|
const out: AdminUserExportColumn[] = [];
|
|
for (const c of columns) {
|
|
if (typeof c === 'string' && allowed.has(c) && !out.includes(c as AdminUserExportColumn)) {
|
|
out.push(c as AdminUserExportColumn);
|
|
}
|
|
}
|
|
return out.length > 0 ? out : [...ADMIN_USER_EXPORT_COLUMNS];
|
|
}
|