mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-11-30 22:45:46 +01:00
updated employee and shift structure
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
// backend/src/controllers/authController.ts
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { Request, Response } from 'express';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { db } from '../services/databaseService.js';
|
||||
import { AuthRequest } from '../middleware/auth.js';
|
||||
import { Employee, EmployeeWithPassword } from '../models/Employee.js';
|
||||
|
||||
export interface User {
|
||||
id: number;
|
||||
@@ -50,8 +52,8 @@ export const login = async (req: Request, res: Response) => {
|
||||
}
|
||||
|
||||
// Get user from database
|
||||
const user = await db.get<UserWithPassword>(
|
||||
'SELECT id, email, password, name, role FROM users WHERE email = ? AND is_active = 1',
|
||||
const user = await db.get<EmployeeWithPassword>(
|
||||
'SELECT id, email, password, name, role, employee_type as employeeType, contract_type as contractType, can_work_alone as canWorkAlone, is_active as isActive FROM employees WHERE email = ? AND is_active = 1',
|
||||
[email]
|
||||
);
|
||||
|
||||
@@ -114,9 +116,9 @@ export const getCurrentUser = async (req: Request, res: Response) => {
|
||||
return res.status(401).json({ error: 'Nicht authentifiziert' });
|
||||
}
|
||||
|
||||
const user = await db.get<User>(
|
||||
'SELECT id, email, name, role FROM users WHERE id = ? AND is_active = 1',
|
||||
[jwtUser.userId] // ← HIER: userId verwenden
|
||||
const user = await db.get<Employee>(
|
||||
'SELECT id, email, name, role, employee_type as employeeType, contract_type as contractType, can_work_alone as canWorkAlone, is_active as isActive FROM employees WHERE id = ? AND is_active = 1',
|
||||
[jwtUser.userId]
|
||||
);
|
||||
|
||||
console.log('🔍 User found in database:', user ? 'Yes' : 'No');
|
||||
@@ -171,8 +173,8 @@ export const register = async (req: Request, res: Response) => {
|
||||
}
|
||||
|
||||
// Check if email already exists
|
||||
const existingUser = await db.get<User>(
|
||||
'SELECT id FROM users WHERE email = ?',
|
||||
const existingUser = await db.get<Employee>(
|
||||
'SELECT id FROM employees WHERE email = ?',
|
||||
[email]
|
||||
);
|
||||
|
||||
@@ -187,9 +189,9 @@ export const register = async (req: Request, res: Response) => {
|
||||
|
||||
// Insert user
|
||||
const result = await db.run(
|
||||
`INSERT INTO users (email, password, name, role)
|
||||
VALUES (?, ?, ?, ?)`,
|
||||
[email, hashedPassword, name, role]
|
||||
`INSERT INTO employees (id, email, password, name, role, employee_type, contract_type, can_work_alone, is_active)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[uuidv4(), email, hashedPassword, name, role, 'experienced', 'small', false, 1]
|
||||
);
|
||||
|
||||
if (!result.lastID) {
|
||||
@@ -197,8 +199,8 @@ export const register = async (req: Request, res: Response) => {
|
||||
}
|
||||
|
||||
// Get created user
|
||||
const newUser = await db.get<User>(
|
||||
'SELECT id, email, name, role FROM users WHERE id = ?',
|
||||
const newUser = await db.get<Employee>(
|
||||
'SELECT id, email, name, role FROM employees WHERE id = ?',
|
||||
[result.lastID]
|
||||
);
|
||||
|
||||
|
||||
@@ -3,34 +3,7 @@ import { Request, Response } from 'express';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { db } from '../services/databaseService.js';
|
||||
import { AuthRequest } from '../middleware/auth.js';
|
||||
|
||||
export interface ShiftPlan {
|
||||
id: string;
|
||||
name: string;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
templateId?: string;
|
||||
status: 'draft' | 'published';
|
||||
createdBy: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface AssignedShift {
|
||||
id: string;
|
||||
shiftPlanId: string;
|
||||
date: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
requiredEmployees: number;
|
||||
assignedEmployees: string[];
|
||||
}
|
||||
|
||||
export interface CreateShiftPlanRequest {
|
||||
name: string;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
templateId?: string;
|
||||
}
|
||||
import { ShiftPlan, CreateShiftPlanRequest } from '../models/ShiftPlan.js';
|
||||
|
||||
export const getShiftPlans = async (req: AuthRequest, res: Response): Promise<void> => {
|
||||
try {
|
||||
|
||||
@@ -1,83 +1,118 @@
|
||||
-- Tabelle für Benutzer
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
-- Employees table
|
||||
CREATE TABLE IF NOT EXISTS employees (
|
||||
id TEXT PRIMARY KEY,
|
||||
email TEXT UNIQUE NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
role TEXT CHECK(role IN ('admin', 'user', 'instandhalter')) NOT NULL,
|
||||
employee_type TEXT CHECK(employee_type IN ('chef', 'neuling', 'erfahren')),
|
||||
is_sufficiently_independent BOOLEAN DEFAULT FALSE,
|
||||
role TEXT CHECK(role IN ('admin', 'user', 'maintenance')) NOT NULL,
|
||||
employee_type TEXT CHECK(employee_type IN ('manager', 'trainee', 'experienced')) NOT NULL,
|
||||
contract_type TEXT CHECK(contract_type IN ('small', 'large')) NOT NULL,
|
||||
can_work_alone BOOLEAN DEFAULT FALSE,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
last_login TEXT DEFAULT NULL
|
||||
);
|
||||
|
||||
-- Tabelle für Schichtvorlagen
|
||||
CREATE TABLE IF NOT EXISTS shift_templates (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
is_default BOOLEAN DEFAULT FALSE,
|
||||
created_by TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (created_by) REFERENCES users(id)
|
||||
);
|
||||
|
||||
-- Tabelle für die Schichten in den Vorlagen
|
||||
CREATE TABLE IF NOT EXISTS template_shifts (
|
||||
id TEXT PRIMARY KEY,
|
||||
template_id TEXT NOT NULL,
|
||||
time_slot_id TEXT NOT NULL,
|
||||
day_of_week INTEGER NOT NULL CHECK (day_of_week >= 1 AND day_of_week <= 7),
|
||||
required_employees INTEGER DEFAULT 1,
|
||||
color TEXT DEFAULT '#3498db',
|
||||
FOREIGN KEY (template_id) REFERENCES shift_templates(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (time_slot_id) REFERENCES template_time_slots(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Tabelle für Zeitbereiche in den Vorlagen
|
||||
CREATE TABLE IF NOT EXISTS template_time_slots (
|
||||
id TEXT PRIMARY KEY,
|
||||
template_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
start_time TEXT NOT NULL,
|
||||
end_time TEXT NOT NULL,
|
||||
FOREIGN KEY (template_id) REFERENCES shift_templates(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Zusätzliche Tabellen für shift_plans
|
||||
-- Shift plans table
|
||||
CREATE TABLE IF NOT EXISTS shift_plans (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
start_date TEXT NOT NULL,
|
||||
end_date TEXT NOT NULL,
|
||||
template_id TEXT,
|
||||
status TEXT CHECK(status IN ('draft', 'published')) DEFAULT 'draft',
|
||||
description TEXT,
|
||||
start_date TEXT,
|
||||
end_date TEXT,
|
||||
is_template BOOLEAN DEFAULT FALSE,
|
||||
status TEXT CHECK(status IN ('draft', 'published', 'archived', 'template')) DEFAULT 'draft',
|
||||
created_by TEXT NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (created_by) REFERENCES users(id),
|
||||
FOREIGN KEY (template_id) REFERENCES shift_templates(id)
|
||||
FOREIGN KEY (created_by) REFERENCES employees(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS assigned_shifts (
|
||||
-- Time slots within plans
|
||||
CREATE TABLE IF NOT EXISTS time_slots (
|
||||
id TEXT PRIMARY KEY,
|
||||
plan_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
shift_plan_id TEXT NOT NULL,
|
||||
date TEXT NOT NULL,
|
||||
start_time TEXT NOT NULL,
|
||||
end_time TEXT NOT NULL,
|
||||
required_employees INTEGER DEFAULT 1,
|
||||
assigned_employees TEXT DEFAULT '[]', -- JSON array of user IDs
|
||||
FOREIGN KEY (shift_plan_id) REFERENCES shift_plans(id) ON DELETE CASCADE
|
||||
description TEXT,
|
||||
FOREIGN KEY (plan_id) REFERENCES shift_plans(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Zusätzliche Tabelle für Mitarbeiter-Verfügbarkeiten
|
||||
CREATE TABLE IF NOT EXISTS employee_availabilities (
|
||||
-- Shifts table (defines shifts for each day of week in the plan)
|
||||
CREATE TABLE IF NOT EXISTS shifts (
|
||||
id TEXT PRIMARY KEY,
|
||||
plan_id TEXT NOT NULL,
|
||||
time_slot_id TEXT NOT NULL,
|
||||
day_of_week INTEGER NOT NULL CHECK (day_of_week >= 1 AND day_of_week <= 7),
|
||||
required_employees INTEGER NOT NULL CHECK (required_employees >= 1 AND required_employees <= 10) DEFAULT 2,
|
||||
color TEXT DEFAULT '#3498db',
|
||||
FOREIGN KEY (plan_id) REFERENCES shift_plans(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (time_slot_id) REFERENCES time_slots(id) ON DELETE CASCADE,
|
||||
UNIQUE(plan_id, time_slot_id, day_of_week)
|
||||
);
|
||||
|
||||
-- Actual scheduled shifts (generated from plan + date range)
|
||||
CREATE TABLE IF NOT EXISTS scheduled_shifts (
|
||||
id TEXT PRIMARY KEY,
|
||||
plan_id TEXT NOT NULL,
|
||||
date TEXT NOT NULL,
|
||||
time_slot_id TEXT NOT NULL,
|
||||
required_employees INTEGER NOT NULL CHECK (required_employees >= 1 AND required_employees <= 10) DEFAULT 2,
|
||||
assigned_employees TEXT DEFAULT '[]', -- JSON array of employee IDs
|
||||
FOREIGN KEY (plan_id) REFERENCES shift_plans(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (time_slot_id) REFERENCES time_slots(id),
|
||||
UNIQUE(plan_id, date, time_slot_id)
|
||||
);
|
||||
|
||||
-- Employee assignments to specific shifts
|
||||
CREATE TABLE IF NOT EXISTS shift_assignments (
|
||||
id TEXT PRIMARY KEY,
|
||||
scheduled_shift_id TEXT NOT NULL,
|
||||
employee_id TEXT NOT NULL,
|
||||
assignment_status TEXT CHECK(assignment_status IN ('assigned', 'cancelled')) DEFAULT 'assigned',
|
||||
assigned_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
assigned_by TEXT NOT NULL,
|
||||
FOREIGN KEY (scheduled_shift_id) REFERENCES scheduled_shifts(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (employee_id) REFERENCES employees(id),
|
||||
FOREIGN KEY (assigned_by) REFERENCES employees(id),
|
||||
UNIQUE(scheduled_shift_id, employee_id)
|
||||
);
|
||||
|
||||
-- Employee availability preferences for specific shift plans
|
||||
CREATE TABLE IF NOT EXISTS employee_availability (
|
||||
id TEXT PRIMARY KEY,
|
||||
employee_id TEXT NOT NULL,
|
||||
day_of_week INTEGER NOT NULL CHECK (day_of_week >= 0 AND day_of_week <= 6),
|
||||
start_time TEXT NOT NULL,
|
||||
end_time TEXT NOT NULL,
|
||||
is_available BOOLEAN DEFAULT FALSE,
|
||||
FOREIGN KEY (employee_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
plan_id TEXT NOT NULL,
|
||||
day_of_week INTEGER NOT NULL CHECK (day_of_week >= 1 AND day_of_week <= 7),
|
||||
time_slot_id TEXT NOT NULL,
|
||||
preference_level INTEGER CHECK(preference_level IN (1, 2, 3)) NOT NULL,
|
||||
notes TEXT,
|
||||
FOREIGN KEY (employee_id) REFERENCES employees(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (plan_id) REFERENCES shift_plans(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (time_slot_id) REFERENCES time_slots(id),
|
||||
UNIQUE(employee_id, plan_id, day_of_week, time_slot_id)
|
||||
);
|
||||
|
||||
-- Performance indexes
|
||||
CREATE INDEX IF NOT EXISTS idx_employees_role_active ON employees(role, is_active);
|
||||
CREATE INDEX IF NOT EXISTS idx_employees_email_active ON employees(email, is_active);
|
||||
CREATE INDEX IF NOT EXISTS idx_employees_type_active ON employees(employee_type, is_active);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_shift_plans_status_date ON shift_plans(status, start_date, end_date);
|
||||
CREATE INDEX IF NOT EXISTS idx_shift_plans_created_by ON shift_plans(created_by);
|
||||
CREATE INDEX IF NOT EXISTS idx_shift_plans_template ON shift_plans(is_template, status);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_time_slots_plan ON time_slots(plan_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_shifts_plan_day ON shifts(plan_id, day_of_week);
|
||||
CREATE INDEX IF NOT EXISTS idx_shifts_required_employees ON shifts(required_employees);
|
||||
CREATE INDEX IF NOT EXISTS idx_shifts_plan_time ON shifts(plan_id, time_slot_id, day_of_week);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_scheduled_shifts_plan_date ON scheduled_shifts(plan_id, date);
|
||||
CREATE INDEX IF NOT EXISTS idx_scheduled_shifts_date_time ON scheduled_shifts(date, time_slot_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_scheduled_shifts_required_employees ON scheduled_shifts(required_employees);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_shift_assignments_employee ON shift_assignments(employee_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_shift_assignments_shift ON shift_assignments(scheduled_shift_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_employee_availability_employee_plan ON employee_availability(employee_id, plan_id);
|
||||
@@ -3,9 +3,10 @@ export interface Employee {
|
||||
id: string;
|
||||
email: string;
|
||||
name: string;
|
||||
role: 'admin' | 'instandhalter' | 'user';
|
||||
employeeType: 'chef' | 'neuling' | 'erfahren';
|
||||
isSufficientlyIndependent: boolean;
|
||||
role: 'admin' | 'maintenance' | 'user';
|
||||
employeeType: 'manager' | 'trainee' | 'experienced';
|
||||
contractType: 'small' | 'large';
|
||||
canWorkAlone: boolean;
|
||||
isActive: boolean;
|
||||
createdAt: string;
|
||||
lastLogin?: string | null;
|
||||
@@ -15,19 +16,60 @@ export interface CreateEmployeeRequest {
|
||||
email: string;
|
||||
password: string;
|
||||
name: string;
|
||||
role: 'admin' | 'instandhalter' | 'user';
|
||||
employeeType: 'chef' | 'neuling' | 'erfahren';
|
||||
isSufficientlyIndependent: boolean;
|
||||
role: 'admin' | 'maintenance' | 'user';
|
||||
employeeType: 'manager' | 'trainee' | 'experienced';
|
||||
contractType: 'small' | 'large';
|
||||
canWorkAlone: boolean;
|
||||
}
|
||||
|
||||
export interface UpdateEmployeeRequest {
|
||||
name?: string;
|
||||
role?: 'admin' | 'instandhalter' | 'user';
|
||||
employeeType?: 'chef' | 'neuling' | 'erfahren';
|
||||
isSufficientlyIndependent?: boolean;
|
||||
role?: 'admin' | 'maintenance' | 'user';
|
||||
employeeType?: 'manager' | 'trainee' | 'experienced';
|
||||
contractType?: 'small' | 'large';
|
||||
canWorkAlone?: boolean;
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
export interface EmployeeWithPassword extends Employee {
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface EmployeeAvailability {
|
||||
id: string;
|
||||
employeeId: string;
|
||||
planId: string;
|
||||
dayOfWeek: number; // 1=Monday, 7=Sunday
|
||||
timeSlotId: string;
|
||||
preferenceLevel: 1 | 2 | 3; // 1:preferred, 2:available, 3:unavailable
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
export interface ManagerAvailability {
|
||||
id: string;
|
||||
employeeId: string;
|
||||
planId: string;
|
||||
dayOfWeek: number; // 1=Monday, 7=Sunday
|
||||
timeSlotId: string;
|
||||
isAvailable: boolean; // Simple available/not available
|
||||
assignedBy: string; // Always self for manager
|
||||
}
|
||||
|
||||
export interface CreateAvailabilityRequest {
|
||||
planId: string;
|
||||
availabilities: Omit<EmployeeAvailability, 'id' | 'employeeId'>[];
|
||||
}
|
||||
|
||||
export interface UpdateAvailabilityRequest {
|
||||
planId: string;
|
||||
availabilities: Omit<EmployeeAvailability, 'id' | 'employeeId'>[];
|
||||
}
|
||||
|
||||
export interface ManagerSelfAssignmentRequest {
|
||||
planId: string;
|
||||
assignments: Omit<ManagerAvailability, 'id' | 'employeeId' | 'assignedBy'>[];
|
||||
}
|
||||
|
||||
export interface EmployeeWithAvailabilities extends Employee {
|
||||
availabilities: EmployeeAvailability[];
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
// backend/src/models/Shift.ts
|
||||
export interface Shift {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
isDefault: boolean;
|
||||
createdBy: string;
|
||||
createdAt: string;
|
||||
shifts: ShiftSlot[];
|
||||
}
|
||||
|
||||
export interface ShiftSlot {
|
||||
id: string;
|
||||
shiftId: string;
|
||||
dayOfWeek: number;
|
||||
name: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
requiredEmployees: number;
|
||||
color?: string;
|
||||
}
|
||||
|
||||
export interface CreateShiftRequest {
|
||||
name: string;
|
||||
description?: string;
|
||||
isDefault: boolean;
|
||||
shifts: Omit<ShiftSlot, 'id' | 'shiftId'>[];
|
||||
}
|
||||
|
||||
export interface UpdateShiftSlotRequest {
|
||||
name?: string;
|
||||
description?: string;
|
||||
isDefault?: boolean;
|
||||
shifts?: Omit<ShiftSlot, 'id' | 'shiftId'>[];
|
||||
}
|
||||
|
||||
export interface ShiftPlan {
|
||||
id: string;
|
||||
name: string;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
templateId?: string;
|
||||
shifts: AssignedShift[];
|
||||
status: 'draft' | 'published';
|
||||
createdBy: string;
|
||||
}
|
||||
|
||||
export interface AssignedShift {
|
||||
id: string;
|
||||
date: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
requiredEmployees: number;
|
||||
assignedEmployees: string[];
|
||||
}
|
||||
219
backend/src/models/ShiftPlan.ts
Normal file
219
backend/src/models/ShiftPlan.ts
Normal file
@@ -0,0 +1,219 @@
|
||||
// backend/src/models/ShiftPlan.ts
|
||||
export interface ShiftPlan {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
startDate?: string; // Optional for templates
|
||||
endDate?: string; // Optional for templates
|
||||
isTemplate: boolean;
|
||||
status: 'draft' | 'published' | 'archived' | 'template';
|
||||
createdBy: string;
|
||||
createdAt: string;
|
||||
timeSlots: TimeSlot[];
|
||||
shifts: Shift[];
|
||||
scheduledShifts?: ScheduledShift[]; // Only for non-template plans with dates
|
||||
}
|
||||
|
||||
export interface TimeSlot {
|
||||
id: string;
|
||||
planId: string;
|
||||
name: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export interface Shift {
|
||||
id: string;
|
||||
planId: string;
|
||||
timeSlotId: string;
|
||||
dayOfWeek: number; // 1=Monday, 7=Sunday
|
||||
requiredEmployees: number;
|
||||
color?: string;
|
||||
}
|
||||
|
||||
export interface ScheduledShift {
|
||||
id: string;
|
||||
planId: string;
|
||||
date: string;
|
||||
timeSlotId: string;
|
||||
requiredEmployees: number;
|
||||
assignedEmployees: string[]; // employee IDs
|
||||
}
|
||||
|
||||
export interface ShiftAssignment {
|
||||
id: string;
|
||||
scheduledShiftId: string;
|
||||
employeeId: string;
|
||||
assignmentStatus: 'assigned' | 'cancelled';
|
||||
assignedAt: string;
|
||||
assignedBy: string;
|
||||
}
|
||||
|
||||
export interface EmployeeAvailability {
|
||||
id: string;
|
||||
employeeId: string;
|
||||
planId: string;
|
||||
dayOfWeek: number;
|
||||
timeSlotId: string;
|
||||
preferenceLevel: 1 | 2 | 3; // 1:preferred, 2:available, 3:unavailable
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
// Request/Response DTOs
|
||||
export interface CreateShiftPlanRequest {
|
||||
name: string;
|
||||
description?: string;
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
isTemplate: boolean;
|
||||
timeSlots: Omit<TimeSlot, 'id' | 'planId'>[];
|
||||
shifts: Omit<Shift, 'id' | 'planId'>[];
|
||||
}
|
||||
|
||||
export interface UpdateShiftPlanRequest {
|
||||
name?: string;
|
||||
description?: string;
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
status?: 'draft' | 'published' | 'archived' | 'template';
|
||||
timeSlots?: Omit<TimeSlot, 'id' | 'planId'>[];
|
||||
shifts?: Omit<Shift, 'id' | 'planId'>[];
|
||||
}
|
||||
|
||||
export interface CreateShiftFromTemplateRequest {
|
||||
templatePlanId: string;
|
||||
name: string;
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export interface AssignEmployeeRequest {
|
||||
employeeId: string;
|
||||
scheduledShiftId: string;
|
||||
}
|
||||
|
||||
export interface UpdateAvailabilityRequest {
|
||||
planId: string;
|
||||
availabilities: Omit<EmployeeAvailability, 'id' | 'employeeId'>[];
|
||||
}
|
||||
|
||||
// Default time slots for ZEBRA (specific workplace)
|
||||
export const DEFAULT_ZEBRA_TIME_SLOTS: Omit<TimeSlot, 'id' | 'planId'>[] = [
|
||||
{
|
||||
name: 'Vormittag',
|
||||
startTime: '08:00',
|
||||
endTime: '12:00',
|
||||
description: 'Vormittagsschicht'
|
||||
},
|
||||
{
|
||||
name: 'Nachmittag',
|
||||
startTime: '11:30',
|
||||
endTime: '15:30',
|
||||
description: 'Nachmittagsschicht'
|
||||
},
|
||||
];
|
||||
|
||||
// Default time slots for general use
|
||||
export const DEFAULT_TIME_SLOTS: Omit<TimeSlot, 'id' | 'planId'>[] = [
|
||||
{
|
||||
name: 'Vormittag',
|
||||
startTime: '08:00',
|
||||
endTime: '12:00',
|
||||
description: 'Vormittagsschicht'
|
||||
},
|
||||
{
|
||||
name: 'Nachmittag',
|
||||
startTime: '11:30',
|
||||
endTime: '15:30',
|
||||
description: 'Nachmittagsschicht'
|
||||
},
|
||||
{
|
||||
name: 'Abend',
|
||||
startTime: '14:00',
|
||||
endTime: '18:00',
|
||||
description: 'Abendschicht'
|
||||
},
|
||||
];
|
||||
|
||||
// Helper functions
|
||||
export function validateRequiredEmployees(shift: Shift | ScheduledShift): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (shift.requiredEmployees < 1) {
|
||||
errors.push('Required employees must be at least 1');
|
||||
}
|
||||
|
||||
if (shift.requiredEmployees > 10) {
|
||||
errors.push('Required employees cannot exceed 10');
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
|
||||
export function isTemplate(plan: ShiftPlan): boolean {
|
||||
return plan.isTemplate || plan.status === 'template';
|
||||
}
|
||||
|
||||
export function hasDateRange(plan: ShiftPlan): boolean {
|
||||
return !isTemplate(plan) && !!plan.startDate && !!plan.endDate;
|
||||
}
|
||||
|
||||
export function validatePlanDates(plan: ShiftPlan): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (!isTemplate(plan)) {
|
||||
if (!plan.startDate) errors.push('Start date is required for non-template plans');
|
||||
if (!plan.endDate) errors.push('End date is required for non-template plans');
|
||||
if (plan.startDate && plan.endDate && plan.startDate > plan.endDate) {
|
||||
errors.push('Start date must be before end date');
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
// Type guards
|
||||
export function isScheduledShift(shift: Shift | ScheduledShift): shift is ScheduledShift {
|
||||
return 'date' in shift;
|
||||
}
|
||||
|
||||
// Template presets for quick setup
|
||||
// Default shifts for ZEBRA standard week template with variable required employees
|
||||
export const DEFAULT_ZEBRA_SHIFTS: Omit<Shift, 'id' | 'planId'>[] = [
|
||||
// Monday-Thursday: Morning + Afternoon
|
||||
...Array.from({ length: 4 }, (_, i) => i + 1).flatMap(day => [
|
||||
{ timeSlotId: 'morning', dayOfWeek: day, requiredEmployees: 2, color: '#3498db' },
|
||||
{ timeSlotId: 'afternoon', dayOfWeek: day, requiredEmployees: 2, color: '#e74c3c' }
|
||||
]),
|
||||
// Friday: Morning only
|
||||
{ timeSlotId: 'morning', dayOfWeek: 5, requiredEmployees: 2, color: '#3498db' }
|
||||
];
|
||||
|
||||
// Default shifts for general standard week template with variable required employees
|
||||
export const DEFAULT_SHIFTS: Omit<Shift, 'id' | 'planId'>[] = [
|
||||
// Monday-Friday: Morning + Afternoon + Evening
|
||||
...Array.from({ length: 5 }, (_, i) => i + 1).flatMap(day => [
|
||||
{ timeSlotId: 'morning', dayOfWeek: day, requiredEmployees: 2, color: '#3498db' },
|
||||
{ timeSlotId: 'afternoon', dayOfWeek: day, requiredEmployees: 2, color: '#e74c3c' },
|
||||
{ timeSlotId: 'evening', dayOfWeek: day, requiredEmployees: 1, color: '#2ecc71' } // Only 1 for evening
|
||||
])
|
||||
];
|
||||
|
||||
// Template presets for quick creation
|
||||
export const TEMPLATE_PRESETS = {
|
||||
ZEBRA_STANDARD: {
|
||||
name: 'ZEBRA Standardwoche',
|
||||
description: 'Standard Vorlage für ZEBRA: Mo-Do Vormittag+Nachmittag, Fr nur Vormittag',
|
||||
timeSlots: DEFAULT_ZEBRA_TIME_SLOTS,
|
||||
shifts: DEFAULT_ZEBRA_SHIFTS
|
||||
},
|
||||
GENERAL_STANDARD: {
|
||||
name: 'Standard Wochenplan',
|
||||
description: 'Standard Vorlage: Mo-Fr Vormittag+Nachmittag+Abend',
|
||||
timeSlots: DEFAULT_TIME_SLOTS,
|
||||
shifts: DEFAULT_SHIFTS
|
||||
}
|
||||
} as const;
|
||||
@@ -1,48 +0,0 @@
|
||||
// backend/src/models/ShiftTemplate.ts
|
||||
export interface TemplateShift {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
isDefault: boolean;
|
||||
createdBy: string;
|
||||
createdAt: string;
|
||||
shifts: TemplateShiftSlot[];
|
||||
}
|
||||
|
||||
export interface TemplateShiftSlot {
|
||||
id: string;
|
||||
templateId: string;
|
||||
dayOfWeek: number;
|
||||
timeSlot: TemplateShiftTimeSlot;
|
||||
requiredEmployees: number;
|
||||
color?: string;
|
||||
}
|
||||
|
||||
export interface TemplateShiftTimeSlot {
|
||||
id: string;
|
||||
name: string; // e.g., "Frühschicht", "Spätschicht"
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
}
|
||||
|
||||
export const DEFAULT_TIME_SLOTS: TemplateShiftTimeSlot[] = [
|
||||
{ id: 'morning', name: 'Vormittag', startTime: '08:00', endTime: '12:00' },
|
||||
{ id: 'afternoon', name: 'Nachmittag', startTime: '11:30', endTime: '15:30' },
|
||||
];
|
||||
|
||||
|
||||
export interface CreateShiftTemplateRequest {
|
||||
name: string;
|
||||
description?: string;
|
||||
isDefault: boolean;
|
||||
shifts: Omit<TemplateShiftSlot, 'id' | 'templateId'>[];
|
||||
timeSlots: TemplateShiftTimeSlot[];
|
||||
}
|
||||
|
||||
export interface UpdateShiftTemplateRequest {
|
||||
name?: string;
|
||||
description?: string;
|
||||
isDefault?: boolean;
|
||||
shifts?: Omit<TemplateShiftSlot, 'id' | 'templateId'>[];
|
||||
timeSlots?: TemplateShiftTimeSlot[];
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
// backend/src/models/User.ts
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
password: string; // gehashed
|
||||
name: string;
|
||||
role: 'admin' | 'instandhalter' | 'user';
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
export interface UserSession {
|
||||
userId: string;
|
||||
token: string;
|
||||
expiresAt: Date;
|
||||
}
|
||||
85
backend/src/models/defaults/employeeDefaults.ts
Normal file
85
backend/src/models/defaults/employeeDefaults.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
// backend/src/models/defaults/employeeDefaults.ts
|
||||
import { EmployeeAvailability, ManagerAvailability } from '../Employee.js';
|
||||
|
||||
// Default employee data for quick creation
|
||||
export const EMPLOYEE_DEFAULTS = {
|
||||
role: 'user' as const,
|
||||
employeeType: 'experienced' as const,
|
||||
contractType: 'small' as const,
|
||||
canWorkAlone: false,
|
||||
isActive: true
|
||||
};
|
||||
|
||||
// Manager-specific defaults
|
||||
export const MANAGER_DEFAULTS = {
|
||||
role: 'admin' as const,
|
||||
employeeType: 'manager' as const,
|
||||
contractType: 'large' as const, // Not really used but required by DB
|
||||
canWorkAlone: true,
|
||||
isActive: true
|
||||
};
|
||||
|
||||
// Contract type descriptions
|
||||
export const CONTRACT_TYPE_DESCRIPTIONS = {
|
||||
small: '1 Schicht pro Woche',
|
||||
large: '2 Schichten pro Woche',
|
||||
manager: 'Kein Vertragslimit - Immer MO und DI verfügbar'
|
||||
} as const;
|
||||
|
||||
// Employee type descriptions
|
||||
export const EMPLOYEE_TYPE_DESCRIPTIONS = {
|
||||
manager: 'Chef - Immer MO und DI in beiden Schichten, kann eigene Schichten festlegen',
|
||||
trainee: 'Neuling - Darf nicht alleine sein, benötigt erfahrene Begleitung',
|
||||
experienced: 'Erfahren - Kann alleine arbeiten (wenn freigegeben)'
|
||||
} as const;
|
||||
|
||||
// Availability preference descriptions
|
||||
export const AVAILABILITY_PREFERENCES = {
|
||||
1: { label: 'Bevorzugt', color: '#10b981', description: 'Möchte diese Schicht arbeiten' },
|
||||
2: { label: 'Möglich', color: '#f59e0b', description: 'Kann diese Schicht arbeiten' },
|
||||
3: { label: 'Nicht möglich', color: '#ef4444', description: 'Kann diese Schicht nicht arbeiten' }
|
||||
} as const;
|
||||
|
||||
// Default availability for new employees (all shifts unavailable as level 3)
|
||||
export function createDefaultAvailabilities(employeeId: string, planId: string, timeSlotIds: string[]): Omit<EmployeeAvailability, 'id'>[] {
|
||||
const availabilities: Omit<EmployeeAvailability, 'id'>[] = [];
|
||||
|
||||
// Monday to Friday (1-5)
|
||||
for (let day = 1; day <= 5; day++) {
|
||||
for (const timeSlotId of timeSlotIds) {
|
||||
availabilities.push({
|
||||
employeeId,
|
||||
planId,
|
||||
dayOfWeek: day,
|
||||
timeSlotId,
|
||||
preferenceLevel: 3 // Default to "unavailable" - employees must explicitly set availability
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return availabilities;
|
||||
}
|
||||
|
||||
// Create complete manager availability for all days (default: only Mon-Tue available)
|
||||
export function createManagerDefaultSchedule(managerId: string, planId: string, timeSlotIds: string[]): Omit<ManagerAvailability, 'id'>[] {
|
||||
const assignments: Omit<ManagerAvailability, 'id'>[] = [];
|
||||
|
||||
// Monday to Sunday (1-7)
|
||||
for (let dayOfWeek = 1; dayOfWeek <= 7; dayOfWeek++) {
|
||||
for (const timeSlotId of timeSlotIds) {
|
||||
// Default: available only on Monday (1) and Tuesday (2)
|
||||
const isAvailable = dayOfWeek === 1 || dayOfWeek === 2;
|
||||
|
||||
assignments.push({
|
||||
employeeId: managerId,
|
||||
planId,
|
||||
dayOfWeek,
|
||||
timeSlotId,
|
||||
isAvailable,
|
||||
assignedBy: managerId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return assignments;
|
||||
}
|
||||
3
backend/src/models/defaults/index.ts
Normal file
3
backend/src/models/defaults/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
// backend/src/models/defaults/index.ts
|
||||
export * from './employeeDefaults.js';
|
||||
export * from './shiftPlanDefaults.js';
|
||||
146
backend/src/models/defaults/shiftPlanDefaults.ts
Normal file
146
backend/src/models/defaults/shiftPlanDefaults.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
// backend/src/models/defaults/shiftPlanDefaults.ts
|
||||
import { TimeSlot, Shift } from '../ShiftPlan.js';
|
||||
|
||||
// Default time slots for ZEBRA (specific workplace)
|
||||
export const DEFAULT_ZEBRA_TIME_SLOTS: Omit<TimeSlot, 'id' | 'planId'>[] = [
|
||||
{
|
||||
name: 'Vormittag',
|
||||
startTime: '08:00',
|
||||
endTime: '12:00',
|
||||
description: 'Vormittagsschicht'
|
||||
},
|
||||
{
|
||||
name: 'Nachmittag',
|
||||
startTime: '11:30',
|
||||
endTime: '15:30',
|
||||
description: 'Nachmittagsschicht'
|
||||
},
|
||||
];
|
||||
|
||||
// Default time slots for general use
|
||||
export const DEFAULT_TIME_SLOTS: Omit<TimeSlot, 'id' | 'planId'>[] = [
|
||||
{
|
||||
name: 'Vormittag',
|
||||
startTime: '08:00',
|
||||
endTime: '12:00',
|
||||
description: 'Vormittagsschicht'
|
||||
},
|
||||
{
|
||||
name: 'Nachmittag',
|
||||
startTime: '11:30',
|
||||
endTime: '15:30',
|
||||
description: 'Nachmittagsschicht'
|
||||
},
|
||||
{
|
||||
name: 'Abend',
|
||||
startTime: '14:00',
|
||||
endTime: '18:00',
|
||||
description: 'Abendschicht'
|
||||
},
|
||||
];
|
||||
|
||||
// Default shifts for ZEBRA standard week template with variable required employees
|
||||
export const DEFAULT_ZEBRA_SHIFTS: Omit<Shift, 'id' | 'planId'>[] = [
|
||||
// Monday-Thursday: Morning + Afternoon
|
||||
...Array.from({ length: 4 }, (_, i) => i + 1).flatMap(day => [
|
||||
{ timeSlotId: 'morning', dayOfWeek: day, requiredEmployees: 2, color: '#3498db' },
|
||||
{ timeSlotId: 'afternoon', dayOfWeek: day, requiredEmployees: 2, color: '#e74c3c' }
|
||||
]),
|
||||
// Friday: Morning only
|
||||
{ timeSlotId: 'morning', dayOfWeek: 5, requiredEmployees: 2, color: '#3498db' }
|
||||
];
|
||||
|
||||
// Default shifts for general standard week template with variable required employees
|
||||
export const DEFAULT_SHIFTS: Omit<Shift, 'id' | 'planId'>[] = [
|
||||
// Monday-Friday: Morning + Afternoon + Evening
|
||||
...Array.from({ length: 5 }, (_, i) => i + 1).flatMap(day => [
|
||||
{ timeSlotId: 'morning', dayOfWeek: day, requiredEmployees: 2, color: '#3498db' },
|
||||
{ timeSlotId: 'afternoon', dayOfWeek: day, requiredEmployees: 2, color: '#e74c3c' },
|
||||
{ timeSlotId: 'evening', dayOfWeek: day, requiredEmployees: 1, color: '#2ecc71' } // Only 1 for evening
|
||||
])
|
||||
];
|
||||
|
||||
// Template presets for quick creation
|
||||
export const TEMPLATE_PRESETS = {
|
||||
ZEBRA_STANDARD: {
|
||||
name: 'ZEBRA Standardwoche',
|
||||
description: 'Standard Vorlage für ZEBRA: Mo-Do Vormittag+Nachmittag, Fr nur Vormittag',
|
||||
timeSlots: DEFAULT_ZEBRA_TIME_SLOTS,
|
||||
shifts: DEFAULT_ZEBRA_SHIFTS
|
||||
},
|
||||
ZEBRA_MINIMAL: {
|
||||
name: 'ZEBRA Minimal',
|
||||
description: 'ZEBRA mit minimaler Besetzung',
|
||||
timeSlots: DEFAULT_ZEBRA_TIME_SLOTS,
|
||||
shifts: [
|
||||
...Array.from({ length: 5 }, (_, i) => i + 1).flatMap(day => [
|
||||
{ timeSlotId: 'morning', dayOfWeek: day, requiredEmployees: 1, color: '#3498db' },
|
||||
{ timeSlotId: 'afternoon', dayOfWeek: day, requiredEmployees: 1, color: '#e74c3c' }
|
||||
])
|
||||
]
|
||||
},
|
||||
ZEBRA_FULL: {
|
||||
name: 'ZEBRA Vollbesetzung',
|
||||
description: 'ZEBRA mit voller Besetzung',
|
||||
timeSlots: DEFAULT_ZEBRA_TIME_SLOTS,
|
||||
shifts: [
|
||||
...Array.from({ length: 5 }, (_, i) => i + 1).flatMap(day => [
|
||||
{ timeSlotId: 'morning', dayOfWeek: day, requiredEmployees: 3, color: '#3498db' },
|
||||
{ timeSlotId: 'afternoon', dayOfWeek: day, requiredEmployees: 3, color: '#e74c3c' }
|
||||
])
|
||||
]
|
||||
},
|
||||
GENERAL_STANDARD: {
|
||||
name: 'Standard Wochenplan',
|
||||
description: 'Standard Vorlage: Mo-Fr Vormittag+Nachmittag+Abend',
|
||||
timeSlots: DEFAULT_TIME_SLOTS,
|
||||
shifts: DEFAULT_SHIFTS
|
||||
},
|
||||
ZEBRA_PART_TIME: {
|
||||
name: 'ZEBRA Teilzeit',
|
||||
description: 'ZEBRA Vorlage mit reduzierten Schichten',
|
||||
timeSlots: DEFAULT_ZEBRA_TIME_SLOTS,
|
||||
shifts: [
|
||||
// Monday-Thursday: Morning only
|
||||
...Array.from({ length: 4 }, (_, i) => i + 1).map(day => ({
|
||||
timeSlotId: 'morning', dayOfWeek: day, requiredEmployees: 1, color: '#3498db'
|
||||
}))
|
||||
]
|
||||
}
|
||||
} as const;
|
||||
|
||||
// Helper function to create plan from preset
|
||||
export function createPlanFromPreset(
|
||||
presetName: keyof typeof TEMPLATE_PRESETS,
|
||||
isTemplate: boolean = true,
|
||||
startDate?: string,
|
||||
endDate?: string
|
||||
) {
|
||||
const preset = TEMPLATE_PRESETS[presetName];
|
||||
return {
|
||||
name: preset.name,
|
||||
description: preset.description,
|
||||
startDate,
|
||||
endDate,
|
||||
isTemplate,
|
||||
timeSlots: preset.timeSlots,
|
||||
shifts: preset.shifts
|
||||
};
|
||||
}
|
||||
|
||||
// Color schemes for shifts
|
||||
export const SHIFT_COLORS = {
|
||||
morning: '#3498db', // Blue
|
||||
afternoon: '#e74c3c', // Red
|
||||
evening: '#2ecc71', // Green
|
||||
night: '#9b59b6', // Purple
|
||||
default: '#95a5a6' // Gray
|
||||
} as const;
|
||||
|
||||
// Status descriptions
|
||||
export const PLAN_STATUS_DESCRIPTIONS = {
|
||||
draft: 'Entwurf - Kann bearbeitet werden',
|
||||
published: 'Veröffentlicht - Für alle sichtbar',
|
||||
archived: 'Archiviert - Nur noch lesbar',
|
||||
template: 'Vorlage - Kann für neue Pläne verwendet werden'
|
||||
} as const;
|
||||
128
backend/src/models/helpers/employeeHelpers.ts
Normal file
128
backend/src/models/helpers/employeeHelpers.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
// backend/src/models/helpers/employeeHelpers.ts
|
||||
import { Employee, CreateEmployeeRequest, EmployeeAvailability, ManagerAvailability } from '../Employee.js';
|
||||
|
||||
// Validation helpers
|
||||
export function validateEmployee(employee: CreateEmployeeRequest): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (!employee.email || !employee.email.includes('@')) {
|
||||
errors.push('Valid email is required');
|
||||
}
|
||||
|
||||
if (!employee.password || employee.password.length < 6) {
|
||||
errors.push('Password must be at least 6 characters long');
|
||||
}
|
||||
|
||||
if (!employee.name || employee.name.trim().length < 2) {
|
||||
errors.push('Name is required and must be at least 2 characters long');
|
||||
}
|
||||
|
||||
if (!employee.contractType) {
|
||||
errors.push('Contract type is required');
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
export function validateAvailability(availability: Omit<EmployeeAvailability, 'id' | 'employeeId'>): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (availability.dayOfWeek < 1 || availability.dayOfWeek > 7) {
|
||||
errors.push('Day of week must be between 1 and 7');
|
||||
}
|
||||
|
||||
if (![1, 2, 3].includes(availability.preferenceLevel)) {
|
||||
errors.push('Preference level must be 1, 2, or 3');
|
||||
}
|
||||
|
||||
if (!availability.timeSlotId) {
|
||||
errors.push('Time slot ID is required');
|
||||
}
|
||||
|
||||
if (!availability.planId) {
|
||||
errors.push('Plan ID is required');
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
// Employee type guards
|
||||
export function isManager(employee: Employee): boolean {
|
||||
return employee.employeeType === 'manager';
|
||||
}
|
||||
|
||||
export function isTrainee(employee: Employee): boolean {
|
||||
return employee.employeeType === 'trainee';
|
||||
}
|
||||
|
||||
export function isExperienced(employee: Employee): boolean {
|
||||
return employee.employeeType === 'experienced';
|
||||
}
|
||||
|
||||
export function isAdmin(employee: Employee): boolean {
|
||||
return employee.role === 'admin';
|
||||
}
|
||||
|
||||
// Business logic helpers
|
||||
export function canEmployeeWorkAlone(employee: Employee): boolean {
|
||||
return employee.canWorkAlone && employee.employeeType === 'experienced';
|
||||
}
|
||||
|
||||
export function getEmployeeWorkHours(employee: Employee): number {
|
||||
// Manager: no contract limit, others: small=1, large=2 shifts per week
|
||||
return isManager(employee) ? 999 : (employee.contractType === 'small' ? 1 : 2);
|
||||
}
|
||||
|
||||
export function requiresAvailabilityPreference(employee: Employee): boolean {
|
||||
// Only non-managers use the preference system
|
||||
return !isManager(employee);
|
||||
}
|
||||
|
||||
export function canSetOwnAvailability(employee: Employee): boolean {
|
||||
// Manager can set their own specific shift assignments
|
||||
return isManager(employee);
|
||||
}
|
||||
|
||||
// Manager availability helpers
|
||||
export function isManagerAvailable(
|
||||
managerAssignments: ManagerAvailability[],
|
||||
dayOfWeek: number,
|
||||
timeSlotId: string
|
||||
): boolean {
|
||||
const assignment = managerAssignments.find(assignment =>
|
||||
assignment.dayOfWeek === dayOfWeek &&
|
||||
assignment.timeSlotId === timeSlotId
|
||||
);
|
||||
|
||||
return assignment ? assignment.isAvailable : false;
|
||||
}
|
||||
|
||||
export function getManagerAvailableShifts(managerAssignments: ManagerAvailability[]): ManagerAvailability[] {
|
||||
return managerAssignments.filter(assignment => assignment.isAvailable);
|
||||
}
|
||||
|
||||
export function updateManagerAvailability(
|
||||
assignments: ManagerAvailability[],
|
||||
dayOfWeek: number,
|
||||
timeSlotId: string,
|
||||
isAvailable: boolean
|
||||
): ManagerAvailability[] {
|
||||
return assignments.map(assignment =>
|
||||
assignment.dayOfWeek === dayOfWeek && assignment.timeSlotId === timeSlotId
|
||||
? { ...assignment, isAvailable }
|
||||
: assignment
|
||||
);
|
||||
}
|
||||
|
||||
export function validateManagerMinimumAvailability(managerAssignments: ManagerAvailability[]): boolean {
|
||||
const requiredShifts = [
|
||||
{ dayOfWeek: 1, timeSlotId: 'morning' },
|
||||
{ dayOfWeek: 1, timeSlotId: 'afternoon' },
|
||||
{ dayOfWeek: 2, timeSlotId: 'morning' },
|
||||
{ dayOfWeek: 2, timeSlotId: 'afternoon' }
|
||||
];
|
||||
|
||||
return requiredShifts.every(required =>
|
||||
isManagerAvailable(managerAssignments, required.dayOfWeek, required.timeSlotId)
|
||||
);
|
||||
}
|
||||
3
backend/src/models/helpers/index.ts
Normal file
3
backend/src/models/helpers/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
// backend/src/models/helpers/index.ts
|
||||
export * from './employeeHelpers.js';
|
||||
export * from './shiftPlanHelpers.js';
|
||||
118
backend/src/models/helpers/shiftPlanHelpers.ts
Normal file
118
backend/src/models/helpers/shiftPlanHelpers.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
// backend/src/models/helpers/shiftPlanHelpers.ts
|
||||
import { ShiftPlan, Shift, ScheduledShift, TimeSlot } from '../ShiftPlan.js';
|
||||
|
||||
// Validation helpers
|
||||
export function validateRequiredEmployees(shift: Shift | ScheduledShift): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (shift.requiredEmployees < 1) {
|
||||
errors.push('Required employees must be at least 1');
|
||||
}
|
||||
|
||||
if (shift.requiredEmployees > 10) {
|
||||
errors.push('Required employees cannot exceed 10');
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
export function isTemplate(plan: ShiftPlan): boolean {
|
||||
return plan.isTemplate || plan.status === 'template';
|
||||
}
|
||||
|
||||
export function hasDateRange(plan: ShiftPlan): boolean {
|
||||
return !isTemplate(plan) && !!plan.startDate && !!plan.endDate;
|
||||
}
|
||||
|
||||
export function validatePlanDates(plan: ShiftPlan): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (!isTemplate(plan)) {
|
||||
if (!plan.startDate) errors.push('Start date is required for non-template plans');
|
||||
if (!plan.endDate) errors.push('End date is required for non-template plans');
|
||||
if (plan.startDate && plan.endDate && plan.startDate > plan.endDate) {
|
||||
errors.push('Start date must be before end date');
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
export function validateTimeSlot(timeSlot: { startTime: string; endTime: string }): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (!timeSlot.startTime || !timeSlot.endTime) {
|
||||
errors.push('Start time and end time are required');
|
||||
return errors;
|
||||
}
|
||||
|
||||
const start = new Date(`2000-01-01T${timeSlot.startTime}`);
|
||||
const end = new Date(`2000-01-01T${timeSlot.endTime}`);
|
||||
|
||||
if (start >= end) {
|
||||
errors.push('Start time must be before end time');
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
// Type guards
|
||||
export function isScheduledShift(shift: Shift | ScheduledShift): shift is ScheduledShift {
|
||||
return 'date' in shift;
|
||||
}
|
||||
|
||||
export function isTemplateShift(shift: Shift | ScheduledShift): shift is Shift {
|
||||
return 'dayOfWeek' in shift && !('date' in shift);
|
||||
}
|
||||
|
||||
// Business logic helpers
|
||||
export function getShiftsForDay(plan: ShiftPlan, dayOfWeek: number): Shift[] {
|
||||
return plan.shifts.filter(shift => shift.dayOfWeek === dayOfWeek);
|
||||
}
|
||||
|
||||
export function getTimeSlotById(plan: ShiftPlan, timeSlotId: string): TimeSlot | undefined {
|
||||
return plan.timeSlots.find(slot => slot.id === timeSlotId);
|
||||
}
|
||||
|
||||
export function calculateTotalRequiredEmployees(plan: ShiftPlan): number {
|
||||
return plan.shifts.reduce((total, shift) => total + shift.requiredEmployees, 0);
|
||||
}
|
||||
|
||||
export function getScheduledShiftByDateAndTime(
|
||||
plan: ShiftPlan,
|
||||
date: string,
|
||||
timeSlotId: string
|
||||
): ScheduledShift | undefined {
|
||||
return plan.scheduledShifts?.find(shift =>
|
||||
shift.date === date && shift.timeSlotId === timeSlotId
|
||||
);
|
||||
}
|
||||
|
||||
export function canPublishPlan(plan: ShiftPlan): { canPublish: boolean; errors: string[] } {
|
||||
const errors: string[] = [];
|
||||
|
||||
if (!hasDateRange(plan)) {
|
||||
errors.push('Plan must have a date range to be published');
|
||||
}
|
||||
|
||||
if (plan.shifts.length === 0) {
|
||||
errors.push('Plan must have at least one shift');
|
||||
}
|
||||
|
||||
if (plan.timeSlots.length === 0) {
|
||||
errors.push('Plan must have at least one time slot');
|
||||
}
|
||||
|
||||
// Validate all shifts
|
||||
plan.shifts.forEach((shift, index) => {
|
||||
const shiftErrors = validateRequiredEmployees(shift);
|
||||
if (shiftErrors.length > 0) {
|
||||
errors.push(`Shift ${index + 1}: ${shiftErrors.join(', ')}`);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
canPublish: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user