feat: add hero background image functionality to Tenant model and related requests, resources, and seeder

This commit is contained in:
ncoronel 2026-07-14 13:58:49 -03:00
parent b4b888adef
commit 38f36a3a23
8 changed files with 108 additions and 5 deletions

View File

@ -63,6 +63,14 @@ class Tenant extends Model
return $this->belongsTo(Attachment::class, 'footer_logo_id'); return $this->belongsTo(Attachment::class, 'footer_logo_id');
} }
/**
* @return BelongsTo<Attachment, $this>
*/
public function heroBgImage(): BelongsTo
{
return $this->belongsTo(Attachment::class, 'hero_bg_image_id');
}
public function productos(): HasMany public function productos(): HasMany
{ {
return $this->hasMany(Product::class, 'tenant_codigo', 'codigo'); return $this->hasMany(Product::class, 'tenant_codigo', 'codigo');

View File

@ -61,8 +61,8 @@ class StoreTenantRequest extends FormRequest
'footer_bg_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], 'footer_bg_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
'header_logo' => $logoRule, 'header_logo' => $logoRule,
'footer_logo' => $logoRule, 'footer_logo' => $logoRule,
'hero_bg_image' => ['nullable', new ImageOrBase64Rule()],
'hero_config' => ['nullable', 'array'], 'hero_config' => ['nullable', 'array'],
'hero_config.background_image' => ['nullable', 'string'],
'hero_config.title_html' => ['nullable', 'string'], 'hero_config.title_html' => ['nullable', 'string'],
'hero_config.description_html' => ['nullable', 'string'], 'hero_config.description_html' => ['nullable', 'string'],
'hero_config.button_text' => ['nullable', 'string'], 'hero_config.button_text' => ['nullable', 'string'],

View File

@ -72,8 +72,8 @@ class UpdateTenantRequest extends FormRequest
'footer_bg_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'], 'footer_bg_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
'header_logo' => $logoRule, 'header_logo' => $logoRule,
'footer_logo' => $logoRule, 'footer_logo' => $logoRule,
'hero_bg_image' => ['nullable', new ImageOrBase64Rule()],
'hero_config' => ['nullable', 'array'], 'hero_config' => ['nullable', 'array'],
'hero_config.background_image' => ['nullable', 'string'],
'hero_config.title_html' => ['nullable', 'string'], 'hero_config.title_html' => ['nullable', 'string'],
'hero_config.description_html' => ['nullable', 'string'], 'hero_config.description_html' => ['nullable', 'string'],
'hero_config.button_text' => ['nullable', 'string'], 'hero_config.button_text' => ['nullable', 'string'],

View File

@ -15,6 +15,12 @@ class TenantResource extends JsonResource
*/ */
public function toArray(Request $request): array public function toArray(Request $request): array
{ {
$heroConfig = $this->hero_config;
if (is_array($heroConfig)) {
$heroConfig['background_image'] = $this->heroBgImage?->getTemporaryUrl(1440);
unset($heroConfig['background_image_id']);
}
return [ return [
'id' => $this->id, 'id' => $this->id,
'codigo' => $this->codigo, 'codigo' => $this->codigo,
@ -29,7 +35,7 @@ class TenantResource extends JsonResource
// 1 day // 1 day
'header_logo' => $this->headerLogo?->getTemporaryUrl(1440), 'header_logo' => $this->headerLogo?->getTemporaryUrl(1440),
'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440 ), 'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440 ),
'hero_config' => $this->hero_config, 'hero_config' => $heroConfig,
'event_config' => $this->event_config, 'event_config' => $this->event_config,
'menues' => $this->whenLoaded('menues'), 'menues' => $this->whenLoaded('menues'),
]; ];

View File

@ -24,8 +24,9 @@ class TenantService
return DB::transaction(function () use ($data): Tenant { return DB::transaction(function () use ($data): Tenant {
$headerLogo = $data['header_logo'] ?? null; $headerLogo = $data['header_logo'] ?? null;
$footerLogo = $data['footer_logo'] ?? null; $footerLogo = $data['footer_logo'] ?? null;
$heroBgImage = $data['hero_bg_image'] ?? null;
unset($data['header_logo'], $data['footer_logo']); unset($data['header_logo'], $data['footer_logo'], $data['hero_bg_image']);
$headerAttachmentId = null; $headerAttachmentId = null;
if ($headerLogo) { if ($headerLogo) {
@ -52,6 +53,18 @@ class TenantService
$data['header_logo_id'] = $headerAttachmentId; $data['header_logo_id'] = $headerAttachmentId;
$data['footer_logo_id'] = $footerAttachmentId; $data['footer_logo_id'] = $footerAttachmentId;
if ($heroBgImage) {
$attachment = Str::isUuid($heroBgImage)
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $heroBgImage)->first()
: $this->attachmentService->store($heroBgImage, 'tenants');
if ($attachment) {
$heroConfig = $data['hero_config'] ?? [];
$heroConfig['background_image_id'] = $attachment->id;
$data['hero_config'] = $heroConfig;
}
}
/** @var Tenant $tenant */ /** @var Tenant $tenant */
$tenant = Tenant::query()->create($data); $tenant = Tenant::query()->create($data);
@ -71,10 +84,14 @@ class TenantService
return DB::transaction(function () use ($tenant, $data): Tenant { return DB::transaction(function () use ($tenant, $data): Tenant {
$hasHeaderLogoKey = array_key_exists('header_logo', $data); $hasHeaderLogoKey = array_key_exists('header_logo', $data);
$hasFooterLogoKey = array_key_exists('footer_logo', $data); $hasFooterLogoKey = array_key_exists('footer_logo', $data);
$hasHeroBgImageKey = array_key_exists('hero_bg_image', $data);
$headerLogo = $data['header_logo'] ?? null; $headerLogo = $data['header_logo'] ?? null;
$footerLogo = $data['footer_logo'] ?? null; $footerLogo = $data['footer_logo'] ?? null;
$heroBgImage = $data['hero_bg_image'] ?? null;
unset($data['header_logo'], $data['footer_logo']); unset($data['header_logo'], $data['footer_logo'], $data['hero_bg_image']);
$oldHeroBgId = $tenant->hero_bg_image_id;
$tenant->fill($data); $tenant->fill($data);
@ -110,6 +127,28 @@ class TenantService
} }
} }
$currentHeroConfig = $tenant->hero_config ?? [];
if ($hasHeroBgImageKey) {
if ($heroBgImage) {
$attachment = Str::isUuid($heroBgImage)
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $heroBgImage)->first()
: $this->attachmentService->store($heroBgImage, 'tenants');
if ($attachment) {
$currentHeroConfig['background_image_id'] = $attachment->id;
} else {
unset($currentHeroConfig['background_image_id']);
}
} else {
unset($currentHeroConfig['background_image_id']);
}
} else {
if ($oldHeroBgId) {
$currentHeroConfig['background_image_id'] = $oldHeroBgId;
}
}
$tenant->hero_config = empty($currentHeroConfig) ? null : $currentHeroConfig;
$tenant->save(); $tenant->save();
return $tenant; return $tenant;

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('tenants', function (Blueprint $table) {
$table->unsignedBigInteger('hero_bg_image_id')
->virtualAs('hero_config->>"$.background_image_id"')
->nullable()
->after('footer_bg_color');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('tenants', function (Blueprint $table) {
$table->dropColumn('hero_bg_image_id');
});
}
};

View File

@ -100,6 +100,11 @@ class TenantSeeder extends Seeder
$attachmentService->delete($existingFiesta->footerLogo); $attachmentService->delete($existingFiesta->footerLogo);
} catch (\Throwable $e) {} } catch (\Throwable $e) {}
} }
if ($existingFiesta->heroBgImage) {
try {
$attachmentService->delete($existingFiesta->heroBgImage);
} catch (\Throwable $e) {}
}
$existingFiesta->delete(); $existingFiesta->delete();
} }
@ -111,6 +116,7 @@ class TenantSeeder extends Seeder
$fiestaHeaderImagePath = public_path('images/futbol_infantil_header.png'); $fiestaHeaderImagePath = public_path('images/futbol_infantil_header.png');
$fiestaFooterImagePath = public_path('images/futbol_infantil_footer.png'); $fiestaFooterImagePath = public_path('images/futbol_infantil_footer.png');
$fiestaHeroBgImagePath = public_path('images/futbol_infantil_hero.jpg');
if (! file_exists($fiestaHeaderImagePath)) { if (! file_exists($fiestaHeaderImagePath)) {
throw new \RuntimeException("Image not found at path: {$fiestaHeaderImagePath}"); throw new \RuntimeException("Image not found at path: {$fiestaHeaderImagePath}");
@ -120,6 +126,10 @@ class TenantSeeder extends Seeder
throw new \RuntimeException("Image not found at path: {$fiestaFooterImagePath}"); throw new \RuntimeException("Image not found at path: {$fiestaFooterImagePath}");
} }
if (! file_exists($fiestaHeroBgImagePath)) {
throw new \RuntimeException("Image not found at path: {$fiestaHeroBgImagePath}");
}
$fiestaHeaderLogo = new UploadedFile( $fiestaHeaderLogo = new UploadedFile(
$fiestaHeaderImagePath, $fiestaHeaderImagePath,
'futbol_infantil_header.png', 'futbol_infantil_header.png',
@ -136,6 +146,14 @@ class TenantSeeder extends Seeder
true true
); );
$fiestaHeroBgImage = new UploadedFile(
$fiestaHeroBgImagePath,
'futbol_infantil_hero.jpg',
'image/jpeg',
null,
true
);
$this->tenantService->create([ $this->tenantService->create([
'codigo' => 'fiesta_futbol_infantil', 'codigo' => 'fiesta_futbol_infantil',
'nombre' => 'Fiesta Fútbol Infantil', 'nombre' => 'Fiesta Fútbol Infantil',
@ -148,6 +166,7 @@ class TenantSeeder extends Seeder
'footer_bg_color' => '#015327', 'footer_bg_color' => '#015327',
'header_logo' => $fiestaHeaderLogo, 'header_logo' => $fiestaHeaderLogo,
'footer_logo' => $fiestaFooterLogo, 'footer_logo' => $fiestaFooterLogo,
'hero_bg_image' => $fiestaHeroBgImage,
'hero_config' => [ 'hero_config' => [
'title_html' => '<strong>ASEGURÁ TU LUGAR</strong>', 'title_html' => '<strong>ASEGURÁ TU LUGAR</strong>',
'description_html' => '<strong>Comprá tu entrada oficial en segundos</strong> de forma 100% segura. Preparate para vivir la experiencia completa.', 'description_html' => '<strong>Comprá tu entrada oficial en segundos</strong> de forma 100% segura. Preparate para vivir la experiencia completa.',

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB