"use strict"; /** * Admin controller — HTTP handlers for admin user management endpoints. * Delegates all business logic to adminService. */ 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.listUsers = listUsers; exports.startUsersExport = startUsersExport; exports.getUsersExportStatus = getUsersExportStatus; exports.downloadUsersExport = downloadUsersExport; exports.updateUserFlags = updateUserFlags; exports.batchUpdateUserFlags = batchUpdateUserFlags; exports.batchUpdateUserSecurity = batchUpdateUserSecurity; exports.batchGrantLedgerCredit = batchGrantLedgerCredit; exports.verifyUserEmail = verifyUserEmail; exports.getUserDetails = getUserDetails; exports.updateUserRole = updateUserRole; exports.updateUserRoles = updateUserRoles; exports.deleteUser = deleteUser; const securityAudit_1 = require("../utils/securityAudit"); const adminService = __importStar(require("../services/adminService")); const adminUserExportService = __importStar(require("../services/adminUserExportService")); const adminUserListQuery_1 = require("../utils/adminUserListQuery"); /** * Lists users for the admin panel with optional search, filters, sorting, and pagination. */ async function listUsers(req, res) { try { const options = (0, adminUserListQuery_1.parseAdminUserListOptions)(req.query); const minAge = options.minAge; const maxAge = options.maxAge; if (minAge !== undefined && maxAge !== undefined && minAge > maxAge) { res.status(400).json({ error: 'minAge cannot be greater than maxAge' }); return; } const result = await adminService.listUsers(options); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Failed to list users'; res.status(500).json({ error: msg }); } } async function startUsersExport(req, res) { try { const body = req.body ?? {}; const minAge = (0, adminUserListQuery_1.parseAgeQuery)(body.minAge); const maxAge = (0, adminUserListQuery_1.parseAgeQuery)(body.maxAge); if (minAge !== undefined && maxAge !== undefined && minAge > maxAge) { res.status(400).json({ error: 'minAge cannot be greater than maxAge' }); return; } const listOptions = (0, adminUserListQuery_1.parseAdminUserListOptions)(body, { limit: 100, includeTotal: false }); const userIds = Array.isArray(body.userIds) ? body.userIds.filter((id) => typeof id === 'string' && id.trim()).map((id) => id.trim()) : undefined; const maxRowsRaw = body.maxRows; const maxRows = maxRowsRaw === undefined || maxRowsRaw === null || String(maxRowsRaw).trim() === '' ? undefined : parseInt(String(maxRowsRaw), 10); const job = await adminUserExportService.startAdminUserExport(req.user.userId, { ...listOptions, columns: (0, adminUserListQuery_1.sanitizeExportColumns)(body.columns), maxRows: Number.isFinite(maxRows) && (maxRows ?? 0) > 0 ? maxRows : undefined, userIds: userIds?.length ? userIds : undefined, format: body.format === 'csv' ? 'csv' : 'xlsx', }); res.status(202).json(job); } catch (e) { const msg = e instanceof Error ? e.message : 'Failed to start export'; res.status(500).json({ error: msg }); } } async function getUsersExportStatus(req, res) { try { const job = await adminUserExportService.getAdminUserExportJob(req.params.jobId, req.user.userId); if (!job) { res.status(404).json({ error: 'Export job not found' }); return; } res.json(job); } catch (e) { const msg = e instanceof Error ? e.message : 'Failed to get export status'; res.status(500).json({ error: msg }); } } async function downloadUsersExport(req, res) { try { const file = await adminUserExportService.getAdminUserExportFilePath(req.params.jobId, req.user.userId); if (!file) { res.status(404).json({ error: 'Export file not ready' }); return; } const mime = file.fileName.endsWith('.csv') ? 'text/csv; charset=utf-8' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; res.download(file.filePath, file.fileName, { headers: { 'Content-Type': mime } }); } catch (e) { const msg = e instanceof Error ? e.message : 'Failed to download export'; res.status(500).json({ error: msg }); } } async function updateUserFlags(req, res) { try { const userId = req.params.userId; const { isEnabled, isAccountNonExpired, isAccountNonLocked, isCredentialsNonExpired } = req.body; const ctx = (0, securityAudit_1.auditContextFromRequest)(req); await adminService.updateUserAccountFlags(userId, { isEnabled, isAccountNonExpired, isAccountNonLocked, isCredentialsNonExpired, }, { adminUserId: req.user.userId, ipAddress: ctx.ipAddress, userAgent: ctx.userAgent }); res.json({ message: 'Updated' }); } catch (e) { const msg = e instanceof Error ? e.message : 'Update failed'; const status = msg === 'User not found' ? 404 : 400; res.status(status).json({ error: msg }); } } async function batchUpdateUserFlags(req, res) { try { const { userIds, ...flags } = req.body; if (!Array.isArray(userIds) || userIds.length === 0) { res.status(400).json({ error: 'userIds array is required' }); return; } const ctx = (0, securityAudit_1.auditContextFromRequest)(req); const result = await adminService.batchUpdateUserAccountFlags(userIds, flags, { adminUserId: req.user.userId, ipAddress: ctx.ipAddress, userAgent: ctx.userAgent, }); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Batch update failed'; res.status(400).json({ error: msg }); } } async function batchUpdateUserSecurity(req, res) { try { const { userIds, withdrawalsSuspended, underSurveillance, reactivateWithdrawals, reason } = req.body; if (!Array.isArray(userIds) || userIds.length === 0) { res.status(400).json({ error: 'userIds array is required' }); return; } const ctx = (0, securityAudit_1.auditContextFromRequest)(req); const result = await adminService.batchUpdateUserSecurity(userIds, { withdrawalsSuspended, underSurveillance, reactivateWithdrawals, reason, }, { adminUserId: req.user.userId, ipAddress: ctx.ipAddress, userAgent: ctx.userAgent, }); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Batch security update failed'; res.status(400).json({ error: msg }); } } async function batchGrantLedgerCredit(req, res) { try { const { userIds, walletTypeId, adjustmentType, amountRaw, reason } = req.body; if (!Array.isArray(userIds) || userIds.length === 0) { res.status(400).json({ error: 'userIds array is required' }); return; } if (!walletTypeId || !adjustmentType || !amountRaw || !reason) { res.status(400).json({ error: 'walletTypeId, adjustmentType, amountRaw, and reason are required', }); return; } const result = await adminService.batchGrantLedgerCredit(userIds, { walletTypeId, adjustmentType, amountRaw, reason }, req.user.userId); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Batch credit failed'; res.status(400).json({ error: msg }); } } async function verifyUserEmail(req, res) { try { const userId = req.params.userId; const adminUserId = req.user.userId; const result = await adminService.markUserEmailVerified(userId, adminUserId); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Verification failed'; const status = msg === 'User not found' ? 404 : 400; res.status(status).json({ error: msg }); } } async function getUserDetails(req, res) { try { const userId = req.params.userId; const details = await adminService.getUserDetails(userId); res.json(details); } catch (e) { const msg = e instanceof Error ? e.message : 'Failed to load user details'; const status = msg === 'User not found' ? 404 : 500; res.status(status).json({ error: msg }); } } async function updateUserRole(req, res) { try { const userId = req.params.userId; const { role } = req.body; if (!role) { res.status(400).json({ error: 'role is required' }); return; } const ctx = (0, securityAudit_1.auditContextFromRequest)(req); const result = await adminService.updateUserRole(userId, role, req.user.userId, ctx); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Failed to update role'; const status = msg === 'User not found' ? 404 : 400; res.status(status).json({ error: msg }); } } async function updateUserRoles(req, res) { try { const userId = req.params.userId; const { roles } = req.body; if (!roles?.length) { res.status(400).json({ error: 'roles array is required' }); return; } const ctx = (0, securityAudit_1.auditContextFromRequest)(req); const result = await adminService.updateUserRoles(userId, roles, req.user.userId, ctx); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Failed to update roles'; res.status(400).json({ error: msg }); } } async function deleteUser(req, res) { try { const userId = req.params.userId; const { reason } = (req.body ?? {}); const ctx = (0, securityAudit_1.auditContextFromRequest)(req); const result = await adminService.deleteUser(userId, req.user.userId, { reason, ipAddress: ctx.ipAddress, userAgent: ctx.userAgent, }); res.json(result); } catch (e) { const msg = e instanceof Error ? e.message : 'Delete failed'; const status = msg === 'User not found' ? 404 : msg.includes('cannot delete') || msg.includes('must remain') ? 403 : 400; res.status(status).json({ error: msg }); } } //# sourceMappingURL=adminController.js.map