Init
This commit is contained in:
131
helpers/utils.js
Normal file
131
helpers/utils.js
Normal file
@@ -0,0 +1,131 @@
|
||||
// Helper-Funktionen für das Stundenerfassungs-System
|
||||
|
||||
// Helper: Prüft ob User eine bestimmte Rolle hat
|
||||
function hasRole(req, role) {
|
||||
if (!req.session.roles || !Array.isArray(req.session.roles)) {
|
||||
return false;
|
||||
}
|
||||
return req.session.roles.includes(role);
|
||||
}
|
||||
|
||||
// Helper: Bestimmt die Standard-Rolle (höchste Priorität: admin > verwaltung > mitarbeiter)
|
||||
function getDefaultRole(roles) {
|
||||
if (!Array.isArray(roles) || roles.length === 0) {
|
||||
return 'mitarbeiter';
|
||||
}
|
||||
if (roles.includes('admin')) return 'admin';
|
||||
if (roles.includes('verwaltung')) return 'verwaltung';
|
||||
return roles[0]; // Fallback auf erste Rolle
|
||||
}
|
||||
|
||||
// Helper: Gibt aktuelles Datum als YYYY-MM-DD zurück
|
||||
function getCurrentDate() {
|
||||
const now = new Date();
|
||||
const year = now.getFullYear();
|
||||
const month = String(now.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(now.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
// Helper: Gibt aktuelle Zeit als HH:MM zurück
|
||||
function getCurrentTime() {
|
||||
const now = new Date();
|
||||
const hours = String(now.getHours()).padStart(2, '0');
|
||||
const minutes = String(now.getMinutes()).padStart(2, '0');
|
||||
return `${hours}:${minutes}`;
|
||||
}
|
||||
|
||||
// Helper: Berechnet Pausenzeit in Minuten zwischen zwei Zeiten
|
||||
function calculateBreakMinutes(pauseStart, pauseEnd) {
|
||||
if (!pauseStart || !pauseEnd) return 0;
|
||||
|
||||
const [startHours, startMinutes] = pauseStart.split(':').map(Number);
|
||||
const [endHours, endMinutes] = pauseEnd.split(':').map(Number);
|
||||
|
||||
const startTotalMinutes = startHours * 60 + startMinutes;
|
||||
const endTotalMinutes = endHours * 60 + endMinutes;
|
||||
|
||||
return endTotalMinutes - startTotalMinutes;
|
||||
}
|
||||
|
||||
// Helper: Berechnet total_hours basierend auf start_time, end_time und break_minutes
|
||||
function updateTotalHours(startTime, endTime, breakMinutes) {
|
||||
if (!startTime || !endTime) return 0;
|
||||
|
||||
const [startHours, startMinutes] = startTime.split(':').map(Number);
|
||||
const [endHours, endMinutes] = endTime.split(':').map(Number);
|
||||
|
||||
const startTotalMinutes = startHours * 60 + startMinutes;
|
||||
const endTotalMinutes = endHours * 60 + endMinutes;
|
||||
|
||||
const totalMinutes = endTotalMinutes - startTotalMinutes - (breakMinutes || 0);
|
||||
return totalMinutes / 60; // Konvertiere zu Stunden
|
||||
}
|
||||
|
||||
// Helper: Formatiert Datum für Anzeige (DD.MM.YYYY)
|
||||
function formatDate(dateStr) {
|
||||
const date = new Date(dateStr);
|
||||
return date.toLocaleDateString('de-DE');
|
||||
}
|
||||
|
||||
// Helper: Formatiert Datum und Zeit für Anzeige
|
||||
function formatDateTime(dateStr) {
|
||||
const date = new Date(dateStr);
|
||||
return date.toLocaleString('de-DE');
|
||||
}
|
||||
|
||||
// Helper: Berechnet Kalenderwoche aus einem Datum (ISO 8601)
|
||||
function getCalendarWeek(dateStr) {
|
||||
const date = new Date(dateStr);
|
||||
const d = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
|
||||
const dayNum = d.getUTCDay() || 7;
|
||||
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
|
||||
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
|
||||
const weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7);
|
||||
return weekNo;
|
||||
}
|
||||
|
||||
// Helper: Berechnet week_start (Montag) und week_end (Sonntag) aus Jahr und Kalenderwoche (ISO 8601)
|
||||
function getWeekDatesFromCalendarWeek(year, weekNumber) {
|
||||
// ISO 8601: Woche beginnt am Montag, erste Woche enthält den 4. Januar
|
||||
const jan4 = new Date(Date.UTC(year, 0, 4));
|
||||
const jan4Day = jan4.getUTCDay() || 7; // 1 = Montag, 7 = Sonntag
|
||||
const daysToMonday = jan4Day === 1 ? 0 : 1 - jan4Day;
|
||||
|
||||
// Montag der ersten Woche
|
||||
const firstMonday = new Date(Date.UTC(year, 0, 4 + daysToMonday));
|
||||
|
||||
// Montag der gewünschten Woche (Woche 1 = erste Woche)
|
||||
const weekMonday = new Date(firstMonday);
|
||||
weekMonday.setUTCDate(firstMonday.getUTCDate() + (weekNumber - 1) * 7);
|
||||
|
||||
// Sonntag der Woche (6 Tage nach Montag)
|
||||
const weekSunday = new Date(weekMonday);
|
||||
weekSunday.setUTCDate(weekMonday.getUTCDate() + 6);
|
||||
|
||||
// Format: YYYY-MM-DD
|
||||
const formatDate = (date) => {
|
||||
const y = date.getUTCFullYear();
|
||||
const m = String(date.getUTCMonth() + 1).padStart(2, '0');
|
||||
const d = String(date.getUTCDate()).padStart(2, '0');
|
||||
return `${y}-${m}-${d}`;
|
||||
};
|
||||
|
||||
return {
|
||||
week_start: formatDate(weekMonday),
|
||||
week_end: formatDate(weekSunday)
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
hasRole,
|
||||
getDefaultRole,
|
||||
getCurrentDate,
|
||||
getCurrentTime,
|
||||
calculateBreakMinutes,
|
||||
updateTotalHours,
|
||||
formatDate,
|
||||
formatDateTime,
|
||||
getCalendarWeek,
|
||||
getWeekDatesFromCalendarWeek
|
||||
};
|
||||
Reference in New Issue
Block a user