update
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Request, Response } from 'express';
|
||||
import atemschutzService from '../services/atemschutz.service';
|
||||
import notificationService from '../services/notification.service';
|
||||
import { CreateAtemschutzSchema, UpdateAtemschutzSchema } from '../models/atemschutz.model';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
@@ -159,6 +160,49 @@ class AtemschutzController {
|
||||
}
|
||||
}
|
||||
|
||||
async getExpiring(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const expiring = await atemschutzService.getExpiringCertifications(30);
|
||||
|
||||
// Side-effect: create notifications for expiring certifications (dedup via DB constraint)
|
||||
for (const item of expiring) {
|
||||
if (item.untersuchung_status !== 'ok') {
|
||||
await notificationService.createNotification({
|
||||
user_id: item.user_id,
|
||||
typ: 'atemschutz_expiry',
|
||||
titel: item.untersuchung_status === 'abgelaufen'
|
||||
? 'G26 Untersuchung abgelaufen'
|
||||
: 'G26 Untersuchung läuft bald ab',
|
||||
nachricht: `Ihre G26 Untersuchung ${item.untersuchung_status === 'abgelaufen' ? 'ist abgelaufen' : 'läuft bald ab'}.`,
|
||||
schwere: item.untersuchung_status === 'abgelaufen' ? 'fehler' : 'warnung',
|
||||
quell_typ: 'atemschutz_untersuchung',
|
||||
quell_id: item.id,
|
||||
link: '/atemschutz',
|
||||
});
|
||||
}
|
||||
if (item.leistungstest_status !== 'ok') {
|
||||
await notificationService.createNotification({
|
||||
user_id: item.user_id,
|
||||
typ: 'atemschutz_expiry',
|
||||
titel: item.leistungstest_status === 'abgelaufen'
|
||||
? 'Leistungstest abgelaufen'
|
||||
: 'Leistungstest läuft bald ab',
|
||||
nachricht: `Ihr Leistungstest ${item.leistungstest_status === 'abgelaufen' ? 'ist abgelaufen' : 'läuft bald ab'}.`,
|
||||
schwere: item.leistungstest_status === 'abgelaufen' ? 'fehler' : 'warnung',
|
||||
quell_typ: 'atemschutz_leistungstest',
|
||||
quell_id: item.id,
|
||||
link: '/atemschutz',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
res.status(200).json({ success: true, data: expiring });
|
||||
} catch (error) {
|
||||
logger.error('Atemschutz getExpiring error', { error });
|
||||
res.status(500).json({ success: false, message: 'Ablaufende Zertifizierungen konnten nicht geladen werden' });
|
||||
}
|
||||
}
|
||||
|
||||
async delete(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { id } = req.params as Record<string, string>;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { ZodError } from 'zod';
|
||||
import bookingService from '../services/booking.service';
|
||||
import vehicleService from '../services/vehicle.service';
|
||||
import {
|
||||
CreateBuchungSchema,
|
||||
UpdateBuchungSchema,
|
||||
@@ -52,12 +53,13 @@ class BookingController {
|
||||
res.status(400).json({ success: false, message: 'from und to sind erforderlich' });
|
||||
return;
|
||||
}
|
||||
const bookings = await bookingService.getBookingsByRange(
|
||||
new Date(from as string),
|
||||
new Date(to as string),
|
||||
fahrzeugId as string | undefined
|
||||
);
|
||||
res.json({ success: true, data: bookings });
|
||||
const fromDate = new Date(from as string);
|
||||
const toDate = new Date(to as string);
|
||||
const [bookings, maintenanceWindows] = await Promise.all([
|
||||
bookingService.getBookingsByRange(fromDate, toDate, fahrzeugId as string | undefined),
|
||||
vehicleService.getMaintenanceWindows(fromDate, toDate),
|
||||
]);
|
||||
res.json({ success: true, data: { bookings, maintenanceWindows } });
|
||||
} catch (error) {
|
||||
logger.error('Booking getCalendarRange error', { error });
|
||||
res.status(500).json({ success: false, message: 'Buchungen konnten nicht geladen werden' });
|
||||
|
||||
@@ -118,6 +118,39 @@ class EventsController {
|
||||
}
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// GET /api/events/conflicts?from=<ISO>&to=<ISO>&excludeId=<uuid>
|
||||
// -------------------------------------------------------------------------
|
||||
checkConflicts = async (req: Request, res: Response): Promise<void> => {
|
||||
try {
|
||||
const fromStr = req.query.from as string | undefined;
|
||||
const toStr = req.query.to as string | undefined;
|
||||
const excludeId = req.query.excludeId as string | undefined;
|
||||
|
||||
if (!fromStr || !toStr) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
message: 'Query-Parameter "from" und "to" sind erforderlich (ISO-8601)',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const from = new Date(fromStr);
|
||||
const to = new Date(toStr);
|
||||
|
||||
if (isNaN(from.getTime()) || isNaN(to.getTime())) {
|
||||
res.status(400).json({ success: false, message: 'Ungültiges Datumsformat' });
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await eventsService.checkConflicts(from, to, excludeId);
|
||||
res.json({ success: true, data });
|
||||
} catch (error) {
|
||||
logger.error('checkConflicts error', { error });
|
||||
res.status(500).json({ success: false, message: 'Fehler bei der Konfliktprüfung' });
|
||||
}
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// GET /api/events/calendar?from=<ISO>&to=<ISO>
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { z } from 'zod';
|
||||
import vehicleService from '../services/vehicle.service';
|
||||
import equipmentService from '../services/equipment.service';
|
||||
import { FahrzeugStatus } from '../models/vehicle.model';
|
||||
import logger from '../utils/logger';
|
||||
|
||||
@@ -140,6 +141,69 @@ class VehicleController {
|
||||
}
|
||||
}
|
||||
|
||||
async exportAlerts(_req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const escape = (v: unknown): string => {
|
||||
if (v === null || v === undefined) return '';
|
||||
const str = String(v);
|
||||
let safe = str.replace(/"/g, '""');
|
||||
if (/^[=+@\-]/.test(safe)) safe = "'" + safe;
|
||||
return `"${safe}"`;
|
||||
};
|
||||
|
||||
const formatDate = (d: Date | string | null): string => {
|
||||
if (!d) return '';
|
||||
const date = typeof d === 'string' ? new Date(d) : d;
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const year = date.getFullYear();
|
||||
return `${day}.${month}.${year}`;
|
||||
};
|
||||
|
||||
const [vehicleAlerts, equipmentAlerts] = await Promise.all([
|
||||
vehicleService.getUpcomingInspections(365),
|
||||
equipmentService.getUpcomingInspections(365),
|
||||
]);
|
||||
|
||||
const header = 'Typ,Bezeichnung,Kurzname,Prüfungsart,Fällig am,Tage verbleibend';
|
||||
const rows: string[] = [];
|
||||
|
||||
for (const a of vehicleAlerts) {
|
||||
const pruefungsart = a.type === '57a' ? '§57a Überprüfung' : 'Nächste Wartung';
|
||||
rows.push([
|
||||
escape('Fahrzeug'),
|
||||
escape(a.bezeichnung),
|
||||
escape(a.kurzname),
|
||||
escape(pruefungsart),
|
||||
escape(formatDate(a.faelligAm)),
|
||||
escape(a.tage),
|
||||
].join(','));
|
||||
}
|
||||
|
||||
for (const e of equipmentAlerts) {
|
||||
rows.push([
|
||||
escape('Ausrüstung'),
|
||||
escape(e.bezeichnung),
|
||||
escape(''),
|
||||
escape('Prüfung'),
|
||||
escape(formatDate(e.naechste_pruefung_am)),
|
||||
escape(e.pruefung_tage_bis_faelligkeit),
|
||||
].join(','));
|
||||
}
|
||||
|
||||
const today = new Date();
|
||||
const dateStr = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`;
|
||||
const csv = '\uFEFF' + header + '\n' + rows.join('\n');
|
||||
|
||||
res.setHeader('Content-Type', 'text/csv; charset=utf-8');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="pruefungen_${dateStr}.csv"`);
|
||||
res.status(200).send(csv);
|
||||
} catch (error) {
|
||||
logger.error('exportAlerts error', { error });
|
||||
res.status(500).json({ success: false, message: 'Prüfungsexport konnte nicht erstellt werden' });
|
||||
}
|
||||
}
|
||||
|
||||
async getVehicle(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { id } = req.params as Record<string, string>;
|
||||
|
||||
Reference in New Issue
Block a user