refactor(tenant): remove legacy presentation configuration and related carousel images

This commit is contained in:
2026-07-29 14:54:57 -03:00
parent 5416c0fd61
commit b9ad2e589f
14 changed files with 136 additions and 560 deletions

View File

@@ -19,7 +19,6 @@ class BootstrapTenantController extends Controller
->with([
'headerLogo',
'footerLogo',
'mainCarouselImages',
'menues',
'socialMedia',
'categories' => fn ($query) => $query->orderBy('nombre'),

View File

@@ -19,7 +19,7 @@ class TenantController extends Controller
{
return TenantResource::collection(
Tenant::query()
->with(['headerLogo', 'footerLogo', 'mainCarouselImages', 'socialMedia'])
->with(['headerLogo', 'footerLogo', 'socialMedia'])
->latest()
->paginateFromRequest()
)->response();
@@ -30,14 +30,14 @@ class TenantController extends Controller
$tenant = $this->tenantService->create($request->validated());
return TenantResource::make(
$tenant->loadMissing(['headerLogo', 'footerLogo', 'mainCarouselImages', 'socialMedia'])
$tenant->loadMissing(['headerLogo', 'footerLogo', 'socialMedia'])
)->response()->setStatusCode(201);
}
public function show(Tenant $tenant): TenantResource
{
return TenantResource::make(
$tenant->loadMissing(['headerLogo', 'footerLogo', 'mainCarouselImages', 'socialMedia'])
$tenant->loadMissing(['headerLogo', 'footerLogo', 'socialMedia'])
);
}
@@ -46,7 +46,7 @@ class TenantController extends Controller
$tenant = $this->tenantService->update($tenant, $request->validated());
return TenantResource::make(
$tenant->loadMissing(['headerLogo', 'footerLogo', 'mainCarouselImages', 'socialMedia'])
$tenant->loadMissing(['headerLogo', 'footerLogo', 'socialMedia'])
);
}

View File

@@ -28,8 +28,6 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
'footer_bg_color',
'header_logo_id',
'footer_logo_id',
'hero_config',
'event_config',
'website_type_code',
'search_product_layout',
'search_group_layout',
@@ -58,8 +56,6 @@ class Tenant extends Model
protected function casts(): array
{
return [
'hero_config' => 'array',
'event_config' => 'array',
'search_product_layout' => ProductLayout::class,
'search_group_layout' => GroupLayout::class,
'search_items_per_page' => 'integer',
@@ -82,14 +78,6 @@ class Tenant extends Model
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');
}
/**
* @return BelongsTo<WebsiteType, $this>
*/
@@ -98,22 +86,6 @@ class Tenant extends Model
return $this->belongsTo(WebsiteType::class, 'website_type_code', 'codigo');
}
/**
* @return BelongsToMany<Attachment, $this>
*/
public function mainCarouselImages(): BelongsToMany
{
return $this->belongsToMany(
Attachment::class,
'tenant_main_carousel_images',
'tenant_id',
'attachment_id'
)
->withPivot('orden')
->withTimestamps()
->orderByPivot('orden');
}
public function catalogItems(): HasMany
{
return $this->hasMany(CatalogItem::class, 'tenant_code', 'codigo');

View File

@@ -62,9 +62,6 @@ 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})$/'],
'header_logo' => $logoRule,
'footer_logo' => $logoRule,
'hero_bg_image' => ['nullable', new ImageOrBase64Rule],
'main_carousel_images' => ['sometimes', 'array'],
'main_carousel_images.*' => ['required', 'distinct', new ImageOrBase64Rule],
'social_media' => ['sometimes', 'array'],
'social_media.*.code' => [
'required',
@@ -74,16 +71,6 @@ class StoreTenantRequest extends FormRequest
],
'social_media.*.url' => ['required', 'url', 'max:2048'],
'social_media.*.orden' => ['sometimes', 'integer', 'min:0', 'distinct'],
'hero_config' => ['nullable', 'array'],
'hero_config.title_html' => ['nullable', 'string'],
'hero_config.description_html' => ['nullable', 'string'],
'hero_config.button_text' => ['nullable', 'string'],
'hero_config.button_href' => ['nullable', 'string'],
'event_config' => ['nullable', 'array'],
'event_config.title' => ['nullable', 'string'],
'event_config.location' => ['nullable', 'string'],
'event_config.dates' => ['nullable', 'array'],
'event_config.dates.*' => ['required', 'string'],
'search_product_layout' => ['sometimes', Rule::enum(ProductLayout::class)],
'search_group_layout' => ['sometimes', Rule::enum(GroupLayout::class)],
'search_items_per_page' => ['sometimes', 'integer', 'min:4', 'max:48'],

View File

@@ -73,9 +73,6 @@ 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})$/'],
'header_logo' => $logoRule,
'footer_logo' => $logoRule,
'hero_bg_image' => ['nullable', new ImageOrBase64Rule],
'main_carousel_images' => ['sometimes', 'array'],
'main_carousel_images.*' => ['required', 'distinct', new ImageOrBase64Rule],
'social_media' => ['sometimes', 'array'],
'social_media.*.code' => [
'required',
@@ -85,16 +82,6 @@ class UpdateTenantRequest extends FormRequest
],
'social_media.*.url' => ['required', 'url', 'max:2048'],
'social_media.*.orden' => ['sometimes', 'integer', 'min:0', 'distinct'],
'hero_config' => ['nullable', 'array'],
'hero_config.title_html' => ['nullable', 'string'],
'hero_config.description_html' => ['nullable', 'string'],
'hero_config.button_text' => ['nullable', 'string'],
'hero_config.button_href' => ['nullable', 'string'],
'event_config' => ['nullable', 'array'],
'event_config.title' => ['nullable', 'string'],
'event_config.location' => ['nullable', 'string'],
'event_config.dates' => ['nullable', 'array'],
'event_config.dates.*' => ['required', 'string'],
'search_product_layout' => ['sometimes', Rule::enum(ProductLayout::class)],
'search_group_layout' => ['sometimes', Rule::enum(GroupLayout::class)],
'search_items_per_page' => ['sometimes', 'integer', 'min:4', 'max:48'],

View File

@@ -19,12 +19,6 @@ class TenantResource extends JsonResource
*/
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 [
'id' => $this->id,
'codigo' => $this->codigo,
@@ -39,17 +33,9 @@ class TenantResource extends JsonResource
// 1 day
'header_logo' => $this->headerLogo?->getTemporaryUrl(1440),
'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440),
'hero_config' => $heroConfig,
'event_config' => $this->event_config,
'search_product_layout' => $this->search_product_layout->value,
'search_group_layout' => $this->search_group_layout->value,
'search_items_per_page' => $this->search_items_per_page,
'main_carousel_images' => $this->whenLoaded(
'mainCarouselImages',
fn () => $this->mainCarouselImages
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
->values()
),
'social_media' => $this->whenLoaded(
'socialMedia',
fn () => $this->socialMedia

View File

@@ -2,13 +2,11 @@
namespace App\Domains\Tenant\Services;
use App\Domains\Attachable\Enums\AttachmentType;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Attachable\Services\AttachmentService;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
class TenantService
{
@@ -24,15 +22,11 @@ class TenantService
return DB::transaction(function () use ($data): Tenant {
$headerLogo = $data['header_logo'] ?? null;
$footerLogo = $data['footer_logo'] ?? null;
$heroBgImage = $data['hero_bg_image'] ?? null;
$mainCarouselImages = $data['main_carousel_images'] ?? [];
$socialMedia = $data['social_media'] ?? [];
unset(
$data['header_logo'],
$data['footer_logo'],
$data['hero_bg_image'],
$data['main_carousel_images'],
$data['social_media']
);
@@ -61,21 +55,8 @@ class TenantService
$data['header_logo_id'] = $headerAttachmentId;
$data['footer_logo_id'] = $footerAttachmentId;
if ($heroBgImage) {
$attachment = Str::isUuid($heroBgImage)
? 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 */
$tenant = Tenant::query()->create($data);
$this->syncMainCarouselImages($tenant, $mainCarouselImages);
$this->syncSocialMedia($tenant, $socialMedia);
return $tenant;
@@ -92,25 +73,17 @@ class TenantService
return DB::transaction(function () use ($tenant, $data): Tenant {
$hasHeaderLogoKey = array_key_exists('header_logo', $data);
$hasFooterLogoKey = array_key_exists('footer_logo', $data);
$hasHeroBgImageKey = array_key_exists('hero_bg_image', $data);
$hasMainCarouselImagesKey = array_key_exists('main_carousel_images', $data);
$hasSocialMediaKey = array_key_exists('social_media', $data);
$headerLogo = $data['header_logo'] ?? null;
$footerLogo = $data['footer_logo'] ?? null;
$heroBgImage = $data['hero_bg_image'] ?? null;
$mainCarouselImages = $data['main_carousel_images'] ?? [];
$socialMedia = $data['social_media'] ?? [];
unset(
$data['header_logo'],
$data['footer_logo'],
$data['hero_bg_image'],
$data['main_carousel_images'],
$data['social_media']
);
$oldHeroBgId = $tenant->hero_bg_image_id;
$tenant->fill($data);
if ($hasHeaderLogoKey) {
@@ -145,34 +118,8 @@ class TenantService
}
}
$currentHeroConfig = $tenant->hero_config ?? [];
if ($hasHeroBgImageKey) {
if ($heroBgImage) {
$attachment = Str::isUuid($heroBgImage)
? 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();
if ($hasMainCarouselImagesKey) {
$this->syncMainCarouselImages($tenant, $mainCarouselImages);
}
if ($hasSocialMediaKey) {
$this->syncSocialMedia($tenant, $socialMedia);
}
@@ -181,21 +128,6 @@ class TenantService
});
}
/**
* @param array<int, mixed> $images
*/
private function syncMainCarouselImages(Tenant $tenant, array $images): void
{
$attachments = [];
foreach (array_values($images) as $order => $image) {
$attachment = $this->resolveMainCarouselImage($image, $order);
$attachments[$attachment->id] = ['orden' => $order];
}
$tenant->mainCarouselImages()->sync($attachments);
}
/**
* @param array<int, array{code: string, url: string, orden?: int}> $socialMedia
*/
@@ -213,26 +145,4 @@ class TenantService
$tenant->socialMedia()->sync($associations);
$tenant->unsetRelation('socialMedia');
}
private function resolveMainCarouselImage(mixed $image, int $order): Attachment
{
if (is_string($image) && Str::isUuid($image)) {
$attachment = Attachment::query()
->where('key', $image)
->where('type', AttachmentType::Image->value)
->first();
if ($attachment === null) {
throw ValidationException::withMessages([
"main_carousel_images.{$order}" => [
__('api.tenant.invalid_carousel_image'),
],
]);
}
return $attachment;
}
return $this->attachmentService->store($image, 'tenants/main-carousel');
}
}