Seperater CHeck inserver auf Port 3334

This commit is contained in:
Carsten Graf
2026-01-23 11:08:53 +01:00
parent 70c106ec1a
commit 0f10f35149
2 changed files with 137 additions and 111 deletions

231
server.js
View File

@@ -954,114 +954,8 @@ app.post('/api/timesheet/save', requireAuth, (req, res) => {
});
});
// API: Check-in (Kommen)
app.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)
app.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 und Check-out Endpoints wurden auf Port 3334 ausgelagert (checkin-server.js)
// Diese Endpoints sind nicht mehr auf diesem Server verfügbar
// API: Stundenerfassung für Woche laden
app.get('/api/timesheet/week/:weekStart', requireAuth, (req, res) => {
@@ -1641,6 +1535,122 @@ function setupLDAPScheduler() {
}, 5 * 60 * 1000); // Alle 5 Minuten prüfen
}
// Check-in-Server (separater Express-App auf Port 3334)
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
});
});
});
});
});
// Server starten
app.listen(PORT, () => {
console.log(`Server läuft auf http://localhost:${PORT}`);
@@ -1651,3 +1661,8 @@ app.listen(PORT, () => {
// LDAP-Scheduler starten
setupLDAPScheduler();
});
// Check-in-Server starten (auf Port 3334)
checkinApp.listen(CHECKIN_PORT, () => {
console.log(`Check-in Server läuft auf http://localhost:${CHECKIN_PORT}`);
});

View File

@@ -115,14 +115,25 @@
}
}
// URLs mit aktueller Domain aktualisieren
// URLs mit aktueller Domain aktualisieren (Port 3334 für Check-in)
document.addEventListener('DOMContentLoaded', function() {
const userId = '<%= user.id %>';
const baseUrl = window.location.origin;
// Check-in URLs verwenden Port 3334
// Ersetze Port in URL oder füge Port hinzu falls nicht vorhanden
let checkinBaseUrl;
if (baseUrl.match(/:\d+$/)) {
// Port vorhanden - ersetze ihn
checkinBaseUrl = baseUrl.replace(/:\d+$/, ':3334');
} else {
// Kein Port - füge Port hinzu
const url = new URL(baseUrl);
checkinBaseUrl = `${url.protocol}//${url.hostname}:3334`;
}
const checkinInput = document.getElementById('checkinUrl');
const checkoutInput = document.getElementById('checkoutUrl');
if (checkinInput) checkinInput.value = `${baseUrl}/api/checkin/${userId}`;
if (checkoutInput) checkoutInput.value = `${baseUrl}/api/checkout/${userId}`;
if (checkinInput) checkinInput.value = `${checkinBaseUrl}/api/checkin/${userId}`;
if (checkoutInput) checkoutInput.value = `${checkinBaseUrl}/api/checkout/${userId}`;
// Rollenwechsel-Handler
const roleSwitcher = document.getElementById('roleSwitcher');