feat(carousel): add migration and test for Mutual SMEP carousel images

This commit is contained in:
2026-09-24 12:41:38 -03:00
parent 74b72a7d4e
commit c3def0678f
6 changed files with 259 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
return new class extends Migration
{
private const TENANT_CODE = 'mutual_smep';
private const EXTRA_CODE = 'carousel';
private const IMAGE_DIRECTORY = 'images/tennants/mutual_smep/carousel';
private const STORAGE_PREFIX = 'tenants/mutual_smep/extras/carousel-v1/';
/** @var list<string> */
private const IMAGES = [
'01-tecnologia.webp',
'02-hogar.webp',
'03-vida-activa.webp',
'04-descanso.webp',
];
/** @var list<string> */
private array $storedPaths = [];
public function up(): void
{
$tenant = DB::table('tenants')
->where('codigo', self::TENANT_CODE)
->first(['codigo', 'storefront_website_type_code']);
if ($tenant === null) {
return;
}
$extraId = DB::table('storefront_website_type_extras')
->where('storefront_website_type_code', $tenant->storefront_website_type_code)
->where('codigo', self::EXTRA_CODE)
->value('id');
if ($extraId === null || DB::table('websites_extras')->where([
'website_code' => self::TENANT_CODE,
'website_type_extra_id' => $extraId,
])->exists()) {
return;
}
try {
DB::transaction(function () use ($extraId): void {
$attachmentIds = [];
foreach (self::IMAGES as $filename) {
$attachmentIds[] = $this->storeImage($filename);
}
DB::table('websites_extras')->insert([
'website_code' => self::TENANT_CODE,
'website_type_extra_id' => $extraId,
'config' => json_encode($attachmentIds, JSON_THROW_ON_ERROR),
'is_enabled' => true,
'created_at' => now(),
'updated_at' => now(),
]);
});
} catch (Throwable $throwable) {
Storage::disk('s3')->delete($this->storedPaths);
throw $throwable;
}
}
public function down(): void
{
$attachments = DB::table('attachments')
->where('path', 'like', self::STORAGE_PREFIX.'%')
->get(['id', 'path']);
if ($attachments->isEmpty()) {
return;
}
$attachmentIds = $attachments->pluck('id')->map(fn ($id): int => (int) $id)->all();
$extraId = DB::table('storefront_website_type_extras')
->where('storefront_website_type_code', 'shopit')
->where('codigo', self::EXTRA_CODE)
->value('id');
DB::transaction(function () use ($attachmentIds, $extraId): void {
if ($extraId !== null) {
DB::table('websites_extras')
->where('website_code', self::TENANT_CODE)
->where('website_type_extra_id', $extraId)
->where('config', json_encode($attachmentIds, JSON_THROW_ON_ERROR))
->delete();
}
DB::table('attachments')->whereIn('id', $attachmentIds)->delete();
});
Storage::disk('s3')->delete($attachments->pluck('path')->all());
}
private function storeImage(string $filename): int
{
$sourcePath = public_path(self::IMAGE_DIRECTORY."/{$filename}");
if (! is_file($sourcePath)) {
throw new RuntimeException("Carousel image not found: {$sourcePath}");
}
$contents = file_get_contents($sourcePath);
if ($contents === false) {
throw new RuntimeException("Carousel image could not be read: {$sourcePath}");
}
$storedPath = self::STORAGE_PREFIX.$filename;
if (! Storage::disk('s3')->put($storedPath, $contents)) {
throw new RuntimeException("Carousel image could not be stored: {$storedPath}");
}
$this->storedPaths[] = $storedPath;
return (int) DB::table('attachments')->insertGetId([
'key' => (string) Str::uuid(),
'path' => $storedPath,
'filename' => $filename,
'type' => 'image',
'mime_type' => 'image/webp',
'extension' => 'webp',
'size' => strlen($contents),
'created_at' => now(),
'updated_at' => now(),
]);
}
};