feat: implement full CRUD support and database schema for multi-tenant architecture
This commit is contained in:
@@ -17,11 +17,7 @@ class CartControllerTest extends TestCase
|
||||
|
||||
public function test_it_returns_an_empty_guest_cart_when_cart_does_not_exist(): void
|
||||
{
|
||||
Tenant::create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
]);
|
||||
$this->createTenant('acme', 'Acme', 'acme.com');
|
||||
|
||||
$this->getJson('/api/tenants/acme/cart')
|
||||
->assertOk()
|
||||
@@ -272,10 +268,10 @@ class CartControllerTest extends TestCase
|
||||
string $price,
|
||||
string $slugPrefix = 'shirt',
|
||||
): ProductVariant {
|
||||
Tenant::query()->firstOrCreate(
|
||||
['codigo' => $tenantCode],
|
||||
['nombre' => ucfirst($tenantCode), 'dominio' => "{$tenantCode}.com"],
|
||||
);
|
||||
$tenant = Tenant::query()->where('codigo', $tenantCode)->first();
|
||||
if (! $tenant) {
|
||||
$this->createTenant($tenantCode, ucfirst($tenantCode), "{$tenantCode}.com");
|
||||
}
|
||||
|
||||
$category = \App\Domains\Catalog\Models\Category::query()->create([
|
||||
'tenant_code' => $tenantCode,
|
||||
@@ -300,4 +296,37 @@ class CartControllerTest extends TestCase
|
||||
'precio' => $price,
|
||||
])->load('product');
|
||||
}
|
||||
|
||||
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
|
||||
{
|
||||
$hdrKey = (string) \Illuminate\Support\Str::uuid();
|
||||
$ftrKey = (string) \Illuminate\Support\Str::uuid();
|
||||
|
||||
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $hdrKey,
|
||||
'path' => 'tenants/' . $hdrKey . '.png',
|
||||
'filename' => 'logo_header.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $ftrKey,
|
||||
'path' => 'tenants/' . $ftrKey . '.png',
|
||||
'filename' => 'logo_footer.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
return Tenant::create([
|
||||
'codigo' => $codigo,
|
||||
'nombre' => $nombre,
|
||||
'dominio' => $dominio,
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo_id' => $headerAttachment->id,
|
||||
'footer_logo_id' => $footerAttachment->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,11 +12,7 @@ class ProductAttributeControllerTest extends TestCase
|
||||
|
||||
public function test_it_creates_a_select_attribute_with_options(): void
|
||||
{
|
||||
Tenant::create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
]);
|
||||
$this->createTenant('acme', 'Acme', 'acme.com');
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/product-attributes', [
|
||||
'codigo' => 'color',
|
||||
@@ -62,11 +58,7 @@ class ProductAttributeControllerTest extends TestCase
|
||||
|
||||
public function test_it_rejects_options_for_string_attributes(): void
|
||||
{
|
||||
Tenant::create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
]);
|
||||
$this->createTenant('acme', 'Acme', 'acme.com');
|
||||
|
||||
$response = $this->postJson('/api/tenants/acme/product-attributes', [
|
||||
'codigo' => 'material',
|
||||
@@ -81,4 +73,37 @@ class ProductAttributeControllerTest extends TestCase
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors(['options']);
|
||||
}
|
||||
|
||||
protected function createTenant(string $codigo, string $nombre, string $dominio): Tenant
|
||||
{
|
||||
$hdrKey = (string) \Illuminate\Support\Str::uuid();
|
||||
$ftrKey = (string) \Illuminate\Support\Str::uuid();
|
||||
|
||||
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $hdrKey,
|
||||
'path' => 'tenants/' . $hdrKey . '.png',
|
||||
'filename' => 'logo_header.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $ftrKey,
|
||||
'path' => 'tenants/' . $ftrKey . '.png',
|
||||
'filename' => 'logo_footer.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
return Tenant::create([
|
||||
'codigo' => $codigo,
|
||||
'nombre' => $nombre,
|
||||
'dominio' => $dominio,
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo_id' => $headerAttachment->id,
|
||||
'footer_logo_id' => $footerAttachment->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
|
||||
public function test_it_bootstraps_a_tenant_from_a_full_url(): void
|
||||
{
|
||||
Tenant::create([
|
||||
$this->createTenant([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
@@ -151,6 +151,12 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
'codigo' => 'globex',
|
||||
'nombre' => 'Globex',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo' => (string) Str::uuid(),
|
||||
'footer_logo' => (string) Str::uuid(),
|
||||
]);
|
||||
|
||||
$secondResponse
|
||||
@@ -160,13 +166,13 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
|
||||
public function test_it_allows_keeping_the_same_domain_on_update_but_rejects_collisions(): void
|
||||
{
|
||||
$tenant = Tenant::create([
|
||||
$tenant = $this->createTenant([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
]);
|
||||
|
||||
$otherTenant = Tenant::create([
|
||||
$otherTenant = $this->createTenant([
|
||||
'codigo' => 'globex',
|
||||
'nombre' => 'Globex',
|
||||
'dominio' => 'globex.com',
|
||||
@@ -246,7 +252,7 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
|
||||
public function test_it_allows_partial_update_without_required_fields(): void
|
||||
{
|
||||
$tenant = Tenant::create([
|
||||
$tenant = $this->createTenant([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
@@ -276,7 +282,13 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
$response = $this->postJson('/api/tenants', [
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => 'invalid-color',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo' => (string) Str::uuid(),
|
||||
'footer_logo' => (string) Str::uuid(),
|
||||
]);
|
||||
|
||||
$response->assertJsonValidationErrors(['primary_color']);
|
||||
@@ -284,7 +296,13 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
$response2 = $this->postJson('/api/tenants', [
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#12345',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo' => (string) Str::uuid(),
|
||||
'footer_logo' => (string) Str::uuid(),
|
||||
]);
|
||||
|
||||
$response2->assertJsonValidationErrors(['primary_color']);
|
||||
@@ -297,7 +315,13 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
$response = $this->postJson('/api/tenants', [
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo' => UploadedFile::fake()->create('document.pdf', 10, 'application/pdf'),
|
||||
'footer_logo' => (string) Str::uuid(),
|
||||
]);
|
||||
|
||||
$response->assertJsonValidationErrors(['header_logo']);
|
||||
@@ -305,7 +329,13 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
$response2 = $this->postJson('/api/tenants', [
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo' => 'data:application/pdf;base64,JVBERi0xLjQKJdcfqksKMSAwIG9iagogIDw8IC9UeXBlIC9DYXRhbG9nCiAgICAvUGFnZXMgMiAwIFI...',
|
||||
'footer_logo' => (string) Str::uuid(),
|
||||
]);
|
||||
|
||||
$response2->assertJsonValidationErrors(['header_logo']);
|
||||
@@ -321,6 +351,11 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
$response = $this->postJson('/api/tenants', [
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo' => $header,
|
||||
'footer_logo' => $footer,
|
||||
]);
|
||||
@@ -348,16 +383,64 @@ class BootstrapTenantControllerTest extends TestCase
|
||||
$this->assertDatabaseHas('attachments', ['key' => $tenant->footerLogo->key]);
|
||||
}
|
||||
|
||||
private function createTenant(array $attributes = []): Tenant
|
||||
{
|
||||
$hdrKey = (string) Str::uuid();
|
||||
$ftrKey = (string) Str::uuid();
|
||||
|
||||
$headerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $hdrKey,
|
||||
'path' => 'tenants/' . $hdrKey . '.png',
|
||||
'filename' => 'logo_header.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $ftrKey,
|
||||
'path' => 'tenants/' . $ftrKey . '.png',
|
||||
'filename' => 'logo_footer.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
return Tenant::create(array_merge([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#ff0000',
|
||||
'secondary_color' => '#00ff00',
|
||||
'danger_color' => '#0000ff',
|
||||
'header_footer_bg_color' => '#ffffff',
|
||||
'header_logo_id' => $headerAttachment->id,
|
||||
'footer_logo_id' => $footerAttachment->id,
|
||||
], $attributes));
|
||||
}
|
||||
|
||||
public function test_it_stores_base64_logos_in_tenants_directory(): void
|
||||
{
|
||||
Storage::fake('s3');
|
||||
|
||||
$base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
|
||||
|
||||
$ftrUuid = (string) Str::uuid();
|
||||
$footerAttachment = \App\Domains\Attachable\Models\Attachment::create([
|
||||
'key' => $ftrUuid,
|
||||
'path' => 'tenants/' . $ftrUuid . '.png',
|
||||
'filename' => 'logo_footer.png',
|
||||
'type' => \App\Domains\Attachable\Enums\AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
|
||||
$response = $this->postJson('/api/tenants', [
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'header_footer_bg_color' => '#444444',
|
||||
'header_logo' => $base64Image,
|
||||
'footer_logo' => $ftrUuid,
|
||||
]);
|
||||
|
||||
$response->assertCreated();
|
||||
|
||||
Reference in New Issue
Block a user