-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathdataExchangeRoutes.ts
More file actions
52 lines (46 loc) · 1.89 KB
/
Copy pathdataExchangeRoutes.ts
File metadata and controls
52 lines (46 loc) · 1.89 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
import { Router, Request, Response } from 'express';
import { requirePermission, requireAdmin } from '../auth/authMiddleware.js';
import databaseService from '../../services/database.js';
import { ALL_SOURCES } from '../../db/repositories/index.js';
import { logger } from '../../utils/logger.js';
import { ok, fail } from '../utils/apiResponse.js';
const router: Router = Router();
router.get('/stats', requirePermission('dashboard', 'read'), async (req: Request, res: Response) => {
try {
const statsSourceId = req.query.sourceId as string | undefined;
// intentional cross-source: stats totals span all sources when no sourceId is specified
const messageCount = await databaseService.messages.getMessageCount(statsSourceId ?? ALL_SOURCES);
const nodeCount = await databaseService.nodes.getNodeCount(statsSourceId ?? ALL_SOURCES);
const channelCount = await databaseService.channels.getChannelCount(statsSourceId ?? ALL_SOURCES);
const messagesByDay = await databaseService.getMessagesByDayAsync(7, statsSourceId);
res.json({
messageCount,
nodeCount,
channelCount,
messagesByDay,
});
} catch (error) {
logger.error('Error fetching stats:', error);
fail(res, 500, 'STATS_FAILED', 'Failed to fetch stats');
}
});
router.post('/export', requireAdmin(), async (_req: Request, res: Response) => {
try {
const data = await databaseService.exportDataAsync();
res.json(data);
} catch (error) {
logger.error('Error exporting data:', error);
fail(res, 500, 'EXPORT_FAILED', 'Failed to export data');
}
});
router.post('/import', requireAdmin(), async (req: Request, res: Response) => {
try {
const data = req.body;
await databaseService.importDataAsync(data);
ok(res);
} catch (error) {
logger.error('Error importing data:', error);
fail(res, 500, 'IMPORT_FAILED', 'Failed to import data');
}
});
export default router;