mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-12-01 06:55:45 +01:00
can add and delete shiftplans from pressets
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// frontend/src/services/authService.ts
|
||||
import { Employee } from '../../../backend/src/models/employee';
|
||||
import { Employee } from '../models/Employee';
|
||||
const API_BASE = 'http://localhost:3002/api';
|
||||
|
||||
export interface LoginRequest {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// frontend/src/services/employeeService.ts
|
||||
import { Employee, CreateEmployeeRequest, UpdateEmployeeRequest, EmployeeAvailability } from '../../../backend/src/models/employee';
|
||||
import { Employee, CreateEmployeeRequest, UpdateEmployeeRequest, EmployeeAvailability } from '../models/Employee';
|
||||
|
||||
const API_BASE_URL = 'http://localhost:3002/api';
|
||||
|
||||
|
||||
@@ -1,9 +1,28 @@
|
||||
// frontend/src/services/shiftPlanService.ts
|
||||
import { authService } from './authService';
|
||||
import { ShiftPlan, CreateShiftPlanRequest, Shift } from '../models/ShiftPlan';
|
||||
import { ShiftPlan, CreateShiftPlanRequest, Shift, CreateShiftFromTemplateRequest } from '../models/ShiftPlan';
|
||||
import { TEMPLATE_PRESETS } from '../models/defaults/shiftPlanDefaults';
|
||||
|
||||
const API_BASE = 'http://localhost:3002/api/shift-plans';
|
||||
|
||||
// Helper function to get auth headers
|
||||
const getAuthHeaders = () => {
|
||||
const token = localStorage.getItem('token');
|
||||
return {
|
||||
'Content-Type': 'application/json',
|
||||
...(token && { 'Authorization': `Bearer ${token}` })
|
||||
};
|
||||
};
|
||||
|
||||
// Helper function to handle responses
|
||||
const handleResponse = async (response: Response) => {
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||
throw new Error(errorData.error || `HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const shiftPlanService = {
|
||||
async getShiftPlans(): Promise<ShiftPlan[]> {
|
||||
const response = await fetch(API_BASE, {
|
||||
@@ -101,5 +120,70 @@ export const shiftPlanService = {
|
||||
}
|
||||
throw new Error('Fehler beim Löschen des Schichtplans');
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getTemplates: async (): Promise<ShiftPlan[]> => {
|
||||
const response = await fetch(`${API_BASE}/templates`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse(response);
|
||||
},
|
||||
|
||||
// Get specific template or plan
|
||||
getTemplate: async (id: string): Promise<ShiftPlan> => {
|
||||
const response = await fetch(`${API_BASE}/${id}`, {
|
||||
headers: getAuthHeaders()
|
||||
});
|
||||
return handleResponse(response);
|
||||
},
|
||||
|
||||
// Create plan from template
|
||||
createFromTemplate: async (data: CreateShiftFromTemplateRequest): Promise<ShiftPlan> => {
|
||||
const response = await fetch(`${API_BASE}/from-template`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return handleResponse(response);
|
||||
},
|
||||
|
||||
// Create new plan
|
||||
createPlan: async (data: CreateShiftPlanRequest): Promise<ShiftPlan> => {
|
||||
const response = await fetch(`${API_BASE}`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return handleResponse(response);
|
||||
},
|
||||
|
||||
createFromPreset: async (data: {
|
||||
presetName: string;
|
||||
name: string;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
isTemplate?: boolean;
|
||||
}): Promise<ShiftPlan> => {
|
||||
const response = await fetch(`${API_BASE}/from-preset`, {
|
||||
method: 'POST',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||
throw new Error(errorData.error || `HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
},
|
||||
|
||||
getTemplatePresets: async (): Promise<{name: string, label: string, description: string}[]> => {
|
||||
// name = label
|
||||
return Object.entries(TEMPLATE_PRESETS).map(([key, preset]) => ({
|
||||
name: key,
|
||||
label: preset.name,
|
||||
description: preset.description
|
||||
}));
|
||||
},
|
||||
};
|
||||
@@ -1,105 +0,0 @@
|
||||
// frontend/src/services/shiftTemplateService.ts
|
||||
import { ShiftPlan } from '../../../backend/src/models/shiftPlan.js';
|
||||
import { authService } from './authService';
|
||||
|
||||
const API_BASE = 'http://localhost:3002/api/shift-templates';
|
||||
|
||||
export const shiftTemplateService = {
|
||||
async getTemplates(): Promise<ShiftPlan[]> {
|
||||
const response = await fetch(API_BASE, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...authService.getAuthHeaders()
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
authService.logout();
|
||||
throw new Error('Nicht authorisiert - bitte erneut anmelden');
|
||||
}
|
||||
throw new Error('Fehler beim Laden der Vorlagen');
|
||||
}
|
||||
|
||||
const templates = await response.json();
|
||||
return templates;
|
||||
},
|
||||
|
||||
async getTemplate(id: string): Promise<ShiftPlan> {
|
||||
const response = await fetch(`${API_BASE}/${id}`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...authService.getAuthHeaders()
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
authService.logout();
|
||||
throw new Error('Nicht authorisiert - bitte erneut anmelden');
|
||||
}
|
||||
throw new Error('Vorlage nicht gefunden');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
},
|
||||
|
||||
async createTemplate(template: Omit<ShiftPlan, 'id' | 'createdAt' | 'createdBy'>): Promise<ShiftPlan> {
|
||||
const response = await fetch(API_BASE, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...authService.getAuthHeaders()
|
||||
},
|
||||
body: JSON.stringify(template)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
authService.logout();
|
||||
throw new Error('Nicht authorisiert - bitte erneut anmelden');
|
||||
}
|
||||
throw new Error('Fehler beim Erstellen der Vorlage');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
},
|
||||
|
||||
async updateTemplate(id: string, template: Partial<ShiftPlan>): Promise<ShiftPlan> {
|
||||
const response = await fetch(`${API_BASE}/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...authService.getAuthHeaders()
|
||||
},
|
||||
body: JSON.stringify(template)
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
authService.logout();
|
||||
throw new Error('Nicht authorisiert - bitte erneut anmelden');
|
||||
}
|
||||
throw new Error('Fehler beim Aktualisieren der Vorlage');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
},
|
||||
|
||||
async deleteTemplate(id: string): Promise<void> {
|
||||
const response = await fetch(`${API_BASE}/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
...authService.getAuthHeaders()
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
authService.logout();
|
||||
throw new Error('Nicht authorisiert - bitte erneut anmelden');
|
||||
}
|
||||
throw new Error('Fehler beim Löschen der Vorlage');
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user