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

99 lines
3.8 KiB
PHP

<?php
namespace Database\Seeders;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Shared\Attachable\Services\AttachmentService;
use Illuminate\Database\Seeder;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use RuntimeException;
class TestEventsSeeder extends Seeder
{
public function __construct(private readonly AttachmentService $attachmentService) {}
public function run(): void
{
$tenant = Tenant::query()->where('codigo', 'onticket')->firstOrFail();
$events = [
[
'title' => '[Prueba] Noche de Música en Vivo',
'subtitle' => 'Una noche para cantar y bailar',
'description' => 'Evento de prueba para explorar la cartelera de OnTicket.',
'location' => 'Teatro Broadway, Rosario',
'image' => 'live-music.png',
'weeks_from_now' => [2],
'time_start' => '21:00:00',
'time_end' => '23:30:00',
],
[
'title' => '[Prueba] Festival de Sabores',
'subtitle' => 'Gastronomía y música para toda la familia',
'description' => 'Evento de prueba con dos jornadas disponibles.',
'location' => 'Parque de España, Rosario',
'image' => 'food-festival.png',
'weeks_from_now' => [3, 3],
'day_offsets' => [0, 1],
'time_start' => '12:00:00',
'time_end' => '20:00:00',
],
[
'title' => '[Prueba] Stand Up en el Centro',
'subtitle' => 'Humor para compartir',
'description' => 'Evento de prueba para visualizar distintas propuestas.',
'location' => 'Centro Cultural La Comedia, Rosario',
'image' => 'stand-up.png',
'weeks_from_now' => [4],
'time_start' => '20:30:00',
'time_end' => '22:00:00',
],
];
DB::transaction(function () use ($tenant, $events): void {
foreach ($events as $definition) {
$event = $tenant->events()->firstOrCreate(
['title' => $definition['title']],
[
'subtitle' => $definition['subtitle'],
'description' => $definition['description'],
'location' => $definition['location'],
'published_at' => now(),
],
);
if ($event->attachment_id === null) {
$path = public_path('images/tennants/onticket/test-events/'.$definition['image']);
if (! is_file($path)) {
throw new RuntimeException("Image not found at path: {$path}");
}
$attachment = $this->attachmentService->store(
new UploadedFile($path, $definition['image'], 'image/png', null, true),
'tenants/onticket/events',
);
$event->update(['attachment_id' => $attachment->id]);
}
if ($event->dates()->exists()) {
continue;
}
foreach ($definition['weeks_from_now'] as $index => $weeks) {
$date = now()->startOfDay()->addWeeks($weeks)
->addDays($definition['day_offsets'][$index] ?? 0);
$event->dates()->create([
'tenant_code' => $tenant->codigo,
'date' => $date->toDateString(),
'time_start' => $definition['time_start'],
'time_end' => $definition['time_end'],
]);
}
}
});
}
}