// Check-in Server (separater Express-App auf Port 3334) const express = require('express'); const { db } = require('./database'); const { getCurrentDate, getCurrentTime, updateTotalHours } = require('./helpers/utils'); const checkinApp = express(); const CHECKIN_PORT = 3334; // Middleware für Check-in-Server checkinApp.use(express.json()); // API: Check-in (Kommen) checkinApp.get('/api/checkin/:userId', (req, res) => { const userId = parseInt(req.params.userId); const currentDate = getCurrentDate(); const currentTime = getCurrentTime(); // Prüfe ob User existiert db.get('SELECT id FROM users WHERE id = ?', [userId], (err, user) => { if (err || !user) { return res.status(404).json({ success: false, error: 'Benutzer nicht gefunden' }); } // Prüfe ob bereits ein Eintrag für heute existiert db.get('SELECT * FROM timesheet_entries WHERE user_id = ? AND date = ? ORDER BY updated_at DESC, id DESC LIMIT 1', [userId, currentDate], (err, entry) => { if (err) { return res.status(500).json({ success: false, error: 'Fehler beim Abrufen des Eintrags' }); } if (!entry) { // Kein Eintrag existiert → Erstelle neuen mit start_time db.run(`INSERT INTO timesheet_entries (user_id, date, start_time, updated_at) VALUES (?, ?, ?, CURRENT_TIMESTAMP)`, [userId, currentDate, currentTime], (err) => { if (err) { return res.status(500).json({ success: false, error: 'Fehler beim Erstellen des Eintrags' }); } res.json({ success: true, message: `Start-Zeit erfasst: ${currentTime}`, start_time: currentTime, date: currentDate }); }); } else if (!entry.start_time) { // Eintrag existiert, aber keine Start-Zeit → Setze start_time db.run('UPDATE timesheet_entries SET start_time = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?', [currentTime, entry.id], (err) => { if (err) { return res.status(500).json({ success: false, error: 'Fehler beim Aktualisieren' }); } res.json({ success: true, message: `Start-Zeit erfasst: ${currentTime}`, start_time: currentTime, date: currentDate }); }); } else { // Start-Zeit bereits vorhanden → Ignoriere weiteren Check-in res.json({ success: true, message: `Bereits eingecheckt um ${entry.start_time}. Check-in ignoriert.`, start_time: entry.start_time, date: currentDate }); } }); }); }); // API: Check-out (Gehen) checkinApp.get('/api/checkout/:userId', (req, res) => { const userId = parseInt(req.params.userId); const currentDate = getCurrentDate(); const currentTime = getCurrentTime(); // Prüfe ob User existiert db.get('SELECT id FROM users WHERE id = ?', [userId], (err, user) => { if (err || !user) { return res.status(404).json({ success: false, error: 'Benutzer nicht gefunden' }); } // Prüfe ob bereits ein Eintrag für heute existiert db.get('SELECT * FROM timesheet_entries WHERE user_id = ? AND date = ? ORDER BY updated_at DESC, id DESC LIMIT 1', [userId, currentDate], (err, entry) => { if (err) { return res.status(500).json({ success: false, error: 'Fehler beim Abrufen des Eintrags' }); } if (!entry || !entry.start_time) { // Kein Eintrag oder keine Start-Zeit → Fehler return res.status(400).json({ success: false, error: 'Bitte zuerst einchecken (Kommen).' }); } // Berechne total_hours basierend auf start_time, end_time und break_minutes const breakMinutes = entry.break_minutes || 0; const totalHours = updateTotalHours(entry.start_time, currentTime, breakMinutes); // Setze end_time (überschreibt vorherige End-Zeit falls vorhanden) db.run('UPDATE timesheet_entries SET end_time = ?, total_hours = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?', [currentTime, totalHours, entry.id], (err) => { if (err) { return res.status(500).json({ success: false, error: 'Fehler beim Aktualisieren' }); } res.json({ success: true, message: `End-Zeit erfasst: ${currentTime}. Gesamtstunden: ${totalHours.toFixed(2)} h`, end_time: currentTime, total_hours: totalHours, date: currentDate }); }); }); }); }); // Check-in-Server starten (auf Port 3334) checkinApp.listen(CHECKIN_PORT, () => { console.log(`Check-in Server läuft auf http://localhost:${CHECKIN_PORT}`); }); module.exports = checkinApp;