shopit-back/database/seeders/TenantSeeder.php

90 lines
2.8 KiB
PHP

<?php
namespace Database\Seeders;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Services\TenantService;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
class TenantSeeder extends Seeder
{
public function __construct(protected TenantService $tenantService)
{
}
/**
* Run the database seeds.
*/
public function run(): void
{
// Check if tenant 'sonder' already exists and delete it to prevent duplicates
$existing = Tenant::query()->where('codigo', 'sonder')->first();
if ($existing) {
$attachmentService = app(\App\Domains\Attachable\Services\AttachmentService::class);
if ($existing->headerLogo) {
try {
$attachmentService->delete($existing->headerLogo);
} catch (\Throwable $e) {
// Ignore exception on cleanup
}
}
if ($existing->footerLogo && $existing->footer_logo_id !== $existing->header_logo_id) {
try {
$attachmentService->delete($existing->footerLogo);
} catch (\Throwable $e) {
// Ignore exception on cleanup
}
}
$existing->delete();
}
// Check if domain 'localhost' is already in use by another tenant and delete it
$existingDomain = Tenant::query()->where('dominio', 'localhost')->first();
if ($existingDomain) {
$existingDomain->delete();
}
$headerImagePath = public_path('images/sonder_header.png');
$footerImagePath = public_path('images/sonder_footer.png');
if (! file_exists($headerImagePath)) {
throw new \RuntimeException("Image not found at path: {$headerImagePath}");
}
if (! file_exists($footerImagePath)) {
throw new \RuntimeException("Image not found at path: {$footerImagePath}");
}
$headerLogo = new UploadedFile(
$headerImagePath,
'sonder_header.png',
'image/png',
null,
true
);
$footerLogo = new UploadedFile(
$footerImagePath,
'sonder_footer.png',
'image/png',
null,
true
);
$this->tenantService->create([
'codigo' => 'sonder',
'nombre' => 'Sonder',
'dominio' => 'localhost',
'primary_color' => '#6376F3',
'secondary_color' => '#A0A0A0',
'danger_color' => '#FF8888',
'success_color' => '#198754',
'header_bg_color' => '#ffffff',
'footer_bg_color' => '#313131',
'header_logo' => $headerLogo,
'footer_logo' => $footerLogo,
]);
}
}