Files
shopit-back/database/seeders/TenantSeeder.php

217 lines
7.2 KiB
PHP

<?php
namespace Database\Seeders;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Attachable\Services\AttachmentService;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Services\TenantService;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use RuntimeException;
use Throwable;
class TenantSeeder extends Seeder
{
private const SOCIAL_MEDIA = [
[
'code' => 'instagram',
'url' => 'https://www.instagram.com/sonderargentina/',
'orden' => 0,
],
[
'code' => 'facebook',
'url' => 'https://www.facebook.com/SonderArgentina/',
'orden' => 1,
],
[
'code' => 'linkedin',
'url' => 'https://www.linkedin.com/company/35664983/',
'orden' => 2,
],
[
'code' => 'whatsapp',
'url' => 'https://wa.me/5493412602222',
'orden' => 3,
],
];
public function __construct(protected TenantService $tenantService) {}
public function run(): void
{
$this->deleteTenant('sonder');
Tenant::query()
->where('dominio', 'localhost')
->delete();
$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' => $this->uploadedImage('images/sonder_header.png', 'sonder_header.png'),
'footer_logo' => $this->uploadedImage('images/sonder_footer.png', 'sonder_footer.png'),
'social_media' => self::SOCIAL_MEDIA,
'website_type_code' => 'shopit',
'extras' => [
'carousel' => [
$this->uploadedImage(
'images/sonder-main-carousel/01-urban-team.png',
'01-urban-team.png',
),
$this->uploadedImage(
'images/sonder-main-carousel/02-running-shoes.png',
'02-running-shoes.png',
),
$this->uploadedImage(
'images/sonder-main-carousel/03-streetwear.png',
'03-streetwear.png',
),
$this->uploadedImage(
'images/sonder-main-carousel/04-football-training.png',
'04-football-training.png',
),
$this->uploadedImage(
'images/sonder-main-carousel/05-activewear-essentials.png',
'05-activewear-essentials.png',
),
$this->uploadedImage(
'images/sonder-main-carousel/06-city-runners.png',
'06-city-runners.png',
),
],
],
]);
$this->deleteTenant('fiesta_futbol_infantil');
$fiestaDomain = 'fiesta-futbol-infantil.localhost';
Tenant::query()
->where('dominio', $fiestaDomain)
->delete();
$this->tenantService->create([
'codigo' => 'fiesta_futbol_infantil',
'nombre' => 'Fiesta Fútbol Infantil',
'dominio' => $fiestaDomain,
'primary_color' => '#00973F',
'secondary_color' => '#A0A0A0',
'danger_color' => '#FF8888',
'success_color' => '#198754',
'header_bg_color' => '#ffffff',
'footer_bg_color' => '#015327',
'header_logo' => $this->uploadedImage(
'images/futbol_infantil_header.png',
'futbol_infantil_header.png',
),
'footer_logo' => $this->uploadedImage(
'images/futbol_infantil_footer.png',
'futbol_infantil_footer.png',
),
'social_media' => self::SOCIAL_MEDIA,
'website_type_code' => 'onticket',
'extras' => [
'heroConfig' => [
'title_html' => '<h1>Fiesta Fútbol Infantil</h1>',
'description_html' => '<p>Viví una jornada inolvidable de fútbol infantil.</p>',
'button_text' => 'Comprar entradas',
'button_href' => '/tickets',
'background_image_id' => $this->uploadedImage(
'images/futbol_infantil_hero.jpg',
'futbol_infantil_hero.jpg',
),
],
'eventConfig' => [
'title' => 'Fiesta Fútbol Infantil',
'location' => 'Rosario, Santa Fe',
'dates_text' => '9, 10, 11 y 12 de Octubre 2026',
'dates' => [
'2026-12-05',
'2026-12-06',
],
],
],
]);
}
private function deleteTenant(string $codigo): void
{
$tenant = Tenant::query()
->where('codigo', $codigo)
->first();
if ($tenant === null) {
return;
}
$attachmentService = app(AttachmentService::class);
$extraAttachments = Attachment::query()
->where('path', 'like', 'tenants/%/extras/%')
->get()
->filter(
fn (Attachment $attachment): bool => str_starts_with(
$attachment->path,
"tenants/{$codigo}/extras/"
)
);
foreach ($extraAttachments as $attachment) {
try {
$attachmentService->delete($attachment);
} catch (Throwable) {
// Ignore cleanup errors while recreating demo data.
}
}
if ($tenant->headerLogo) {
try {
$attachmentService->delete($tenant->headerLogo);
} catch (Throwable) {
// Ignore cleanup errors while recreating demo data.
}
}
if ($tenant->footerLogo && $tenant->footer_logo_id !== $tenant->header_logo_id) {
try {
$attachmentService->delete($tenant->footerLogo);
} catch (Throwable) {
// Ignore cleanup errors while recreating demo data.
}
}
$tenant->delete();
}
private function uploadedImage(string $relativePath, string $filename): UploadedFile
{
$path = public_path($relativePath);
if (! file_exists($path)) {
throw new RuntimeException("Image not found at path: {$path}");
}
$mimeType = match (strtolower(pathinfo($filename, PATHINFO_EXTENSION))) {
'jpg', 'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'webp' => 'image/webp',
default => 'image/png',
};
return new UploadedFile(
$path,
$filename,
$mimeType,
null,
true,
);
}
}