update
This commit is contained in:
@@ -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