mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-11-30 22:45:46 +01:00
added shiftplan generating logic
This commit is contained in:
@@ -10,7 +10,10 @@ export const getEmployees = async (req: AuthRequest, res: Response): Promise<voi
|
||||
try {
|
||||
console.log('🔍 Fetching employees - User:', req.user);
|
||||
|
||||
const employees = await db.all<any>(`
|
||||
const { includeInactive } = req.query;
|
||||
const includeInactiveFlag = includeInactive === 'true';
|
||||
|
||||
let query = `
|
||||
SELECT
|
||||
id, email, name, role, is_active as isActive,
|
||||
employee_type as employeeType,
|
||||
@@ -19,9 +22,15 @@ export const getEmployees = async (req: AuthRequest, res: Response): Promise<voi
|
||||
created_at as createdAt,
|
||||
last_login as lastLogin
|
||||
FROM employees
|
||||
WHERE is_active = 1
|
||||
ORDER BY name
|
||||
`);
|
||||
`;
|
||||
|
||||
if (!includeInactiveFlag) {
|
||||
query += ' WHERE is_active = 1';
|
||||
}
|
||||
|
||||
query += ' ORDER BY name';
|
||||
|
||||
const employees = await db.all<any>(query);
|
||||
|
||||
console.log('✅ Employees found:', employees.length);
|
||||
res.json(employees);
|
||||
|
||||
117
backend/src/routes/scheduledShifts.ts
Normal file
117
backend/src/routes/scheduledShifts.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
// backend/src/routes/scheduledShifts.ts - COMPLETE REWRITE
|
||||
import express from 'express';
|
||||
import { authMiddleware, requireRole } from '../middleware/auth.js';
|
||||
import { db } from '../services/databaseService.js';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use(authMiddleware);
|
||||
|
||||
// GET all scheduled shifts for a plan (for debugging)
|
||||
router.get('/plan/:planId', async (req, res) => {
|
||||
try {
|
||||
const { planId } = req.params;
|
||||
|
||||
const shifts = await db.all(
|
||||
`SELECT * FROM scheduled_shifts WHERE plan_id = ? ORDER BY date, time_slot_id`,
|
||||
[planId]
|
||||
);
|
||||
|
||||
// Parse JSON arrays safely
|
||||
const parsedShifts = shifts.map((shift: any) => {
|
||||
try {
|
||||
return {
|
||||
...shift,
|
||||
assigned_employees: JSON.parse(shift.assigned_employees || '[]')
|
||||
};
|
||||
} catch (parseError) {
|
||||
console.error('Error parsing assigned_employees:', parseError);
|
||||
return {
|
||||
...shift,
|
||||
assigned_employees: []
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
res.json(parsedShifts);
|
||||
} catch (error) {
|
||||
console.error('Error fetching scheduled shifts:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET specific scheduled shift
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const shift = await db.get(
|
||||
'SELECT * FROM scheduled_shifts WHERE id = ?',
|
||||
[id]
|
||||
) as any;
|
||||
|
||||
if (!shift) {
|
||||
return res.status(404).json({ error: 'Scheduled shift not found' });
|
||||
}
|
||||
|
||||
// Parse JSON array
|
||||
const parsedShift = {
|
||||
...shift,
|
||||
assigned_employees: JSON.parse(shift.assigned_employees || '[]')
|
||||
};
|
||||
|
||||
res.json(parsedShift);
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching scheduled shift:', error);
|
||||
res.status(500).json({ error: 'Internal server error: ' + error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// UPDATE scheduled shift
|
||||
router.put('/:id', requireRole(['admin', 'instandhalter']), async (req, res) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
const { assignedEmployees } = req.body;
|
||||
|
||||
console.log('🔄 Updating scheduled shift:', {
|
||||
id,
|
||||
assignedEmployees,
|
||||
body: req.body
|
||||
});
|
||||
|
||||
if (!Array.isArray(assignedEmployees)) {
|
||||
return res.status(400).json({ error: 'assignedEmployees must be an array' });
|
||||
}
|
||||
|
||||
// Check if shift exists
|
||||
const existingShift = await db.get(
|
||||
'SELECT id FROM scheduled_shifts WHERE id = ?',
|
||||
[id]
|
||||
) as any;
|
||||
|
||||
if (!existingShift) {
|
||||
console.error('❌ Scheduled shift not found:', id);
|
||||
return res.status(404).json({ error: `Scheduled shift ${id} not found` });
|
||||
}
|
||||
|
||||
// Update the shift
|
||||
const result = await db.run(
|
||||
'UPDATE scheduled_shifts SET assigned_employees = ? WHERE id = ?',
|
||||
[JSON.stringify(assignedEmployees), id]
|
||||
);
|
||||
|
||||
console.log('✅ Scheduled shift updated successfully');
|
||||
|
||||
res.json({
|
||||
message: 'Scheduled shift updated successfully',
|
||||
id: id,
|
||||
assignedEmployees: assignedEmployees
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('❌ Error updating scheduled shift:', error);
|
||||
res.status(500).json({ error: 'Internal server error: ' + error.message });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -8,6 +8,7 @@ import authRoutes from './routes/auth.js';
|
||||
import employeeRoutes from './routes/employees.js';
|
||||
import shiftPlanRoutes from './routes/shiftPlans.js';
|
||||
import setupRoutes from './routes/setup.js';
|
||||
import scheduledShiftsRouter from './routes/scheduledShifts.js';
|
||||
|
||||
const app = express();
|
||||
const PORT = 3002;
|
||||
@@ -21,6 +22,18 @@ app.use('/api/setup', setupRoutes);
|
||||
app.use('/api/auth', authRoutes);
|
||||
app.use('/api/employees', employeeRoutes);
|
||||
app.use('/api/shift-plans', shiftPlanRoutes);
|
||||
app.use('/api/scheduled-shifts', scheduledShiftsRouter);
|
||||
|
||||
// Error handling middleware should come after routes
|
||||
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
console.error('Unhandled error:', err);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
});
|
||||
|
||||
// 404 handler for API routes
|
||||
app.use('/api/*', (req, res) => {
|
||||
res.status(404).json({ error: 'API endpoint not found' });
|
||||
});
|
||||
|
||||
// Health route
|
||||
app.get('/api/health', (req: any, res: any) => {
|
||||
|
||||
Reference in New Issue
Block a user