Overtime Corrections mit Historie und Grund

This commit is contained in:
2026-02-12 11:24:45 +01:00
parent e020aa4e46
commit 3edc0fe60c
6 changed files with 648 additions and 225 deletions

View File

@@ -132,6 +132,8 @@ function registerVerwaltungRoutes(app) {
app.put('/api/verwaltung/user/:id/overtime-offset', requireVerwaltung, (req, res) => {
const userId = req.params.id;
const raw = req.body ? req.body.overtime_offset_hours : undefined;
const reasonRaw = req.body ? req.body.reason : undefined;
const reason = (reasonRaw === null || reasonRaw === undefined) ? '' : String(reasonRaw).trim();
// Leere Eingabe => 0
const normalized = (raw === '' || raw === null || raw === undefined) ? 0 : parseFloat(raw);
@@ -139,12 +141,59 @@ function registerVerwaltungRoutes(app) {
return res.status(400).json({ error: 'Ungültiger Überstunden-Offset' });
}
db.run('UPDATE users SET overtime_offset_hours = ? WHERE id = ?', [normalized, userId], (err) => {
if (err) {
console.error('Fehler beim Speichern des Überstunden-Offsets:', err);
return res.status(500).json({ error: 'Fehler beim Speichern des Überstunden-Offsets' });
}
res.json({ success: true, overtime_offset_hours: normalized });
// Neue Logik: Korrektur protokollieren + kumulativ addieren
// Feld in der Verwaltung soll nach dem Speichern immer auf 0 zurückgesetzt werden.
if (normalized === 0) {
return res.json({ success: true, overtime_offset_hours: 0 });
}
if (!reason) {
return res.status(400).json({ error: 'Bitte geben Sie einen Grund für die Korrektur an.' });
}
db.serialize(() => {
db.run('BEGIN TRANSACTION');
db.run(
`INSERT INTO overtime_corrections (user_id, correction_hours, reason, corrected_at)
VALUES (?, ?, ?, datetime('now'))`,
[userId, normalized, reason],
(err) => {
if (err) {
console.error('Fehler beim Speichern der Überstunden-Korrektur:', err);
db.run('ROLLBACK', () => {
return res.status(500).json({ error: 'Fehler beim Speichern der Überstunden-Korrektur' });
});
return;
}
db.run(
'UPDATE users SET overtime_offset_hours = COALESCE(overtime_offset_hours, 0) + ? WHERE id = ?',
[normalized, userId],
(err) => {
if (err) {
console.error('Fehler beim Aktualisieren des Überstunden-Offsets:', err);
db.run('ROLLBACK', () => {
return res.status(500).json({ error: 'Fehler beim Speichern des Überstunden-Offsets' });
});
return;
}
db.run('COMMIT', (err) => {
if (err) {
console.error('Fehler beim Commit der Überstunden-Korrektur:', err);
db.run('ROLLBACK', () => {
return res.status(500).json({ error: 'Fehler beim Speichern der Überstunden-Korrektur' });
});
return;
}
res.json({ success: true, overtime_offset_hours: 0 });
});
}
);
}
);
});
});
@@ -168,6 +217,26 @@ function registerVerwaltungRoutes(app) {
});
});
// API: Überstunden-Korrektur-Historie für einen User abrufen
app.get('/api/verwaltung/user/:id/overtime-corrections', requireVerwaltung, (req, res) => {
const userId = req.params.id;
db.all(
`SELECT correction_hours, corrected_at, reason
FROM overtime_corrections
WHERE user_id = ?
ORDER BY corrected_at DESC`,
[userId],
(err, rows) => {
// Falls Tabelle noch nicht existiert (z. B. alte DB), nicht hart fehlschlagen
if (err) {
return res.json({ corrections: [] });
}
res.json({ corrections: rows || [] });
}
);
});
// API: Krankheitstage für einen User im aktuellen Jahr abrufen
app.get('/api/verwaltung/user/:id/sick-days', requireVerwaltung, (req, res) => {
const userId = req.params.id;