beta.0002

This commit is contained in:
Flatlogic Bot 2025-09-28 22:08:52 +00:00
parent 95de2da48e
commit 851fd0211a

View File

@ -13,12 +13,15 @@ function seed_database() {
// Check if user already exists // Check if user already exists
$stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?"); $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
$stmt->execute([$email]); $stmt->execute([$email]);
if ($stmt->fetch()) { $adminUserId = $stmt->fetchColumn();
echo "Admin user already exists.\n";
if ($adminUserId) {
echo "Admin user already exists with ID: $adminUserId.\n";
} else { } else {
$stmt = $pdo->prepare("INSERT INTO users (email, password_enc, role, display_name) VALUES (?, ?, 'Admin', 'Admin User')"); $stmt = $pdo->prepare("INSERT INTO users (email, password_enc, role, display_name) VALUES (?, ?, 'Admin', 'Admin User')");
$stmt->execute([$email, $hashedPassword]); $stmt->execute([$email, $hashedPassword]);
echo "Admin user created successfully.\n"; $adminUserId = $pdo->lastInsertId();
echo "Admin user created successfully with ID: $adminUserId.\n";
echo "Email: " . $email . "\n"; echo "Email: " . $email . "\n";
echo "Password: " . $password . "\n"; echo "Password: " . $password . "\n";
} }
@ -31,18 +34,34 @@ function seed_database() {
try { try {
$stmt = $pdo->query("SELECT count(*) FROM clients"); $stmt = $pdo->query("SELECT count(*) FROM clients");
if ($stmt->fetchColumn() > 0) { if ($stmt->fetchColumn() > 0) {
echo "Clients table already seeded.\n"; echo "Clients table already has data. Checking for user_id assignment.\n";
// Check if clients are unassigned
$stmt = $pdo->prepare("SELECT client_id FROM clients WHERE user_id IS NULL OR user_id = 0");
$stmt->execute();
$unassigned_clients = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (count($unassigned_clients) > 0) {
echo "Found " . count($unassigned_clients) . " unassigned clients. Assigning to admin user.\n";
$updateStmt = $pdo->prepare("UPDATE clients SET user_id = ? WHERE client_id = ?");
foreach ($unassigned_clients as $client_id) {
$updateStmt->execute([$adminUserId, $client_id]);
}
echo "Assigned " . count($unassigned_clients) . " clients to admin user.\n";
} else {
echo "All clients are already assigned to a user.\n";
}
} else { } else {
echo "Clients table is empty. Seeding new clients.\n";
$clients = [ $clients = [
['1001', 'Stark Industries', 'active'], ['1001', 'Stark Industries', 'active', $adminUserId],
['1002', 'Wayne Enterprises', 'active'], ['1002', 'Wayne Enterprises', 'active', $adminUserId],
['1003', 'Cyberdyne Systems', 'inactive'], ['1003', 'Cyberdyne Systems', 'inactive', $adminUserId],
]; ];
$stmt = $pdo->prepare("INSERT INTO clients (client_id, name, status) VALUES (?, ?, ?)"); $stmt = $pdo->prepare("INSERT INTO clients (client_id, name, status, user_id) VALUES (?, ?, ?, ?)");
foreach ($clients as $client) { foreach ($clients as $client) {
$stmt->execute($client); $stmt->execute($client);
} }
echo "Seeded " . count($clients) . " clients.\n"; echo "Seeded " . count($clients) . " clients for admin user.\n";
} }
} catch (PDOException $e) { } catch (PDOException $e) {
echo "Error seeding clients: " . $e->getMessage() . "\n"; echo "Error seeding clients: " . $e->getMessage() . "\n";