feat(seed): RolesSeeder + SystemTenantSeeder + MusterschuleSeeder + PrivatBobanSeeder

main
Boban Blaskovic 2026-05-22 06:07:35 +02:00
parent 0f3a48604e
commit bc6fff37a7
5 changed files with 178 additions and 12 deletions

View File

@ -2,24 +2,18 @@
namespace Database\Seeders; namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
{ {
use WithoutModelEvents;
/**
* Seed the application's database.
*/
public function run(): void public function run(): void
{ {
// User::factory(10)->create(); // Order matters: Roles FIRST (FK deps), then System, then Schools, then Private
$this->call([
User::factory()->create([ RolesSeeder::class,
'name' => 'Test User', SystemTenantSeeder::class,
'email' => 'test@example.com', MusterschuleSeeder::class,
PrivatBobanSeeder::class,
]); ]);
} }
} }

View File

@ -0,0 +1,55 @@
<?php
namespace Database\Seeders;
use App\Models\License;
use App\Models\Role;
use App\Models\Tenant;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class MusterschuleSeeder extends Seeder
{
public function run(): void
{
$school = Tenant::firstOrCreate(
['name' => 'Musterschule Wien'],
['type' => 'school', 'active' => true]
);
$license = License::withoutGlobalScopes()->firstOrCreate(
['tenant_id' => $school->id, 'type' => 'school_flat'],
['seats' => null, 'expires_at' => now()->addYear(), 'active' => true]
);
$users = [
['email' => 'schuladmin@musterschule.at', 'name' => 'Schuladmin Muster', 'role' => 'school-admin'],
['email' => 'lehrer@musterschule.at', 'name' => 'Lehrerin Muster', 'role' => 'teacher'],
['email' => 'kind1@musterschule.at', 'name' => 'Kind 1', 'role' => 'child'],
['email' => 'kind2@musterschule.at', 'name' => 'Kind 2', 'role' => 'child'],
];
foreach ($users as $data) {
$user = \App\Models\User::withoutGlobalScopes()->firstOrCreate(
['email' => $data['email']],
['name' => $data['name'], 'tenant_id' => $school->id, 'password' => Hash::make('password')]
);
$role = Role::where('name', $data['role'])->first();
DB::table('role_user')->updateOrInsert(
['user_id' => $user->id],
['role_id' => $role->id, 'created_at' => now(), 'updated_at' => now()]
);
if ($data['role'] === 'child') {
DB::table('license_assignments')->insertOrIgnore([
'license_id' => $license->id,
'user_id' => $user->id,
'created_at' => now(),
'updated_at' => now(),
]);
}
}
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace Database\Seeders;
use App\Models\License;
use App\Models\Role;
use App\Models\Tenant;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class PrivatBobanSeeder extends Seeder
{
public function run(): void
{
$privat = Tenant::firstOrCreate(
['name' => 'Privat Boban'],
['type' => 'private', 'active' => true]
);
$license = License::withoutGlobalScopes()->firstOrCreate(
['tenant_id' => $privat->id, 'type' => 'per_student'],
['seats' => 5, 'expires_at' => now()->addYear(), 'active' => true]
);
$users = [
['email' => 'boban@lernschiff.com', 'name' => 'Boban Blaskovic', 'role' => 'parent'],
['email' => 'kind.boban@lernschiff.com', 'name' => 'Kind Boban', 'role' => 'child'],
];
$createdUsers = [];
foreach ($users as $data) {
$user = \App\Models\User::withoutGlobalScopes()->firstOrCreate(
['email' => $data['email']],
['name' => $data['name'], 'tenant_id' => $privat->id, 'password' => Hash::make('password')]
);
$role = Role::where('name', $data['role'])->first();
DB::table('role_user')->updateOrInsert(
['user_id' => $user->id],
['role_id' => $role->id, 'created_at' => now(), 'updated_at' => now()]
);
if ($data['role'] === 'child') {
DB::table('license_assignments')->insertOrIgnore([
'license_id' => $license->id,
'user_id' => $user->id,
'created_at' => now(),
'updated_at' => now(),
]);
}
$createdUsers[$data['role']] = $user;
}
// Link parent ↔ child (cross-tenant-capable via withoutGlobalScope on children() relation)
if (isset($createdUsers['parent'], $createdUsers['child'])) {
$parent = $createdUsers['parent'];
$child = $createdUsers['child'];
// Avoid duplicate attach
if (!$parent->children()->withoutGlobalScope(\App\Scopes\TenantScope::class)->where('child_id', $child->id)->exists()) {
$parent->children()->attach($child->id);
}
}
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Database\Seeders;
use App\Models\Role;
use Illuminate\Database\Seeder;
class RolesSeeder extends Seeder
{
public function run(): void
{
foreach (['platform-admin', 'school-admin', 'teacher', 'parent', 'child'] as $name) {
Role::firstOrCreate(['name' => $name]);
}
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Database\Seeders;
use App\Models\Role;
use App\Models\Tenant;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class SystemTenantSeeder extends Seeder
{
public function run(): void
{
$system = Tenant::firstOrCreate(
['type' => 'system'],
['name' => 'System', 'active' => true]
);
$admin = \App\Models\User::withoutGlobalScopes()->firstOrCreate(
['email' => 'admin@lernschiff.com'],
[
'name' => 'Platform Admin',
'tenant_id' => $system->id,
'password' => Hash::make('password'),
]
);
$role = Role::where('name', 'platform-admin')->first();
DB::table('role_user')->updateOrInsert(
['user_id' => $admin->id],
['role_id' => $role->id, 'created_at' => now(), 'updated_at' => now()]
);
}
}