Checkin Seite erstellt
This commit is contained in:
@@ -1,15 +1,44 @@
|
||||
// Check-in Server (separater Express-App auf Port 3334)
|
||||
|
||||
const express = require('express');
|
||||
const path = require('path');
|
||||
const { db } = require('./database');
|
||||
const { getCurrentDate, getCurrentTime, updateTotalHours } = require('./helpers/utils');
|
||||
|
||||
const checkinApp = express();
|
||||
const CHECKIN_PORT = 3336;
|
||||
const CHECKIN_PORT = 3334;
|
||||
|
||||
// View-Engine für Browser-Aufrufe (Bestätigungsseiten)
|
||||
checkinApp.set('view engine', 'ejs');
|
||||
checkinApp.set('views', path.join(__dirname, 'views'));
|
||||
|
||||
// Middleware für Check-in-Server
|
||||
checkinApp.use(express.json());
|
||||
|
||||
/** Erkennt Browser-Aufruf: Accept-Header enthält text/html (z. B. beim Aufruf per QR/Link im Browser). */
|
||||
function wantsHtml(req) {
|
||||
const accept = req.get('Accept') || '';
|
||||
return /text\/html/i.test(accept);
|
||||
}
|
||||
|
||||
/** Sendet je nach Client entweder HTML-Seite oder JSON. */
|
||||
function sendResponse(req, res, success, data) {
|
||||
if (wantsHtml(req)) {
|
||||
const title = data.title || (success ? 'Erfolg' : 'Fehler');
|
||||
const message = data.message || data.error || (success ? 'Aktion erfolgreich.' : 'Ein Fehler ist aufgetreten.');
|
||||
if (!success && data.status) {
|
||||
res.status(data.status);
|
||||
}
|
||||
res.render('checkin-result', { success, title, message });
|
||||
} else {
|
||||
if (success) {
|
||||
res.json({ success: true, ...data });
|
||||
} else {
|
||||
res.status(data.status || 500).json({ success: false, error: data.error });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// API: Check-in (Kommen)
|
||||
checkinApp.get('/api/checkin/:userId', (req, res) => {
|
||||
const userId = parseInt(req.params.userId);
|
||||
@@ -19,25 +48,27 @@ checkinApp.get('/api/checkin/:userId', (req, res) => {
|
||||
// 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' });
|
||||
return sendResponse(req, res, false, { error: 'Benutzer nicht gefunden', status: 404 });
|
||||
}
|
||||
|
||||
// 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' });
|
||||
return sendResponse(req, res, false, { error: 'Fehler beim Abrufen des Eintrags', status: 500 });
|
||||
}
|
||||
|
||||
const successTitle = 'Hallo, du wurdest erfolgreich eingecheckt';
|
||||
|
||||
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' });
|
||||
return sendResponse(req, res, false, { error: 'Fehler beim Erstellen des Eintrags', status: 500 });
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
sendResponse(req, res, true, {
|
||||
title: successTitle,
|
||||
message: `Start-Zeit erfasst: ${currentTime}`,
|
||||
start_time: currentTime,
|
||||
date: currentDate
|
||||
@@ -48,10 +79,10 @@ checkinApp.get('/api/checkin/:userId', (req, res) => {
|
||||
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' });
|
||||
return sendResponse(req, res, false, { error: 'Fehler beim Aktualisieren', status: 500 });
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
sendResponse(req, res, true, {
|
||||
title: successTitle,
|
||||
message: `Start-Zeit erfasst: ${currentTime}`,
|
||||
start_time: currentTime,
|
||||
date: currentDate
|
||||
@@ -59,8 +90,8 @@ checkinApp.get('/api/checkin/:userId', (req, res) => {
|
||||
});
|
||||
} else {
|
||||
// Start-Zeit bereits vorhanden → Ignoriere weiteren Check-in
|
||||
res.json({
|
||||
success: true,
|
||||
sendResponse(req, res, true, {
|
||||
title: successTitle,
|
||||
message: `Bereits eingecheckt um ${entry.start_time}. Check-in ignoriert.`,
|
||||
start_time: entry.start_time,
|
||||
date: currentDate
|
||||
@@ -79,21 +110,20 @@ checkinApp.get('/api/checkout/:userId', (req, res) => {
|
||||
// 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' });
|
||||
return sendResponse(req, res, false, { error: 'Benutzer nicht gefunden', status: 404 });
|
||||
}
|
||||
|
||||
// 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' });
|
||||
return sendResponse(req, res, false, { error: 'Fehler beim Abrufen des Eintrags', status: 500 });
|
||||
}
|
||||
|
||||
if (!entry || !entry.start_time) {
|
||||
// Kein Eintrag oder keine Start-Zeit → Fehler
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'Bitte zuerst einchecken (Kommen).'
|
||||
return sendResponse(req, res, false, {
|
||||
error: 'Bitte zuerst einchecken (Kommen).',
|
||||
status: 400
|
||||
});
|
||||
}
|
||||
|
||||
@@ -105,11 +135,13 @@ checkinApp.get('/api/checkout/:userId', (req, res) => {
|
||||
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' });
|
||||
return sendResponse(req, res, false, { error: 'Fehler beim Aktualisieren', status: 500 });
|
||||
}
|
||||
res.json({
|
||||
success: true,
|
||||
message: `End-Zeit erfasst: ${currentTime}. Gesamtstunden: ${totalHours.toFixed(2)} h`,
|
||||
const successTitle = 'Schönen Feierabend, du wurdest erfolgreich ausgecheckt';
|
||||
const successMessage = `End-Zeit erfasst: ${currentTime}. Gesamtstunden: ${totalHours.toFixed(2)} h`;
|
||||
sendResponse(req, res, true, {
|
||||
title: successTitle,
|
||||
message: successMessage,
|
||||
end_time: currentTime,
|
||||
total_hours: totalHours,
|
||||
date: currentDate
|
||||
|
||||
Reference in New Issue
Block a user