update
This commit is contained in:
@@ -21,8 +21,37 @@ const broadcastSchema = z.object({
|
||||
nachricht: z.string().min(1).max(2000),
|
||||
schwere: z.enum(['info', 'warnung', 'fehler']).default('info'),
|
||||
targetGroup: z.string().optional(),
|
||||
targetDienstgrad: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
const broadcastFilterSchema = z.object({
|
||||
targetGroup: z.string().optional(),
|
||||
targetDienstgrad: z.array(z.string()).optional(),
|
||||
});
|
||||
|
||||
function buildFilteredUserQuery(filters: { targetGroup?: string; targetDienstgrad?: string[] }): { text: string; values: unknown[] } {
|
||||
const conditions: string[] = ['u.is_active = TRUE'];
|
||||
const values: unknown[] = [];
|
||||
let paramIndex = 1;
|
||||
|
||||
if (filters.targetGroup) {
|
||||
conditions.push(`$${paramIndex} = ANY(u.authentik_groups)`);
|
||||
values.push(filters.targetGroup);
|
||||
paramIndex++;
|
||||
}
|
||||
|
||||
if (filters.targetDienstgrad && filters.targetDienstgrad.length > 0) {
|
||||
conditions.push(`mp.dienstgrad = ANY($${paramIndex})`);
|
||||
values.push(filters.targetDienstgrad);
|
||||
paramIndex++;
|
||||
}
|
||||
|
||||
const needsJoin = filters.targetDienstgrad && filters.targetDienstgrad.length > 0;
|
||||
const text = `SELECT DISTINCT u.id FROM users u${needsJoin ? ' LEFT JOIN member_profiles mp ON mp.user_id = u.id' : ''} WHERE ${conditions.join(' AND ')}`;
|
||||
|
||||
return { text, values };
|
||||
}
|
||||
|
||||
class ServiceMonitorController {
|
||||
async getAll(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
@@ -160,21 +189,11 @@ class ServiceMonitorController {
|
||||
|
||||
async broadcastNotification(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { titel, nachricht, schwere, targetGroup } = broadcastSchema.parse(req.body);
|
||||
const { titel, nachricht, schwere, targetGroup, targetDienstgrad } = 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;
|
||||
}
|
||||
const query = buildFilteredUserQuery({ targetGroup, targetDienstgrad });
|
||||
const result = await pool.query(query.text, query.values);
|
||||
const users = result.rows;
|
||||
|
||||
let sent = 0;
|
||||
for (const user of users) {
|
||||
@@ -198,6 +217,24 @@ class ServiceMonitorController {
|
||||
res.status(500).json({ success: false, message: 'Failed to broadcast notification' });
|
||||
}
|
||||
}
|
||||
|
||||
async broadcastPreview(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { targetGroup, targetDienstgrad } = broadcastFilterSchema.parse(req.body);
|
||||
|
||||
const query = buildFilteredUserQuery({ targetGroup, targetDienstgrad });
|
||||
const result = await pool.query(query.text, query.values);
|
||||
|
||||
res.json({ success: true, data: { count: result.rows.length } });
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
|
||||
return;
|
||||
}
|
||||
logger.error('Failed to preview broadcast', { error });
|
||||
res.status(500).json({ success: false, message: 'Failed to preview broadcast' });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new ServiceMonitorController();
|
||||
|
||||
Reference in New Issue
Block a user