From eb49c58b2d49005fe8a0cc01d5ac95aef11f63d4 Mon Sep 17 00:00:00 2001 From: donpat1to Date: Sat, 11 Oct 2025 01:26:06 +0200 Subject: [PATCH] shiftplans shown correctly --- backend/dockerfile | 2 +- backend/src/controllers/employeeController.ts | 4 + .../src/controllers/shiftPlanController.ts | 81 +++++++++++-------- backend/src/middleware/auth.ts | 4 +- backend/src/scripts/setupDefaultTemplate.ts | 15 +++- backend/src/server.ts | 8 +- 6 files changed, 73 insertions(+), 41 deletions(-) diff --git a/backend/dockerfile b/backend/dockerfile index 173efe2..e644bb6 100644 --- a/backend/dockerfile +++ b/backend/dockerfile @@ -4,5 +4,5 @@ WORKDIR /app COPY package*.json ./ RUN npm ci --only=production COPY . . -EXPOSE 3001 +EXPOSE 3002 CMD ["node", "dist/server.js"] \ No newline at end of file diff --git a/backend/src/controllers/employeeController.ts b/backend/src/controllers/employeeController.ts index 55a7305..07e5751 100644 --- a/backend/src/controllers/employeeController.ts +++ b/backend/src/controllers/employeeController.ts @@ -144,6 +144,8 @@ export const updateEmployee = async (req: AuthRequest, res: Response): Promise(` SELECT diff --git a/backend/src/controllers/shiftPlanController.ts b/backend/src/controllers/shiftPlanController.ts index c825f97..1a484c4 100644 --- a/backend/src/controllers/shiftPlanController.ts +++ b/backend/src/controllers/shiftPlanController.ts @@ -298,44 +298,59 @@ export const deleteShiftPlan = async (req: AuthRequest, res: Response): Promise< // Helper function to generate shifts from template async function generateShiftsFromTemplate(shiftPlanId: string, templateId: string, startDate: string, endDate: string): Promise { - // Get template shifts - const templateShifts = await db.all(` - SELECT * FROM template_shifts - WHERE template_id = ? - ORDER BY day_of_week, start_time - `, [templateId]); + try { + console.log(`πŸ”„ Generiere Schichten von Vorlage ${templateId} fΓΌr Plan ${shiftPlanId}`); + + // Get template shifts with time slot information + const templateShifts = await db.all(` + SELECT ts.*, tts.name as time_slot_name, tts.start_time, tts.end_time + FROM template_shifts ts + LEFT JOIN template_time_slots tts ON ts.time_slot_id = tts.id + WHERE ts.template_id = ? + ORDER BY ts.day_of_week, tts.start_time + `, [templateId]); - const start = new Date(startDate); - const end = new Date(endDate); + console.log(`πŸ“‹ Gefundene Template-Schichten: ${templateShifts.length}`); - // Generate shifts ONLY for days that have template shifts defined - for (let date = new Date(start); date <= end; date.setDate(date.getDate() + 1)) { - // Convert JS day (0=Sunday) to our format (1=Monday, 7=Sunday) - const dayOfWeek = date.getDay() === 0 ? 7 : date.getDay(); + const start = new Date(startDate); + const end = new Date(endDate); - // Find template shifts for this day of week - const shiftsForDay = templateShifts.filter(shift => shift.day_of_week === dayOfWeek); + // Generate shifts ONLY for days that have template shifts defined + for (let date = new Date(start); date <= end; date.setDate(date.getDate() + 1)) { + // Convert JS day (0=Sunday) to our format (1=Monday, 7=Sunday) + const dayOfWeek = date.getDay() === 0 ? 7 : date.getDay(); - // Only create shifts if there are template shifts defined for this weekday - if (shiftsForDay.length > 0) { - for (const templateShift of shiftsForDay) { - const shiftId = uuidv4(); - - await db.run( - `INSERT INTO assigned_shifts (id, shift_plan_id, date, name, start_time, end_time, required_employees, assigned_employees) - VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, - [ - shiftId, - shiftPlanId, - date.toISOString().split('T')[0], - templateShift.name, - templateShift.start_time, - templateShift.end_time, - templateShift.required_employees, - JSON.stringify([]) - ] - ); + // Find template shifts for this day of week + const shiftsForDay = templateShifts.filter(shift => shift.day_of_week === dayOfWeek); + + // Only create shifts if there are template shifts defined for this weekday + if (shiftsForDay.length > 0) { + for (const templateShift of shiftsForDay) { + const shiftId = uuidv4(); + + await db.run( + `INSERT INTO assigned_shifts (id, shift_plan_id, date, name, start_time, end_time, required_employees, assigned_employees) + VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, + [ + shiftId, + shiftPlanId, + date.toISOString().split('T')[0], // YYYY-MM-DD format + templateShift.time_slot_name || 'Schicht', + templateShift.start_time, + templateShift.end_time, + templateShift.required_employees, + JSON.stringify([]) + ] + ); + } + console.log(`βœ… ${shiftsForDay.length} Schichten erstellt fΓΌr ${date.toISOString().split('T')[0]}`); } } + + console.log(`πŸŽ‰ Schicht-Generierung abgeschlossen fΓΌr Plan ${shiftPlanId}`); + + } catch (error) { + console.error('❌ Fehler beim Generieren der Schichten:', error); + throw error; } } \ No newline at end of file diff --git a/backend/src/middleware/auth.ts b/backend/src/middleware/auth.ts index a573513..9b81a89 100644 --- a/backend/src/middleware/auth.ts +++ b/backend/src/middleware/auth.ts @@ -14,7 +14,7 @@ export interface AuthRequest extends Request { export const authMiddleware = (req: AuthRequest, res: Response, next: NextFunction): void => { const authHeader = req.header('Authorization'); - console.log('πŸ” Auth middleware - Authorization header:', authHeader); + //console.log('πŸ” Auth middleware - Authorization header:', authHeader); const token = authHeader?.replace('Bearer ', ''); @@ -26,7 +26,7 @@ export const authMiddleware = (req: AuthRequest, res: Response, next: NextFuncti try { const decoded = jwt.verify(token, JWT_SECRET) as any; - console.log('βœ… Token valid for user:', decoded.email, 'ID:', decoded.id); + //console.log('βœ… Token valid for user:', decoded.email, 'ID:', decoded.id); // KORREKTUR: Verwende 'id' aus dem JWT Payload req.user = { diff --git a/backend/src/scripts/setupDefaultTemplate.ts b/backend/src/scripts/setupDefaultTemplate.ts index 24c15e0..9551ee2 100644 --- a/backend/src/scripts/setupDefaultTemplate.ts +++ b/backend/src/scripts/setupDefaultTemplate.ts @@ -1,7 +1,7 @@ // backend/src/scripts/setupDefaultTemplate.ts import { v4 as uuidv4 } from 'uuid'; import { db } from '../services/databaseService.js'; -import { DEFAULT_TIME_SLOTS } from '../models/ShiftTemplate.js'; +import { DEFAULT_TIME_SLOTS, TemplateShift } from '../models/ShiftTemplate.js'; interface AdminUser { id: string; @@ -95,6 +95,19 @@ export async function setupDefaultTemplate(): Promise { console.log('βœ… Schichten erstellt'); + // In der problematischen Stelle: + const createdTemplate = await db.get( + 'SELECT * FROM shift_templates WHERE id = ?', + [templateId] + ) as { name: string } | undefined; + console.log('πŸ“‹ Erstellte Vorlage:', createdTemplate?.name); + + const shiftCount = await db.get( + 'SELECT COUNT(*) as count FROM template_shifts WHERE template_id = ?', + [templateId] + ) as { count: number } | undefined; + console.log(`πŸ“Š Anzahl Schichten: ${shiftCount?.count}`); + await db.run('COMMIT'); console.log('πŸŽ‰ Standard-Vorlage erfolgreich initialisiert'); diff --git a/backend/src/server.ts b/backend/src/server.ts index 726f199..17dc7f3 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -58,17 +58,17 @@ const initializeApp = async () => { try { // Initialize database with base schema await initializeDatabase(); - console.log('βœ… Database initialized successfully'); + //console.log('βœ… Database initialized successfully'); // Apply any pending migrations const { applyMigration } = await import('./scripts/applyMigration.js'); await applyMigration(); - console.log('βœ… Database migrations applied'); + //console.log('βœ… Database migrations applied'); // Setup default template await setupDefaultTemplate(); - console.log('βœ… Default template checked/created'); - + //console.log('βœ… Default template checked/created'); + // Start server only after successful initialization app.listen(PORT, () => { console.log('πŸŽ‰ BACKEND STARTED SUCCESSFULLY!');