mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-12-01 06:55:45 +01:00
added scheduledShiftController in backend; fixed API routes for scheduleController
This commit is contained in:
@@ -186,105 +186,4 @@ export const shiftPlanService = {
|
||||
description: preset.description
|
||||
}));
|
||||
},
|
||||
|
||||
async updateScheduledShift(id: string, updates: { assignedEmployees: string[] }): Promise<void> {
|
||||
try {
|
||||
console.log('🔄 Updating scheduled shift via API:', { id, updates });
|
||||
|
||||
const response = await fetch(`/api/scheduled-shifts/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
||||
},
|
||||
body: JSON.stringify(updates)
|
||||
});
|
||||
|
||||
// First, check if we got any response
|
||||
if (!response.ok) {
|
||||
// Try to get error message from response
|
||||
const responseText = await response.text();
|
||||
console.error('❌ Server response:', responseText);
|
||||
|
||||
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
||||
|
||||
// Try to parse as JSON if possible
|
||||
try {
|
||||
const errorData = JSON.parse(responseText);
|
||||
errorMessage = errorData.error || errorMessage;
|
||||
} catch (e) {
|
||||
// If not JSON, use the text as is
|
||||
errorMessage = responseText || errorMessage;
|
||||
}
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
// Try to parse successful response
|
||||
const responseText = await response.text();
|
||||
let result;
|
||||
try {
|
||||
result = responseText ? JSON.parse(responseText) : {};
|
||||
} catch (e) {
|
||||
console.warn('⚠️ Response was not JSON, but request succeeded');
|
||||
result = { message: 'Update successful' };
|
||||
}
|
||||
|
||||
console.log('✅ Scheduled shift updated successfully:', result);
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error updating scheduled shift:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
async getScheduledShift(id: string): Promise<any> {
|
||||
try {
|
||||
const response = await fetch(`/api/scheduled-shifts/${id}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const responseText = await response.text();
|
||||
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
|
||||
|
||||
try {
|
||||
const errorData = JSON.parse(responseText);
|
||||
errorMessage = errorData.error || errorMessage;
|
||||
} catch (e) {
|
||||
errorMessage = responseText || errorMessage;
|
||||
}
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
const responseText = await response.text();
|
||||
return responseText ? JSON.parse(responseText) : {};
|
||||
} catch (error) {
|
||||
console.error('Error fetching scheduled shift:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
// New method to get all scheduled shifts for a plan
|
||||
async getScheduledShiftsForPlan(planId: string): Promise<ScheduledShift[]> {
|
||||
try {
|
||||
const response = await fetch(`/api/scheduled-shifts/plan/${planId}`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${localStorage.getItem('token')}`
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch scheduled shifts: ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error('Error fetching scheduled shifts for plan:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user