mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-11-30 22:45:46 +01:00
updated every file for database changes; starting scheduling debugging
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
// backend/src/models/helpers/employeeHelpers.ts
|
||||
import { Employee, CreateEmployeeRequest, EmployeeAvailability } from '../Employee.js';
|
||||
|
||||
// Email generation function (same as in controllers)
|
||||
// Email generation function
|
||||
function generateEmail(firstname: string, lastname: string): string {
|
||||
// Convert German umlauts to their expanded forms
|
||||
const convertUmlauts = (str: string): string => {
|
||||
return str
|
||||
.toLowerCase()
|
||||
@@ -13,19 +12,16 @@ function generateEmail(firstname: string, lastname: string): string {
|
||||
.replace(/ß/g, 'ss');
|
||||
};
|
||||
|
||||
// Remove any remaining special characters and convert to lowercase
|
||||
const cleanFirstname = convertUmlauts(firstname).replace(/[^a-z0-9]/g, '');
|
||||
const cleanLastname = convertUmlauts(lastname).replace(/[^a-z0-9]/g, '');
|
||||
|
||||
return `${cleanFirstname}.${cleanLastname}@sp.de`;
|
||||
}
|
||||
|
||||
// UPDATED: Validation for new employee model
|
||||
// UPDATED: Validation for new employee model with employee types
|
||||
export function validateEmployeeData(employee: CreateEmployeeRequest): string[] {
|
||||
const errors: string[] = [];
|
||||
|
||||
// Email is now auto-generated, so no email validation needed
|
||||
|
||||
if (employee.password?.length < 6) {
|
||||
errors.push('Password must be at least 6 characters long');
|
||||
}
|
||||
@@ -38,24 +34,70 @@ export function validateEmployeeData(employee: CreateEmployeeRequest): string[]
|
||||
errors.push('Last name is required and must be at least 2 characters long');
|
||||
}
|
||||
|
||||
// Validate employee type
|
||||
const validEmployeeTypes = ['manager', 'personell', 'apprentice', 'guest'];
|
||||
if (!employee.employeeType || !validEmployeeTypes.includes(employee.employeeType)) {
|
||||
errors.push(`Employee type must be one of: ${validEmployeeTypes.join(', ')}`);
|
||||
}
|
||||
|
||||
// Validate contract type based on employee type
|
||||
if (employee.employeeType !== 'guest') {
|
||||
// Internal types require contract type
|
||||
if (!employee.contractType) {
|
||||
errors.push(`Contract type is required for employee type: ${employee.employeeType}`);
|
||||
} else {
|
||||
const validContractTypes = ['small', 'large', 'flexible'];
|
||||
if (!validContractTypes.includes(employee.contractType)) {
|
||||
errors.push(`Contract type must be one of: ${validContractTypes.join(', ')}`);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// External types (guest) should not have contract type
|
||||
if (employee.contractType) {
|
||||
errors.push('Contract type is not allowed for guest employees');
|
||||
}
|
||||
}
|
||||
|
||||
// Validate isTrainee - only applicable for personell type
|
||||
if (employee.isTrainee && employee.employeeType !== 'personell') {
|
||||
errors.push('isTrainee is only allowed for personell employee type');
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
// Generate email for employee (new helper function)
|
||||
// Generate email for employee
|
||||
export function generateEmployeeEmail(firstname: string, lastname: string): string {
|
||||
return generateEmail(firstname, lastname);
|
||||
}
|
||||
|
||||
// Simplified business logic helpers
|
||||
// UPDATED: Business logic helpers for new employee types
|
||||
export const isManager = (employee: Employee): boolean =>
|
||||
employee.employeeType === 'manager';
|
||||
|
||||
export const isPersonell = (employee: Employee): boolean =>
|
||||
employee.employeeType === 'personell';
|
||||
|
||||
export const isApprentice = (employee: Employee): boolean =>
|
||||
employee.employeeType === 'apprentice';
|
||||
|
||||
export const isGuest = (employee: Employee): boolean =>
|
||||
employee.employeeType === 'guest';
|
||||
|
||||
export const isInternal = (employee: Employee): boolean =>
|
||||
['manager', 'personell', 'apprentice'].includes(employee.employeeType);
|
||||
|
||||
export const isExternal = (employee: Employee): boolean =>
|
||||
employee.employeeType === 'guest';
|
||||
|
||||
// UPDATED: Trainee logic - now based on isTrainee field for personell type
|
||||
export const isTrainee = (employee: Employee): boolean =>
|
||||
employee.employeeType === 'trainee';
|
||||
employee.employeeType === 'personell' && employee.isTrainee;
|
||||
|
||||
export const isExperienced = (employee: Employee): boolean =>
|
||||
employee.employeeType === 'experienced';
|
||||
employee.employeeType === 'personell' && !employee.isTrainee;
|
||||
|
||||
// Role-based helpers
|
||||
export const isAdmin = (employee: Employee): boolean =>
|
||||
employee.roles?.includes('admin') || false;
|
||||
|
||||
@@ -65,13 +107,11 @@ export const isMaintenance = (employee: Employee): boolean =>
|
||||
export const isUser = (employee: Employee): boolean =>
|
||||
employee.roles?.includes('user') || false;
|
||||
|
||||
// UPDATED: Work alone permission - managers and experienced personell can work alone
|
||||
export const canEmployeeWorkAlone = (employee: Employee): boolean =>
|
||||
employee.canWorkAlone && isExperienced(employee);
|
||||
employee.canWorkAlone && (isManager(employee) || isExperienced(employee));
|
||||
|
||||
export const getEmployeeWorkHours = (employee: Employee): number =>
|
||||
isManager(employee) ? 999 : (employee.contractType === 'small' ? 1 : 2);
|
||||
|
||||
// New helper for full name display
|
||||
// Helper for full name display
|
||||
export const getFullName = (employee: { firstname: string; lastname: string }): string =>
|
||||
`${employee.firstname} ${employee.lastname}`;
|
||||
|
||||
@@ -92,4 +132,14 @@ export function validateAvailabilityData(availability: Omit<EmployeeAvailability
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
}
|
||||
|
||||
// UPDATED: Helper to get employee type category
|
||||
export const getEmployeeCategory = (employee: Employee): 'internal' | 'external' => {
|
||||
return isInternal(employee) ? 'internal' : 'external';
|
||||
};
|
||||
|
||||
// Helper to check if employee requires contract type
|
||||
export const requiresContractType = (employee: Employee): boolean => {
|
||||
return isInternal(employee);
|
||||
};
|
||||
Reference in New Issue
Block a user