refactor(tenant): introduce TenantInformationService for loading tenant data and resolving website extras

This commit is contained in:
2026-07-29 15:48:01 -03:00
parent 27d4f9fac6
commit 8e3ee7eff6
8 changed files with 436 additions and 33 deletions

View File

@@ -5,6 +5,7 @@ namespace Tests\Feature\Seeders;
use App\Domains\Tenant\Models\Tenant;
use Database\Seeders\SocialMediaSeeder;
use Database\Seeders\TenantSeeder;
use Database\Seeders\WebsiteTypeSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
@@ -18,6 +19,7 @@ class TenantSeederTest extends TestCase
Storage::fake('s3');
$this->seed([
WebsiteTypeSeeder::class,
SocialMediaSeeder::class,
TenantSeeder::class,
]);
@@ -42,4 +44,65 @@ class TenantSeederTest extends TestCase
$this->assertSame([0, 1, 2, 3], $socialMedia->pluck('pivot.orden')->all());
}
}
public function test_it_assigns_each_tenant_its_website_type_and_extras(): void
{
Storage::fake('s3');
$this->seed([
WebsiteTypeSeeder::class,
SocialMediaSeeder::class,
TenantSeeder::class,
]);
$sonder = Tenant::query()
->where('codigo', 'sonder')
->with('websiteExtras.websiteTypeExtra')
->sole();
$this->assertSame('shopit', $sonder->website_type_code);
$carousel = $sonder->websiteExtras
->firstWhere('websiteTypeExtra.nombre', 'carousel');
$this->assertNotNull($carousel);
$this->assertCount(6, $carousel->config);
foreach ($carousel->config as $attachmentId) {
$this->assertDatabaseHas('attachments', [
'id' => $attachmentId,
'type' => 'image',
]);
}
$fiesta = Tenant::query()
->where('codigo', 'fiesta_futbol_infantil')
->with('websiteExtras.websiteTypeExtra')
->sole();
$this->assertSame('onticket', $fiesta->website_type_code);
$extras = $fiesta->websiteExtras->keyBy('websiteTypeExtra.nombre');
$this->assertEqualsCanonicalizing(
['heroConfig', 'eventConfig'],
$extras->keys()->all(),
);
$heroConfig = $extras->get('heroConfig')->config;
$this->assertSame('<h1>Fiesta Fútbol Infantil</h1>', $heroConfig['title_html']);
$this->assertIsInt($heroConfig['background_image_id']);
$this->assertDatabaseHas('attachments', [
'id' => $heroConfig['background_image_id'],
'type' => 'image',
]);
$this->assertSame([
'title' => 'Fiesta Fútbol Infantil',
'location' => 'Rosario, Santa Fe',
'dates' => [
'2026-12-05',
'2026-12-06',
],
], $extras->get('eventConfig')->config);
}
}

View File

@@ -2,7 +2,9 @@
namespace Tests\Feature\Tenant;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Services\TenantInformationService;
use Database\Seeders\WebsiteTypeSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
@@ -99,10 +101,10 @@ class StoreTenantWithExtrasTest extends TestCase
$response->assertCreated();
$config = Tenant::query()
$tenant = Tenant::query()
->where('codigo', 'festival')
->sole()
->websiteExtras()
->sole();
$config = $tenant->websiteExtras()
->sole()
->config;
@@ -112,7 +114,53 @@ class StoreTenantWithExtrasTest extends TestCase
'id' => $config[0],
'type' => 'image',
]);
$response->assertJsonPath('data.extras.carousel.0', $config[0]);
$attachment = Attachment::query()->findOrFail($config[0]);
$carouselUrl = $response->json('data.extras.carousel.0');
$this->assertIsString($carouselUrl);
$this->assertStringContainsString($attachment->key, $carouselUrl);
app(TenantInformationService::class)->load($tenant);
$this->assertInstanceOf(
Attachment::class,
$tenant->websiteExtras->sole()->resolvedConfig()[0]
);
}
public function test_it_returns_scalar_attachment_fields_as_temporary_urls(): void
{
$image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
$response = $this->postJson('/api/tenants', array_merge($this->tenantData(), [
'website_type_code' => 'onticket',
'extras' => [
'heroConfig' => [
'title_html' => '<h1>Festival</h1>',
'background_image_id' => $image,
],
],
]));
$response->assertCreated();
$heroConfig = Tenant::query()
->where('codigo', 'festival')
->sole()
->websiteExtras()
->whereHas(
'websiteTypeExtra',
fn ($query) => $query->where('nombre', 'heroConfig')
)
->sole()
->config;
$attachment = Attachment::query()->findOrFail($heroConfig['background_image_id']);
$backgroundUrl = $response->json('data.extras.heroConfig.background_image_id');
$this->assertIsString($backgroundUrl);
$this->assertStringContainsString($attachment->key, $backgroundUrl);
}
/**