adding chat features, admin features and bug fixes
This commit is contained in:
192
backend/src/controllers/serviceMonitor.controller.ts
Normal file
192
backend/src/controllers/serviceMonitor.controller.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { z } from 'zod';
|
||||
import serviceMonitorService from '../services/serviceMonitor.service';
|
||||
import notificationService from '../services/notification.service';
|
||||
import pool from '../config/database';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
const createServiceSchema = z.object({
|
||||
name: z.string().min(1).max(200),
|
||||
url: z.string().url().max(500),
|
||||
});
|
||||
|
||||
const updateServiceSchema = z.object({
|
||||
name: z.string().min(1).max(200).optional(),
|
||||
url: z.string().url().max(500).optional(),
|
||||
is_active: z.boolean().optional(),
|
||||
});
|
||||
|
||||
const broadcastSchema = z.object({
|
||||
titel: z.string().min(1).max(200),
|
||||
nachricht: z.string().min(1).max(2000),
|
||||
schwere: z.enum(['info', 'warnung', 'fehler']).default('info'),
|
||||
targetGroup: z.string().optional(),
|
||||
});
|
||||
|
||||
class ServiceMonitorController {
|
||||
async getAll(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const services = await serviceMonitorService.getAllServices();
|
||||
res.json({ success: true, data: services });
|
||||
} catch (error) {
|
||||
logger.error('Failed to get services', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to get services' });
|
||||
}
|
||||
}
|
||||
|
||||
async create(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { name, url } = createServiceSchema.parse(req.body);
|
||||
const service = await serviceMonitorService.createService(name, url);
|
||||
res.status(201).json({ success: true, data: service });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
|
||||
return;
|
||||
}
|
||||
logger.error('Failed to create service', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to create service' });
|
||||
}
|
||||
}
|
||||
|
||||
async update(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const data = updateServiceSchema.parse(req.body);
|
||||
const service = await serviceMonitorService.updateService(req.params.id as string, data);
|
||||
res.json({ success: true, data: service });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
|
||||
return;
|
||||
}
|
||||
logger.error('Failed to update service', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to update service' });
|
||||
}
|
||||
}
|
||||
|
||||
async delete(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const deleted = await serviceMonitorService.deleteService(req.params.id as string);
|
||||
if (!deleted) {
|
||||
res.status(404).json({ success: false, message: 'Service not found or is internal' });
|
||||
return;
|
||||
}
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
logger.error('Failed to delete service', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to delete service' });
|
||||
}
|
||||
}
|
||||
|
||||
async pingAll(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const results = await serviceMonitorService.pingAll();
|
||||
res.json({ success: true, data: results });
|
||||
} catch (error) {
|
||||
logger.error('Failed to ping services', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to ping services' });
|
||||
}
|
||||
}
|
||||
|
||||
async getStatusSummary(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const summary = await serviceMonitorService.getStatusSummary();
|
||||
res.json({ success: true, data: summary });
|
||||
} catch (error) {
|
||||
logger.error('Failed to get status summary', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to get status summary' });
|
||||
}
|
||||
}
|
||||
|
||||
async getSystemHealth(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
let dbStatus = false;
|
||||
let dbSize = '0';
|
||||
|
||||
try {
|
||||
await pool.query('SELECT 1');
|
||||
dbStatus = true;
|
||||
const sizeResult = await pool.query('SELECT pg_database_size(current_database()) as size');
|
||||
dbSize = sizeResult.rows[0].size;
|
||||
} catch {
|
||||
// DB is down
|
||||
}
|
||||
|
||||
const mem = process.memoryUsage();
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
nodeVersion: process.version,
|
||||
uptime: process.uptime(),
|
||||
memoryUsage: {
|
||||
heapUsed: mem.heapUsed,
|
||||
heapTotal: mem.heapTotal,
|
||||
rss: mem.rss,
|
||||
external: mem.external,
|
||||
},
|
||||
dbStatus,
|
||||
dbSize,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Failed to get system health', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to get system health' });
|
||||
}
|
||||
}
|
||||
|
||||
async getUsers(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const result = await pool.query(
|
||||
`SELECT id, email, name, role, authentik_groups as groups, is_active, last_login_at
|
||||
FROM users ORDER BY name`
|
||||
);
|
||||
res.json({ success: true, data: result.rows });
|
||||
} catch (error) {
|
||||
logger.error('Failed to get users', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to get users' });
|
||||
}
|
||||
}
|
||||
|
||||
async broadcastNotification(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { titel, nachricht, schwere, targetGroup } = broadcastSchema.parse(req.body);
|
||||
|
||||
let users;
|
||||
if (targetGroup) {
|
||||
const result = await pool.query(
|
||||
`SELECT id FROM users WHERE is_active = TRUE AND $1 = ANY(authentik_groups)`,
|
||||
[targetGroup]
|
||||
);
|
||||
users = result.rows;
|
||||
} else {
|
||||
const result = await pool.query(
|
||||
`SELECT id FROM users WHERE is_active = TRUE`
|
||||
);
|
||||
users = result.rows;
|
||||
}
|
||||
|
||||
let sent = 0;
|
||||
for (const user of users) {
|
||||
await notificationService.createNotification({
|
||||
user_id: user.id,
|
||||
typ: 'broadcast',
|
||||
titel,
|
||||
nachricht,
|
||||
schwere,
|
||||
});
|
||||
sent++;
|
||||
}
|
||||
|
||||
res.json({ success: true, data: { sent } });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
|
||||
return;
|
||||
}
|
||||
logger.error('Failed to broadcast notification', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to broadcast notification' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new ServiceMonitorController();
|
||||
Reference in New Issue
Block a user