This commit is contained in:
2026-03-10 14:39:11 +01:00
parent bf578a8d87
commit d4a544164b
7 changed files with 217 additions and 51 deletions

View File

@@ -5,7 +5,7 @@ const { db } = require('../database');
const { getCurrentDate, getCurrentTime, updateTotalHours } = require('../helpers/utils');
// Ping-Funktion für einen User
async function pingUserIP(userId, ip, currentDate, currentTime) {
async function pingUserIP(userId, ip, defaultBreakMinutes, currentDate, currentTime) {
try {
const result = await ping.promise.probe(ip, {
timeout: 3,
@@ -31,6 +31,10 @@ async function pingUserIP(userId, ip, currentDate, currentTime) {
return;
}
const userDefaultBreakMinutes = Number.isInteger(defaultBreakMinutes) && defaultBreakMinutes >= 0
? defaultBreakMinutes
: 30;
if (isReachable) {
// IP ist erreichbar
if (!pingStatus) {
@@ -67,9 +71,9 @@ async function pingUserIP(userId, ip, currentDate, currentTime) {
});
} else 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) => {
db.run(`INSERT INTO timesheet_entries (user_id, date, start_time, break_minutes, updated_at)
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)`,
[userId, currentDate, currentTime, userDefaultBreakMinutes], (err) => {
if (err) {
console.error(`Fehler beim Erstellen des Eintrags für User ${userId}:`, err);
} else {
@@ -161,7 +165,7 @@ function setupPingService() {
const currentTime = getCurrentTime();
// Hole alle User mit IP-Adresse
db.all('SELECT id, ping_ip FROM users WHERE ping_ip IS NOT NULL AND ping_ip != ""', (err, users) => {
db.all('SELECT id, ping_ip, default_break_minutes FROM users WHERE ping_ip IS NOT NULL AND ping_ip != ""', (err, users) => {
if (err) {
console.error('Fehler beim Abrufen der User mit IP-Adressen:', err);
return;
@@ -173,7 +177,7 @@ function setupPingService() {
// Ping alle User parallel
users.forEach(user => {
pingUserIP(user.id, user.ping_ip, currentDate, currentTime);
pingUserIP(user.id, user.ping_ip, user.default_break_minutes, currentDate, currentTime);
});
});
}, 60000); // Jede Minute