mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-12-01 06:55:45 +01:00
added admin setup
This commit is contained in:
130
backend/src/scripts/initializeDatabase.ts
Normal file
130
backend/src/scripts/initializeDatabase.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { db } from '../services/databaseService.js';
|
||||
import { setupDefaultTemplate } from './setupDefaultTemplate.js';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
export async function initializeDatabase(): Promise<void> {
|
||||
const schemaPath = path.join(__dirname, '../database/schema.sql');
|
||||
const schema = fs.readFileSync(schemaPath, 'utf8');
|
||||
|
||||
try {
|
||||
console.log('Starting database initialization...');
|
||||
|
||||
// Get list of existing tables
|
||||
interface TableInfo {
|
||||
name: string;
|
||||
}
|
||||
|
||||
try {
|
||||
const existingTables = await db.all<TableInfo>(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'"
|
||||
);
|
||||
|
||||
console.log('Existing tables found:', existingTables.map(t => t.name).join(', ') || 'none');
|
||||
|
||||
// Drop existing tables in reverse order of dependencies
|
||||
const tablesToDrop = [
|
||||
'employee_availabilities',
|
||||
'assigned_shifts',
|
||||
'shift_plans',
|
||||
'template_shifts',
|
||||
'shift_templates',
|
||||
'users'
|
||||
];
|
||||
|
||||
for (const table of tablesToDrop) {
|
||||
if (existingTables.some(t => t.name === table)) {
|
||||
console.log(`Dropping table: ${table}`);
|
||||
await db.run(`DROP TABLE IF EXISTS ${table}`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error checking/dropping existing tables:', error);
|
||||
// Continue with schema creation even if table dropping fails
|
||||
}
|
||||
|
||||
// Execute schema creation in a transaction
|
||||
await db.run('BEGIN EXCLUSIVE TRANSACTION');
|
||||
|
||||
// Execute each statement separately for better error reporting
|
||||
const statements = schema
|
||||
.split(';')
|
||||
.map(stmt => stmt.trim())
|
||||
.filter(stmt => stmt.length > 0)
|
||||
.map(stmt => {
|
||||
// Remove any single-line comments
|
||||
return stmt.split('\n')
|
||||
.filter(line => !line.trim().startsWith('--'))
|
||||
.join('\n')
|
||||
.trim();
|
||||
})
|
||||
.filter(stmt => stmt.length > 0);
|
||||
|
||||
for (const statement of statements) {
|
||||
try {
|
||||
console.log('Executing statement:', statement.substring(0, 50) + '...');
|
||||
await db.run(statement);
|
||||
} catch (error) {
|
||||
console.error('Failed SQL statement:', statement);
|
||||
console.error('Error details:', error);
|
||||
await db.run('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
await db.run('COMMIT');
|
||||
console.log('✅ Datenbankschema erfolgreich initialisiert');
|
||||
|
||||
// Give a small delay to ensure all transactions are properly closed
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
|
||||
// Create default template
|
||||
await setupDefaultTemplate();
|
||||
} catch (error) {
|
||||
console.error('Fehler bei der Datenbankinitialisierung:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function createAdminUser(): Promise<void> {
|
||||
try {
|
||||
await db.run('BEGIN TRANSACTION');
|
||||
|
||||
try {
|
||||
// Erstelle Admin-Benutzer, wenn noch keiner existiert
|
||||
const admin = await db.get('SELECT id FROM users WHERE role = ?', ['admin']);
|
||||
|
||||
if (!admin) {
|
||||
await db.run(
|
||||
`INSERT INTO users (id, email, password, name, role, phone, department, is_active)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[
|
||||
'admin-' + Math.random().toString(36).substring(2),
|
||||
'admin@schichtplan.de',
|
||||
'admin123',
|
||||
'Administrator',
|
||||
'admin',
|
||||
'+49 123 456789',
|
||||
'IT',
|
||||
true
|
||||
]
|
||||
);
|
||||
console.log('✅ Admin-Benutzer erstellt');
|
||||
} else {
|
||||
console.log('ℹ️ Admin-Benutzer existiert bereits');
|
||||
}
|
||||
|
||||
await db.run('COMMIT');
|
||||
} catch (error) {
|
||||
await db.run('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Erstellen des Admin-Benutzers:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
98
backend/src/scripts/setupDefaultTemplate.ts
Normal file
98
backend/src/scripts/setupDefaultTemplate.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
// backend/src/scripts/setupDefaultTemplate.ts
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { db } from '../services/databaseService.js';
|
||||
|
||||
interface AdminUser {
|
||||
id: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the default shift template if it doesn't exist
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
export async function setupDefaultTemplate(): Promise<void> {
|
||||
try {
|
||||
// Prüfen ob bereits eine Standard-Vorlage existiert
|
||||
const existingDefault = await db.get(
|
||||
'SELECT * FROM shift_templates WHERE is_default = 1'
|
||||
);
|
||||
|
||||
if (existingDefault) {
|
||||
console.log('Standard-Vorlage existiert bereits');
|
||||
return;
|
||||
}
|
||||
|
||||
// Admin-Benutzer für die Standard-Vorlage finden
|
||||
const adminUser = await db.get<AdminUser>(
|
||||
'SELECT id FROM users WHERE role = ?',
|
||||
['admin']
|
||||
);
|
||||
|
||||
if (!adminUser) {
|
||||
console.log('Kein Admin-Benutzer gefunden. Standard-Vorlage kann nicht erstellt werden.');
|
||||
return;
|
||||
}
|
||||
|
||||
const templateId = uuidv4();
|
||||
|
||||
// Transaktion starten
|
||||
await db.run('BEGIN TRANSACTION');
|
||||
|
||||
try {
|
||||
// Standard-Vorlage erstellen
|
||||
await db.run(
|
||||
`INSERT INTO shift_templates (id, name, description, is_default, created_by)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[
|
||||
templateId,
|
||||
'Standard Wochenplan',
|
||||
'Mo-Do: Vormittags- und Nachmittagsschicht, Fr: nur Vormittagsschicht',
|
||||
1,
|
||||
adminUser.id
|
||||
]
|
||||
);
|
||||
|
||||
console.log('Standard-Vorlage erstellt:', templateId);
|
||||
|
||||
// Vormittagsschicht Mo-Do
|
||||
for (let day = 1; day <= 4; day++) {
|
||||
await db.run(
|
||||
`INSERT INTO template_shifts (id, template_id, day_of_week, name, start_time, end_time, required_employees)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[uuidv4(), templateId, day, 'Vormittagsschicht', '08:00', '12:00', 1]
|
||||
);
|
||||
}
|
||||
|
||||
console.log('Vormittagsschichten Mo-Do erstellt');
|
||||
|
||||
// Nachmittagsschicht Mo-Do
|
||||
for (let day = 1; day <= 4; day++) {
|
||||
await db.run(
|
||||
`INSERT INTO template_shifts (id, template_id, day_of_week, name, start_time, end_time, required_employees)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[uuidv4(), templateId, day, 'Nachmittagsschicht', '11:30', '15:30', 1]
|
||||
);
|
||||
}
|
||||
|
||||
console.log('Nachmittagsschichten Mo-Do erstellt');
|
||||
|
||||
// Freitag nur Vormittagsschicht
|
||||
await db.run(
|
||||
`INSERT INTO template_shifts (id, template_id, day_of_week, name, start_time, end_time, required_employees)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
[uuidv4(), templateId, 5, 'Vormittagsschicht', '08:00', '12:00', 1]
|
||||
);
|
||||
|
||||
console.log('Freitag Vormittagsschicht erstellt');
|
||||
|
||||
await db.run('COMMIT');
|
||||
console.log('Standard-Vorlage erfolgreich initialisiert');
|
||||
} catch (error) {
|
||||
await db.run('ROLLBACK');
|
||||
throw error;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Erstellen der Standard-Vorlage:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user