Files
Schichtenplaner/backend/src/controllers/setupController.ts
2025-10-20 01:50:12 +02:00

133 lines
4.2 KiB
TypeScript

// backend/src/controllers/setupController.ts
import { Request, Response } from 'express';
import bcrypt from 'bcrypt';
import { v4 as uuidv4 } from 'uuid';
import { randomUUID } from 'crypto';
import { db } from '../services/databaseService.js';
// Add the same 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()
.replace(/ü/g, 'ue')
.replace(/ö/g, 'oe')
.replace(/ä/g, 'ae')
.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`;
}
export const checkSetupStatus = async (req: Request, res: Response): Promise<void> => {
try {
const adminExists = await db.get<{ 'COUNT(*)': number }>(
'SELECT COUNT(*) FROM employees WHERE role = ? AND is_active = 1',
['admin']
);
console.log('Admin exists check:', adminExists);
const needsSetup = !adminExists || adminExists['COUNT(*)'] === 0;
res.json({
needsSetup: needsSetup
});
} catch (error) {
console.error('Error checking setup status:', error);
res.status(500).json({
error: 'Internal server error during setup check'
});
}
};
export const setupAdmin = async (req: Request, res: Response): Promise<void> => {
try {
// Check if admin already exists
const adminExists = await db.get<{ 'COUNT(*)': number }>(
'SELECT COUNT(*) FROM employees WHERE role = ? AND is_active = 1',
['admin']
);
console.log('🔍 Admin exists check:', adminExists);
if (adminExists && adminExists['COUNT(*)'] > 0) {
console.log('❌ Admin already exists');
res.status(400).json({ error: 'Admin existiert bereits' });
return;
}
const { password, firstname, lastname } = req.body; // Changed from name to firstname/lastname
console.log('👤 Creating admin with data:', { firstname, lastname });
// Validation - updated for firstname/lastname
if (!password || !firstname || !lastname) {
res.status(400).json({ error: 'Passwort, Vorname und Nachname sind erforderlich' });
return;
}
// Password length validation
if (password.length < 6) {
res.status(400).json({ error: 'Das Passwort muss mindestens 6 Zeichen lang sein' });
return;
}
// Generate email automatically using the same pattern
const email = generateEmail(firstname, lastname);
console.log('📧 Generated admin email:', email);
// Hash password
const hashedPassword = await bcrypt.hash(password, 10);
const adminId = randomUUID();
console.log('📝 Inserting admin user with ID:', adminId);
// Start transaction for the entire setup process
await db.run('BEGIN TRANSACTION');
try {
// Create admin user with generated email
await db.run(
`INSERT INTO employees (id, email, password, firstname, lastname, role, employee_type, contract_type, can_work_alone, is_active)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[adminId, email, hashedPassword, firstname, lastname, 'admin', 'manager', 'large', true, 1]
);
console.log('✅ Admin user created successfully with email:', email);
// Commit the entire setup transaction
await db.run('COMMIT');
console.log('✅ Setup completed successfully');
res.status(201).json({
success: true,
message: 'Admin erfolgreich erstellt',
email: email,
firstname: firstname,
lastname: lastname
});
} catch (dbError) {
await db.run('ROLLBACK');
console.error('❌ Database error during admin creation:', dbError);
res.status(500).json({
error: 'Fehler beim Erstellen des Admin-Accounts'
});
}
} catch (error) {
console.error('❌ Error in setup:', error);
if (!res.headersSent) {
res.status(500).json({
error: 'Ein unerwarteter Fehler ist aufgetreten'
});
}
}
};