mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-12-01 06:55:45 +01:00
shiftplans shown correctly
This commit is contained in:
@@ -4,5 +4,5 @@ WORKDIR /app
|
|||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm ci --only=production
|
RUN npm ci --only=production
|
||||||
COPY . .
|
COPY . .
|
||||||
EXPOSE 3001
|
EXPOSE 3002
|
||||||
CMD ["node", "dist/server.js"]
|
CMD ["node", "dist/server.js"]
|
||||||
@@ -144,6 +144,8 @@ export const updateEmployee = async (req: AuthRequest, res: Response): Promise<v
|
|||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const { name, role, isActive, employeeType, isSufficientlyIndependent } = req.body;
|
const { name, role, isActive, employeeType, isSufficientlyIndependent } = req.body;
|
||||||
|
|
||||||
|
console.log('📝 Update Employee Request:', { id, name, role, isActive, employeeType, isSufficientlyIndependent });
|
||||||
|
|
||||||
// Check if employee exists
|
// Check if employee exists
|
||||||
const existingEmployee = await db.get('SELECT * FROM users WHERE id = ?', [id]);
|
const existingEmployee = await db.get('SELECT * FROM users WHERE id = ?', [id]);
|
||||||
if (!existingEmployee) {
|
if (!existingEmployee) {
|
||||||
@@ -163,6 +165,8 @@ export const updateEmployee = async (req: AuthRequest, res: Response): Promise<v
|
|||||||
[name, role, isActive, employeeType, isSufficientlyIndependent, id]
|
[name, role, isActive, employeeType, isSufficientlyIndependent, id]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log('✅ Employee updated successfully');
|
||||||
|
|
||||||
// Return updated employee
|
// Return updated employee
|
||||||
const updatedEmployee = await db.get<any>(`
|
const updatedEmployee = await db.get<any>(`
|
||||||
SELECT
|
SELECT
|
||||||
|
|||||||
@@ -298,44 +298,59 @@ export const deleteShiftPlan = async (req: AuthRequest, res: Response): Promise<
|
|||||||
|
|
||||||
// Helper function to generate shifts from template
|
// Helper function to generate shifts from template
|
||||||
async function generateShiftsFromTemplate(shiftPlanId: string, templateId: string, startDate: string, endDate: string): Promise<void> {
|
async function generateShiftsFromTemplate(shiftPlanId: string, templateId: string, startDate: string, endDate: string): Promise<void> {
|
||||||
// Get template shifts
|
try {
|
||||||
const templateShifts = await db.all<any>(`
|
console.log(`🔄 Generiere Schichten von Vorlage ${templateId} für Plan ${shiftPlanId}`);
|
||||||
SELECT * FROM template_shifts
|
|
||||||
WHERE template_id = ?
|
|
||||||
ORDER BY day_of_week, start_time
|
|
||||||
`, [templateId]);
|
|
||||||
|
|
||||||
const start = new Date(startDate);
|
// Get template shifts with time slot information
|
||||||
const end = new Date(endDate);
|
const templateShifts = await db.all<any>(`
|
||||||
|
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]);
|
||||||
|
|
||||||
// Generate shifts ONLY for days that have template shifts defined
|
console.log(`📋 Gefundene Template-Schichten: ${templateShifts.length}`);
|
||||||
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();
|
|
||||||
|
|
||||||
// Find template shifts for this day of week
|
const start = new Date(startDate);
|
||||||
const shiftsForDay = templateShifts.filter(shift => shift.day_of_week === dayOfWeek);
|
const end = new Date(endDate);
|
||||||
|
|
||||||
// Only create shifts if there are template shifts defined for this weekday
|
// Generate shifts ONLY for days that have template shifts defined
|
||||||
if (shiftsForDay.length > 0) {
|
for (let date = new Date(start); date <= end; date.setDate(date.getDate() + 1)) {
|
||||||
for (const templateShift of shiftsForDay) {
|
// Convert JS day (0=Sunday) to our format (1=Monday, 7=Sunday)
|
||||||
const shiftId = uuidv4();
|
const dayOfWeek = date.getDay() === 0 ? 7 : date.getDay();
|
||||||
|
|
||||||
await db.run(
|
// Find template shifts for this day of week
|
||||||
`INSERT INTO assigned_shifts (id, shift_plan_id, date, name, start_time, end_time, required_employees, assigned_employees)
|
const shiftsForDay = templateShifts.filter(shift => shift.day_of_week === dayOfWeek);
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
||||||
[
|
// Only create shifts if there are template shifts defined for this weekday
|
||||||
shiftId,
|
if (shiftsForDay.length > 0) {
|
||||||
shiftPlanId,
|
for (const templateShift of shiftsForDay) {
|
||||||
date.toISOString().split('T')[0],
|
const shiftId = uuidv4();
|
||||||
templateShift.name,
|
|
||||||
templateShift.start_time,
|
await db.run(
|
||||||
templateShift.end_time,
|
`INSERT INTO assigned_shifts (id, shift_plan_id, date, name, start_time, end_time, required_employees, assigned_employees)
|
||||||
templateShift.required_employees,
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
JSON.stringify([])
|
[
|
||||||
]
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ export interface AuthRequest extends Request {
|
|||||||
|
|
||||||
export const authMiddleware = (req: AuthRequest, res: Response, next: NextFunction): void => {
|
export const authMiddleware = (req: AuthRequest, res: Response, next: NextFunction): void => {
|
||||||
const authHeader = req.header('Authorization');
|
const authHeader = req.header('Authorization');
|
||||||
console.log('🔐 Auth middleware - Authorization header:', authHeader);
|
//console.log('🔐 Auth middleware - Authorization header:', authHeader);
|
||||||
|
|
||||||
const token = authHeader?.replace('Bearer ', '');
|
const token = authHeader?.replace('Bearer ', '');
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ export const authMiddleware = (req: AuthRequest, res: Response, next: NextFuncti
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const decoded = jwt.verify(token, JWT_SECRET) as any;
|
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
|
// KORREKTUR: Verwende 'id' aus dem JWT Payload
|
||||||
req.user = {
|
req.user = {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// backend/src/scripts/setupDefaultTemplate.ts
|
// backend/src/scripts/setupDefaultTemplate.ts
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
import { db } from '../services/databaseService.js';
|
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 {
|
interface AdminUser {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -95,6 +95,19 @@ export async function setupDefaultTemplate(): Promise<void> {
|
|||||||
|
|
||||||
console.log('✅ Schichten erstellt');
|
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');
|
await db.run('COMMIT');
|
||||||
console.log('🎉 Standard-Vorlage erfolgreich initialisiert');
|
console.log('🎉 Standard-Vorlage erfolgreich initialisiert');
|
||||||
|
|
||||||
|
|||||||
@@ -58,16 +58,16 @@ const initializeApp = async () => {
|
|||||||
try {
|
try {
|
||||||
// Initialize database with base schema
|
// Initialize database with base schema
|
||||||
await initializeDatabase();
|
await initializeDatabase();
|
||||||
console.log('✅ Database initialized successfully');
|
//console.log('✅ Database initialized successfully');
|
||||||
|
|
||||||
// Apply any pending migrations
|
// Apply any pending migrations
|
||||||
const { applyMigration } = await import('./scripts/applyMigration.js');
|
const { applyMigration } = await import('./scripts/applyMigration.js');
|
||||||
await applyMigration();
|
await applyMigration();
|
||||||
console.log('✅ Database migrations applied');
|
//console.log('✅ Database migrations applied');
|
||||||
|
|
||||||
// Setup default template
|
// Setup default template
|
||||||
await setupDefaultTemplate();
|
await setupDefaultTemplate();
|
||||||
console.log('✅ Default template checked/created');
|
//console.log('✅ Default template checked/created');
|
||||||
|
|
||||||
// Start server only after successful initialization
|
// Start server only after successful initialization
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user