-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin controller.js
More file actions
654 lines (560 loc) · 20.6 KB
/
admin controller.js
File metadata and controls
654 lines (560 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// ==================== ADMIN CONTROLLER ====================
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const { Pool } = require('pg');
const Redis = require('ioredis');
const axios = require('axios');
const app = express();
const PORT = process.env.ADMIN_PORT || 3001;
app.use(helmet());
app.use(cors({ origin: process.env.FRONTEND_URL || '*', credentials: true }));
app.use(express.json());
const pool = new Pool({
host: process.env.DB_HOST || 'localhost',
port: process.env.DB_PORT || 5432,
database: process.env.DB_NAME || 'zec_arena',
user: process.env.DB_USER || 'postgres',
password: process.env.DB_PASSWORD,
max: 20
});
const redis = new Redis({
host: process.env.REDIS_HOST || 'localhost',
port: process.env.REDIS_PORT || 6379,
password: process.env.REDIS_PASSWORD
});
// ==================== ERROR HANDLING ====================
const ERROR_CODES = {
ADMIN_ACCESS_REQUIRED: 6000,
INVALID_ADMIN_KEY: 6001,
OPERATION_NOT_ALLOWED: 6002,
USER_NOT_FOUND: 1005,
GAME_NOT_FOUND: 4000,
WITHDRAWAL_NOT_FOUND: 2009,
SERVICE_UNAVAILABLE: 5005,
DATABASE_ERROR: 5000
};
function buildErrorResponse(errorCode, details = {}) {
const errors = {
[ERROR_CODES.ADMIN_ACCESS_REQUIRED]: {
message: 'Admin access required',
userMessage: 'You don\'t have permission to access this resource.',
action: 'Contact an administrator'
},
[ERROR_CODES.INVALID_ADMIN_KEY]: {
message: 'Invalid admin key',
userMessage: 'The admin API key provided is invalid.',
action: 'Check your API key configuration'
},
[ERROR_CODES.OPERATION_NOT_ALLOWED]: {
message: 'Operation not allowed',
userMessage: `This operation cannot be performed: ${details.reason || 'Unknown reason'}`,
action: details.suggestion || 'Review the operation requirements'
},
[ERROR_CODES.USER_NOT_FOUND]: {
message: 'User not found',
userMessage: `No user found with ID: ${details.playerId}`,
action: 'Check the Player ID and try again'
},
[ERROR_CODES.GAME_NOT_FOUND]: {
message: 'Game not found',
userMessage: `No game found with ID: ${details.gameId}`,
action: 'Check the Game ID and try again'
},
[ERROR_CODES.WITHDRAWAL_NOT_FOUND]: {
message: 'Withdrawal not found',
userMessage: `No withdrawal found with ID: ${details.withdrawalId}`,
action: 'Check the Withdrawal ID and try again'
},
[ERROR_CODES.SERVICE_UNAVAILABLE]: {
message: 'Service unavailable',
userMessage: `The ${details.service} service is currently unavailable.`,
action: 'Check service health or try again later'
},
[ERROR_CODES.DATABASE_ERROR]: {
message: 'Database error',
userMessage: 'A database error occurred.',
action: 'Check database connection and try again'
}
};
const errorInfo = errors[errorCode] || {
message: 'An error occurred',
userMessage: 'Something went wrong. Please try again.',
action: 'If the problem persists, contact support'
};
return {
error: true,
errorCode,
message: errorInfo.message,
userMessage: errorInfo.userMessage,
action: errorInfo.action,
details,
timestamp: new Date().toISOString(),
support: 'admin@zecarena.com'
};
}
// ==================== ADMIN AUTH ====================
function requireAdminKey(req, res, next) {
const apiKey = req.headers['x-api-key'];
if (!process.env.ADMIN_API_KEY || apiKey === process.env.ADMIN_API_KEY) {
return next();
}
res.status(403).json({ error: 'Admin access required' });
}
app.use(requireAdminKey);
// ==================== SERVICE URLS ====================
const SERVICES = {
AUTH: process.env.AUTH_SERVICE_URL || 'http://localhost:3002',
WALLET: process.env.WALLET_SERVICE_URL || 'http://localhost:3003',
GAME: process.env.GAME_SERVICE_URL || 'http://localhost:3004',
MONITOR: process.env.MONITOR_SERVICE_URL || 'http://localhost:3005'
};
// ==================== DASHBOARD ====================
app.get('/dashboard', async (req, res) => {
try {
const [users, games, transactions, platformStats] = await Promise.all([
pool.query('SELECT COUNT(*) as total, COUNT(*) FILTER (WHERE is_verified = TRUE) as verified FROM users'),
pool.query('SELECT COUNT(*) as total, COUNT(*) FILTER (WHERE status = \'IN_PROGRESS\') as active FROM games'),
pool.query(`
SELECT
COUNT(*) FILTER (WHERE type = 'DEPOSIT' AND status = 'confirmed') as deposits,
COUNT(*) FILTER (WHERE type = 'WITHDRAW' AND status IN ('confirmed', 'confirming')) as withdrawals,
COALESCE(SUM(amount) FILTER (WHERE type = 'DEPOSIT' AND status = 'confirmed'), 0) as total_deposits,
COALESCE(SUM(amount) FILTER (WHERE type = 'WITHDRAW' AND status IN ('confirmed', 'confirming')), 0) as total_withdrawals,
COALESCE(SUM(amount) FILTER (WHERE type = 'FEE'), 0) as total_fees
FROM transactions
`),
pool.query(`
SELECT
COALESCE(SUM(balance), 0) as total_user_balance,
COUNT(*) FILTER (WHERE last_login > NOW() - INTERVAL '24 hours') as active_24h
FROM users
`)
]);
const [authHealth, walletHealth, gameHealth, monitorHealth] = await Promise.allSettled([
axios.get(`${SERVICES.AUTH}/health`).then(r => r.data),
axios.get(`${SERVICES.WALLET}/health`).then(r => r.data),
axios.get(`${SERVICES.GAME}/health`).then(r => r.data),
axios.get(`${SERVICES.MONITOR}/health`).then(r => r.data)
]);
res.json({
users: {
total: parseInt(users.rows[0].total),
verified: parseInt(users.rows[0].verified),
active24h: parseInt(platformStats.rows[0].active_24h)
},
games: {
total: parseInt(games.rows[0].total),
active: parseInt(games.rows[0].active)
},
transactions: {
deposits: parseInt(transactions.rows[0].deposits),
withdrawals: parseInt(transactions.rows[0].withdrawals),
totalDepositsAmount: parseFloat(transactions.rows[0].total_deposits),
totalWithdrawalsAmount: parseFloat(transactions.rows[0].total_withdrawals),
totalFees: parseFloat(transactions.rows[0].total_fees)
},
platform: {
totalUserBalance: parseFloat(platformStats.rows[0].total_user_balance),
revenue: parseFloat(transactions.rows[0].total_fees)
},
services: {
auth: authHealth.status === 'fulfilled' ? authHealth.value : { status: 'unhealthy' },
wallet: walletHealth.status === 'fulfilled' ? walletHealth.value : { status: 'unhealthy' },
game: gameHealth.status === 'fulfilled' ? gameHealth.value : { status: 'unhealthy' },
monitor: monitorHealth.status === 'fulfilled' ? monitorHealth.value : { status: 'unhealthy' }
}
});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch dashboard' });
}
});
// ==================== USERS MANAGEMENT ====================
app.get('/users', async (req, res) => {
try {
const limit = Math.min(parseInt(req.query.limit) || 100, 500);
const offset = parseInt(req.query.offset) || 0;
const search = req.query.search;
let query = 'SELECT player_id, username, email, balance, is_verified, wallet_address, last_login, created_at FROM users';
const params = [];
if (search) {
query += ' WHERE player_id ILIKE $1 OR username ILIKE $1 OR email ILIKE $1';
params.push(`%${search}%`);
}
query += ` ORDER BY created_at DESC LIMIT $${params.length + 1} OFFSET $${params.length + 2}`;
params.push(limit, offset);
const result = await pool.query(query, params);
res.json({
users: result.rows.map(u => ({
playerId: u.player_id,
username: u.username,
email: u.email,
balance: parseFloat(u.balance),
isVerified: u.is_verified,
walletAddress: u.wallet_address,
lastLogin: u.last_login,
createdAt: u.created_at
})),
limit,
offset
});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch users' });
}
});
app.get('/users/:playerId', async (req, res) => {
try {
const { playerId } = req.params;
const [user, txCount, gameCount] = await Promise.all([
pool.query('SELECT * FROM users WHERE player_id = $1', [playerId]),
pool.query('SELECT COUNT(*) as count FROM transactions WHERE player_id = $1', [playerId]),
pool.query('SELECT COUNT(*) as count FROM game_players WHERE player_id = $1', [playerId])
]);
if (user.rows.length === 0) {
return res.status(404).json({ error: 'User not found' });
}
res.json({
...user.rows[0],
balance: parseFloat(user.rows[0].balance),
transactionCount: parseInt(txCount.rows[0].count),
gameCount: parseInt(gameCount.rows[0].count)
});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch user' });
}
});
app.put('/users/:playerId/verify', async (req, res) => {
try {
const { playerId } = req.params;
await pool.query(
'UPDATE users SET is_verified = TRUE, updated_at = NOW() WHERE player_id = $1',
[playerId]
);
res.json({ success: true, message: 'User verified' });
} catch (error) {
res.status(500).json({ error: 'Failed to verify user' });
}
});
app.put('/users/:playerId/balance', async (req, res) => {
try {
const { playerId } = req.params;
const { amount, reason } = req.body;
await pool.query('BEGIN');
await pool.query(
'UPDATE users SET balance = balance + $1, updated_at = NOW() WHERE player_id = $2',
[amount, playerId]
);
await pool.query(
'INSERT INTO transactions (player_id, type, amount, status, memo) VALUES ($1, $2, $3, $4, $5)',
[playerId, amount > 0 ? 'DEPOSIT' : 'WITHDRAW', Math.abs(amount), 'confirmed', `Admin adjustment: ${reason}`]
);
await pool.query('COMMIT');
res.json({ success: true, message: 'Balance updated' });
} catch (error) {
await pool.query('ROLLBACK');
res.status(500).json({ error: 'Failed to update balance' });
}
});
// ==================== GAMES MANAGEMENT ====================
app.get('/games', async (req, res) => {
try {
const limit = Math.min(parseInt(req.query.limit) || 100, 500);
const offset = parseInt(req.query.offset) || 0;
const status = req.query.status;
let query = 'SELECT * FROM games';
const params = [];
if (status) {
query += ' WHERE status = $1';
params.push(status);
}
query += ` ORDER BY created_at DESC LIMIT $${params.length + 1} OFFSET $${params.length + 2}`;
params.push(limit, offset);
const result = await pool.query(query, params);
res.json({
games: result.rows.map(g => ({
gameId: g.game_id,
gameType: g.game_type,
stake: parseFloat(g.stake),
pot: parseFloat(g.pot),
maxPlayers: g.max_players,
currentPlayers: g.current_players,
status: g.status,
winnerId: g.winner_id,
hostId: g.host_id,
startTime: g.start_time,
endTime: g.end_time,
createdAt: g.created_at
})),
limit,
offset
});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch games' });
}
});
app.delete('/games/:gameId', async (req, res) => {
const client = await pool.connect();
try {
const { gameId } = req.params;
const { reason } = req.body;
await client.query('BEGIN');
const game = await client.query(
'SELECT * FROM games WHERE game_id = $1',
[gameId]
);
if (game.rows.length === 0) {
await client.query('ROLLBACK');
return res.status(404).json({ error: 'Game not found' });
}
const players = await client.query(
'SELECT player_id FROM game_players WHERE game_id = $1',
[gameId]
);
const stake = parseFloat(game.rows[0].stake);
if (stake > 0) {
for (const p of players.rows) {
await client.query(
'UPDATE users SET balance = balance + $1 WHERE player_id = $2',
[stake, p.player_id]
);
await client.query(
'INSERT INTO transactions (player_id, type, amount, status, metadata) VALUES ($1, $2, $3, $4, $5)',
[p.player_id, 'REFUND', stake, 'confirmed', JSON.stringify({ gameId, reason: reason || 'Admin cancelled' })]
);
}
}
await client.query(
'UPDATE games SET status = $1 WHERE game_id = $2',
['CANCELLED', gameId]
);
await client.query('COMMIT');
res.json({ success: true, message: 'Game cancelled and refunded' });
} catch (error) {
await client.query('ROLLBACK');
res.status(500).json({ error: 'Failed to cancel game' });
} finally {
client.release();
}
});
// ==================== TRANSACTIONS ====================
app.get('/transactions', async (req, res) => {
try {
const limit = Math.min(parseInt(req.query.limit) || 100, 500);
const offset = parseInt(req.query.offset) || 0;
const type = req.query.type;
const status = req.query.status;
let query = 'SELECT * FROM transactions WHERE 1=1';
const params = [];
if (type) {
query += ` AND type = $${params.length + 1}`;
params.push(type);
}
if (status) {
query += ` AND status = $${params.length + 1}`;
params.push(status);
}
query += ` ORDER BY created_at DESC LIMIT $${params.length + 1} OFFSET $${params.length + 2}`;
params.push(limit, offset);
const result = await pool.query(query, params);
res.json({
transactions: result.rows.map(tx => ({
txId: tx.tx_id,
playerId: tx.player_id,
type: tx.type,
amount: parseFloat(tx.amount),
status: tx.status,
zcashTxid: tx.zcash_txid,
confirmations: tx.confirmations,
createdAt: tx.created_at
})),
limit,
offset
});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch transactions' });
}
});
// ==================== WITHDRAWALS ====================
app.get('/withdrawals', async (req, res) => {
try {
const status = req.query.status || 'pending';
const result = await pool.query(
`SELECT wq.*, u.username
FROM withdrawal_queue wq
JOIN users u ON wq.player_id = u.player_id
WHERE wq.status = $1
ORDER BY wq.created_at ASC`,
[status]
);
res.json({
withdrawals: result.rows.map(w => ({
withdrawalId: w.withdrawal_id,
playerId: w.player_id,
username: w.username,
amount: parseFloat(w.amount),
toAddress: w.to_address,
status: w.status,
zcashTxid: w.zcash_txid,
retryCount: w.retry_count,
errorMessage: w.error_message,
createdAt: w.created_at
}))
});
} catch (error) {
res.status(500).json({ error: 'Failed to fetch withdrawals' });
}
});
app.post('/withdrawals/:withdrawalId/cancel', async (req, res) => {
const client = await pool.connect();
try {
const { withdrawalId } = req.params;
const { reason } = req.body;
await client.query('BEGIN');
const withdrawal = await client.query(
'SELECT * FROM withdrawal_queue WHERE withdrawal_id = $1',
[withdrawalId]
);
if (withdrawal.rows.length === 0) {
await client.query('ROLLBACK');
return res.status(404).json({ error: 'Withdrawal not found' });
}
const w = withdrawal.rows[0];
if (w.status === 'completed') {
await client.query('ROLLBACK');
return res.status(400).json({ error: 'Cannot cancel completed withdrawal' });
}
await client.query(
'UPDATE withdrawal_queue SET status = $1, error_message = $2 WHERE withdrawal_id = $3',
['cancelled', reason || 'Cancelled by admin', withdrawalId]
);
const refundAmount = parseFloat(w.amount) + parseFloat(process.env.WITHDRAWAL_FEE || 0.0001);
await client.query(
'UPDATE users SET balance = balance + $1 WHERE player_id = $2',
[refundAmount, w.player_id]
);
await client.query(
'UPDATE transactions SET status = $1 WHERE player_id = $2 AND type = $3 AND amount = $4 AND status = $5',
['cancelled', w.player_id, 'WITHDRAW', w.amount, 'pending']
);
await client.query('COMMIT');
res.json({ success: true, message: 'Withdrawal cancelled and refunded' });
} catch (error) {
await client.query('ROLLBACK');
res.status(500).json({ error: 'Failed to cancel withdrawal' });
} finally {
client.release();
}
});
// ==================== MONITORING ====================
app.get('/monitoring/stats', async (req, res) => {
try {
const response = await axios.get(`${SERVICES.MONITOR}/stats`);
res.json(response.data);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch monitoring stats' });
}
});
app.post('/monitoring/trigger-deposits', async (req, res) => {
try {
const response = await axios.post(`${SERVICES.MONITOR}/trigger-deposits`);
res.json(response.data);
} catch (error) {
res.status(500).json({ error: 'Failed to trigger deposit monitoring' });
}
});
app.post('/monitoring/trigger-withdrawals', async (req, res) => {
try {
const response = await axios.post(`${SERVICES.MONITOR}/trigger-withdrawals`);
res.json(response.data);
} catch (error) {
res.status(500).json({ error: 'Failed to trigger withdrawal processing' });
}
});
// ==================== LOGS ====================
app.get('/logs', async (req, res) => {
try {
const limit = Math.min(parseInt(req.query.limit) || 100, 500);
const action = req.query.action;
let query = 'SELECT * FROM admin_logs';
const params = [];
if (action) {
query += ' WHERE action = $1';
params.push(action);
}
query += ` ORDER BY created_at DESC LIMIT $${params.length + 1}`;
params.push(limit);
const result = await pool.query(query, params);
res.json({ logs: result.rows });
} catch (error) {
res.status(500).json({ error: 'Failed to fetch logs' });
}
});
app.post('/logs', async (req, res) => {
try {
const { action, details } = req.body;
await pool.query(
'INSERT INTO admin_logs (admin_id, action, details, ip_address) VALUES ($1, $2, $3, $4)',
['ADMIN', action, JSON.stringify(details), req.ip]
);
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: 'Failed to create log' });
}
});
// ==================== PLATFORM STATS ====================
app.get('/stats/platform', async (req, res) => {
try {
const stats = await pool.query(`
SELECT
(SELECT COUNT(*) FROM users) as total_users,
(SELECT COUNT(*) FROM users WHERE is_verified = TRUE) as verified_users,
(SELECT COUNT(*) FROM users WHERE last_login > NOW() - INTERVAL '24 hours') as active_24h,
(SELECT COUNT(*) FROM games) as total_games,
(SELECT COUNT(*) FROM games WHERE status = 'COMPLETED') as completed_games,
(SELECT COALESCE(SUM(amount), 0) FROM transactions WHERE type = 'DEPOSIT' AND status = 'confirmed') as total_deposits,
(SELECT COALESCE(SUM(amount), 0) FROM transactions WHERE type = 'WITHDRAW' AND status IN ('confirmed', 'confirming')) as total_withdrawals,
(SELECT COALESCE(SUM(amount), 0) FROM transactions WHERE type = 'FEE') as total_fees,
(SELECT COALESCE(SUM(balance), 0) FROM users) as total_user_balance
`);
res.json(stats.rows[0]);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch platform stats' });
}
});
// ==================== HEALTH ====================
app.get('/health', async (req, res) => {
try {
await pool.query('SELECT 1');
await redis.ping();
const services = await Promise.allSettled([
axios.get(`${SERVICES.AUTH}/health`, { timeout: 2000 }),
axios.get(`${SERVICES.WALLET}/health`, { timeout: 2000 }),
axios.get(`${SERVICES.GAME}/health`, { timeout: 2000 }),
axios.get(`${SERVICES.MONITOR}/health`, { timeout: 2000 })
]);
res.json({
status: 'healthy',
service: 'admin',
services: {
auth: services[0].status === 'fulfilled' ? 'healthy' : 'unhealthy',
wallet: services[1].status === 'fulfilled' ? 'healthy' : 'unhealthy',
game: services[2].status === 'fulfilled' ? 'healthy' : 'unhealthy',
monitor: services[3].status === 'fulfilled' ? 'healthy' : 'unhealthy'
}
});
} catch (error) {
res.status(503).json({ status: 'unhealthy', error: error.message });
}
});
app.listen(PORT, () => {
console.log(`👑 Admin Controller running on port ${PORT}`);
});
// Global error handler
app.use((err, req, res, next) => {
console.error('Unhandled error:', err);
res.status(500).json(buildErrorResponse(5006, {
message: process.env.NODE_ENV === 'development' ? err.message : 'Internal server error'
}));
});
module.exports = app;