feat(website): add website type and extras models with migration and tests
This commit is contained in:
@@ -30,6 +30,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
'footer_logo_id',
|
||||
'hero_config',
|
||||
'event_config',
|
||||
'website_type_code',
|
||||
'search_product_layout',
|
||||
'search_group_layout',
|
||||
'search_items_per_page',
|
||||
@@ -89,6 +90,14 @@ class Tenant extends Model
|
||||
return $this->belongsTo(Attachment::class, 'hero_bg_image_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<WebsiteType, $this>
|
||||
*/
|
||||
public function websiteType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WebsiteType::class, 'website_type_code', 'codigo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<Attachment, $this>
|
||||
*/
|
||||
@@ -136,6 +145,14 @@ class Tenant extends Model
|
||||
->orderByPivot('orden');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<WebsiteExtra, $this>
|
||||
*/
|
||||
public function websiteExtras(): HasMany
|
||||
{
|
||||
return $this->hasMany(WebsiteExtra::class, 'website_code', 'codigo');
|
||||
}
|
||||
|
||||
public function menues(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
|
||||
47
app/Domains/Tenant/Models/WebsiteExtra.php
Normal file
47
app/Domains/Tenant/Models/WebsiteExtra.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
#[Fillable([
|
||||
'website_code',
|
||||
'website_type_extra_id',
|
||||
'config',
|
||||
])]
|
||||
class WebsiteExtra extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'websites_extras';
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'website_type_extra_id' => 'integer',
|
||||
'config' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Tenant, $this>
|
||||
*/
|
||||
public function website(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Tenant::class, 'website_code', 'codigo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<WebsiteTypeExtra, $this>
|
||||
*/
|
||||
public function websiteTypeExtra(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WebsiteTypeExtra::class, 'website_type_extra_id');
|
||||
}
|
||||
}
|
||||
35
app/Domains/Tenant/Models/WebsiteType.php
Normal file
35
app/Domains/Tenant/Models/WebsiteType.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
'codigo',
|
||||
'nombre',
|
||||
])]
|
||||
class WebsiteType extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'website_type';
|
||||
|
||||
/**
|
||||
* @return HasMany<WebsiteTypeExtra, $this>
|
||||
*/
|
||||
public function extras(): HasMany
|
||||
{
|
||||
return $this->hasMany(WebsiteTypeExtra::class, 'website_type');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<Tenant, $this>
|
||||
*/
|
||||
public function tenants(): HasMany
|
||||
{
|
||||
return $this->hasMany(Tenant::class, 'website_type_code', 'codigo');
|
||||
}
|
||||
}
|
||||
51
app/Domains/Tenant/Models/WebsiteTypeExtra.php
Normal file
51
app/Domains/Tenant/Models/WebsiteTypeExtra.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
#[Fillable([
|
||||
'website_type',
|
||||
'nombre',
|
||||
'descripcion',
|
||||
'is_required',
|
||||
'config_schema',
|
||||
])]
|
||||
class WebsiteTypeExtra extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'website_type_extras';
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'website_type' => 'integer',
|
||||
'is_required' => 'boolean',
|
||||
'config_schema' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<WebsiteType, $this>
|
||||
*/
|
||||
public function websiteType(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(WebsiteType::class, 'website_type');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<WebsiteExtra, $this>
|
||||
*/
|
||||
public function websiteExtras(): HasMany
|
||||
{
|
||||
return $this->hasMany(WebsiteExtra::class, 'website_type_extra_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('website_type', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('codigo')->unique();
|
||||
$table->string('nombre');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->string('website_type_code')->nullable();
|
||||
|
||||
$table->foreign('website_type_code')
|
||||
->references('codigo')
|
||||
->on('website_type')
|
||||
->cascadeOnUpdate()
|
||||
->nullOnDelete();
|
||||
});
|
||||
|
||||
Schema::create('website_type_extras', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('website_type')
|
||||
->constrained('website_type')
|
||||
->cascadeOnUpdate()
|
||||
->cascadeOnDelete();
|
||||
$table->string('nombre');
|
||||
$table->text('descripcion');
|
||||
$table->boolean('is_required')->default(false);
|
||||
$table->json('config_schema');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::create('websites_extras', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('website_code');
|
||||
$table->foreignId('website_type_extra_id')
|
||||
->constrained('website_type_extras')
|
||||
->cascadeOnUpdate()
|
||||
->cascadeOnDelete();
|
||||
$table->json('config');
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign('website_code')
|
||||
->references('codigo')
|
||||
->on('tenants')
|
||||
->cascadeOnUpdate()
|
||||
->cascadeOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('websites_extras');
|
||||
Schema::dropIfExists('website_type_extras');
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->dropForeign(['website_type_code']);
|
||||
$table->dropColumn('website_type_code');
|
||||
});
|
||||
|
||||
Schema::dropIfExists('website_type');
|
||||
}
|
||||
};
|
||||
124
tests/Feature/Tenant/WebsiteExtrasTest.php
Normal file
124
tests/Feature/Tenant/WebsiteExtrasTest.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Tenant\Models\WebsiteType;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Tests\TestCase;
|
||||
|
||||
class WebsiteExtrasTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_website_extra_tables_have_the_expected_columns(): void
|
||||
{
|
||||
$this->assertTrue(Schema::hasColumn('tenants', 'website_type_code'));
|
||||
|
||||
$this->assertEqualsCanonicalizing([
|
||||
'id',
|
||||
'codigo',
|
||||
'nombre',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
], Schema::getColumnListing('website_type'));
|
||||
|
||||
$this->assertEqualsCanonicalizing([
|
||||
'id',
|
||||
'website_type',
|
||||
'nombre',
|
||||
'descripcion',
|
||||
'is_required',
|
||||
'config_schema',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
], Schema::getColumnListing('website_type_extras'));
|
||||
|
||||
$this->assertEqualsCanonicalizing([
|
||||
'id',
|
||||
'website_code',
|
||||
'website_type_extra_id',
|
||||
'config',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
], Schema::getColumnListing('websites_extras'));
|
||||
}
|
||||
|
||||
public function test_models_map_the_diagram_relations_and_casts(): void
|
||||
{
|
||||
$type = WebsiteType::query()->create([
|
||||
'codigo' => 'store',
|
||||
'nombre' => 'Tienda',
|
||||
]);
|
||||
$typeExtra = $type->extras()->create([
|
||||
'nombre' => 'WhatsApp',
|
||||
'descripcion' => 'Configuracion del canal de WhatsApp',
|
||||
'is_required' => true,
|
||||
'config_schema' => [
|
||||
'type' => 'object',
|
||||
'required' => ['phone'],
|
||||
],
|
||||
]);
|
||||
$tenant = $this->createTenant();
|
||||
$tenant->websiteType()->associate($type)->save();
|
||||
$websiteExtra = $tenant->websiteExtras()->create([
|
||||
'website_type_extra_id' => $typeExtra->id,
|
||||
'config' => ['phone' => '+5491112345678'],
|
||||
]);
|
||||
|
||||
$this->assertTrue($type->extras()->firstOrFail()->is($typeExtra));
|
||||
$this->assertTrue($type->tenants()->firstOrFail()->is($tenant));
|
||||
$this->assertTrue($tenant->websiteType()->firstOrFail()->is($type));
|
||||
$this->assertSame($type->codigo, $tenant->website_type_code);
|
||||
$this->assertTrue($typeExtra->websiteType()->firstOrFail()->is($type));
|
||||
$this->assertTrue($typeExtra->websiteExtras()->firstOrFail()->is($websiteExtra));
|
||||
$this->assertTrue($websiteExtra->website()->firstOrFail()->is($tenant));
|
||||
$this->assertTrue($websiteExtra->websiteTypeExtra()->firstOrFail()->is($typeExtra));
|
||||
$this->assertTrue($typeExtra->is_required);
|
||||
$this->assertSame([
|
||||
'type' => 'object',
|
||||
'required' => ['phone'],
|
||||
], $typeExtra->config_schema);
|
||||
$this->assertSame(['phone' => '+5491112345678'], $websiteExtra->config);
|
||||
}
|
||||
|
||||
public function test_deleting_a_type_cascades_its_definitions_and_website_values(): void
|
||||
{
|
||||
$type = WebsiteType::query()->create([
|
||||
'codigo' => 'store',
|
||||
'nombre' => 'Tienda',
|
||||
]);
|
||||
$typeExtra = $type->extras()->create([
|
||||
'nombre' => 'WhatsApp',
|
||||
'descripcion' => 'Configuracion del canal de WhatsApp',
|
||||
'config_schema' => ['type' => 'object'],
|
||||
]);
|
||||
$websiteExtra = $this->createTenant()->websiteExtras()->create([
|
||||
'website_type_extra_id' => $typeExtra->id,
|
||||
'config' => ['phone' => '+5491112345678'],
|
||||
]);
|
||||
|
||||
$type->delete();
|
||||
|
||||
$this->assertDatabaseMissing('website_type_extras', ['id' => $typeExtra->id]);
|
||||
$this->assertDatabaseMissing('websites_extras', ['id' => $websiteExtra->id]);
|
||||
}
|
||||
|
||||
private function createTenant(): Tenant
|
||||
{
|
||||
return Tenant::query()->create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'success_color' => '#444444',
|
||||
'header_bg_color' => '#ffffff',
|
||||
'footer_bg_color' => '#ffffff',
|
||||
'header_logo_id' => 1,
|
||||
'footer_logo_id' => 2,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user