Files
ecdown 177f122c10
tests / ci (push) Canceled after 0s
added roles and organizations
2026-10-15 20:59:32 -07:00

127 lines
4.2 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Folder;
use App\Models\Todo;
use App\Models\User;
use App\TodoStatus;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
use WithoutModelEvents;
/**
* Seed the application's database.
*/
public function run(): void
{
$this->call(BaseDatabaseSeeder::class);
$user = User::updateOrCreate(
['email' => 'test@example.com'],
[
'name' => 'Test User',
'password' => 'password',
'is_admin' => true,
],
);
// Model events are disabled while seeding, so the created hook that
// normally hands every account its personal workspace does not run.
if ($user->current_organization_id === null) {
$user->createPersonalOrganization();
}
$work = $this->createTopLevelFolder($user, 'Work');
$business = $this->createTopLevelFolder($user, 'Business');
$personal = $this->createTopLevelFolder($user, 'Personal');
$workCompanies = [
$this->createChildFolder($work, 'Acme Corp'),
$this->createChildFolder($work, 'Globex'),
];
$businessTypes = [
$this->createChildFolder($business, 'Invoices'),
$this->createChildFolder($business, 'Prospects'),
];
$personalTypes = [
$this->createChildFolder($personal, 'Health'),
$this->createChildFolder($personal, 'Home'),
];
$folders = collect([$work, $business, $personal])
->merge($workCompanies)
->merge($businessTypes)
->merge($personalTypes);
$samples = [
['Work out the Q4 roadmap', 'Acme Corp', 0, 'open'],
['Review Globex contract', 'Globex', 2, 'overdue'],
['Send invoice for consulting work', 'Invoices', 1, 'due-today'],
['Follow up on new prospect', 'Prospects', 5, 'open'],
['Book dentist appointment', 'Health', -1, 'overdue'],
['Fix the garage door', 'Home', 7, 'open'],
['Plan summer vacation', 'Personal', null, 'open'],
['Consolidate expense reports', 'Business', 10, 'completed'],
];
$foldersByName = $folders->keyBy(fn (Folder $folder) => $folder->name);
foreach ($samples as [$description, $folderName, $daysFromToday, $status]) {
$todo = Todo::firstOrCreate(
['user_id' => $user->id, 'folder_id' => $foldersByName[$folderName]->id, 'description' => $description],
[
'status' => TodoStatus::Open,
'organization_id' => $foldersByName[$folderName]->organization_id,
],
);
if ($daysFromToday === null) {
$todo->update(['due_date' => null]);
} else {
$todo->update(['due_date' => today()->addDays($daysFromToday)]);
}
if ($status === 'overdue') {
$todo->update(['due_date' => today()->subDays(3)]);
}
if ($status === 'completed') {
$todo->update([
'status' => TodoStatus::Completed,
'completed_at' => now()->subDay(),
]);
} else {
$todo->update(['completed_at' => null]);
}
}
}
private function createTopLevelFolder(User $user, string $name): Folder
{
return Folder::firstOrCreate(
['user_id' => $user->id, 'name' => $name, 'parent_id' => null],
[
'organization_id' => $user->current_organization_id,
'color' => fake()->optional()->randomElement(Folder::COLORS),
],
);
}
private function createChildFolder(Folder $parent, string $name): Folder
{
return Folder::firstOrCreate(
['user_id' => $parent->user_id, 'name' => $name, 'parent_id' => $parent->id],
[
'organization_id' => $parent->organization_id,
'color' => fake()->optional()->randomElement(Folder::COLORS),
],
);
}
}