diff --git a/app/Domains/Tenant/Models/WebsiteType.php b/app/Domains/Tenant/Models/WebsiteType.php index e5f877c..8bc62b7 100644 --- a/app/Domains/Tenant/Models/WebsiteType.php +++ b/app/Domains/Tenant/Models/WebsiteType.php @@ -2,14 +2,21 @@ namespace App\Domains\Tenant\Models; +use App\Domains\Attachable\Models\Attachment; 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([ 'codigo', 'nombre', + 'primary_color', + 'secondary_color', + 'danger_color', + 'success_color', + 'site_logo', ])] class WebsiteType extends Model { @@ -17,6 +24,14 @@ class WebsiteType extends Model protected $table = 'website_type'; + /** + * @return BelongsTo + */ + public function siteLogo(): BelongsTo + { + return $this->belongsTo(Attachment::class, 'site_logo'); + } + /** * @return HasMany */ diff --git a/app/Domains/Tenant/Resources/TenantResource.php b/app/Domains/Tenant/Resources/TenantResource.php index 7a2d24e..5c98f9e 100644 --- a/app/Domains/Tenant/Resources/TenantResource.php +++ b/app/Domains/Tenant/Resources/TenantResource.php @@ -37,6 +37,11 @@ class TenantResource extends JsonResource fn () => $this->websiteType ? [ 'codigo' => $this->websiteType->codigo, 'nombre' => $this->websiteType->nombre, + 'primary_color' => $this->websiteType->primary_color, + 'secondary_color' => $this->websiteType->secondary_color, + 'danger_color' => $this->websiteType->danger_color, + 'success_color' => $this->websiteType->success_color, + 'site_logo' => $this->websiteType->siteLogo?->getTemporaryUrl(1440), ] : null ), 'extras' => $this->whenLoaded( diff --git a/app/Domains/Tenant/Services/TenantInformationService.php b/app/Domains/Tenant/Services/TenantInformationService.php index ed64dd9..11826bb 100644 --- a/app/Domains/Tenant/Services/TenantInformationService.php +++ b/app/Domains/Tenant/Services/TenantInformationService.php @@ -14,7 +14,7 @@ class TenantInformationService 'headerLogo', 'footerLogo', 'socialMedia', - 'websiteType', + 'websiteType.siteLogo', 'websiteExtras.websiteTypeExtra', ]; diff --git a/app/Domains/Tenant/Services/WebsiteTypeService.php b/app/Domains/Tenant/Services/WebsiteTypeService.php new file mode 100644 index 0000000..cc0a49e --- /dev/null +++ b/app/Domains/Tenant/Services/WebsiteTypeService.php @@ -0,0 +1,49 @@ + $data + */ + public function create(array $data): WebsiteType + { + return DB::transaction(function () use ($data): WebsiteType { + $siteLogo = $data['site_logo'] ?? null; + + unset($data['site_logo']); + + $siteLogoAttachmentId = null; + + if ($siteLogo) { + $attachment = Str::isUuid($siteLogo) + ? Attachment::query()->where('key', $siteLogo)->first() + : $this->attachmentService->store($siteLogo, 'website-types'); + + if ($attachment) { + $siteLogoAttachmentId = $attachment->id; + } + } + + $data['site_logo'] = $siteLogoAttachmentId; + + /** @var WebsiteType $websiteType */ + $websiteType = WebsiteType::query()->create($data); + + return $websiteType; + }); + } +} diff --git a/database/migrations/2026_07_31_000500_add_presentation_fields_to_website_type_table.php b/database/migrations/2026_07_31_000500_add_presentation_fields_to_website_type_table.php new file mode 100644 index 0000000..2127445 --- /dev/null +++ b/database/migrations/2026_07_31_000500_add_presentation_fields_to_website_type_table.php @@ -0,0 +1,41 @@ +string('primary_color')->nullable()->after('nombre'); + $table->string('secondary_color')->nullable()->after('primary_color'); + $table->string('danger_color')->nullable()->after('secondary_color'); + $table->string('success_color')->nullable()->after('danger_color'); + $table->unsignedBigInteger('site_logo')->nullable()->after('success_color'); + + $table->foreign('site_logo') + ->references('id') + ->on('attachments') + ->nullOnDelete(); + }); + } + + public function down(): void + { + Schema::table('website_type', function (Blueprint $table): void { + if (Schema::getConnection()->getDriverName() !== 'sqlite') { + $table->dropForeign(['site_logo']); + } + + $table->dropColumn([ + 'primary_color', + 'secondary_color', + 'danger_color', + 'success_color', + 'site_logo', + ]); + }); + } +}; diff --git a/tests/Feature/Tenant/WebsiteExtrasTest.php b/tests/Feature/Tenant/WebsiteExtrasTest.php index 2770d16..6ddfded 100644 --- a/tests/Feature/Tenant/WebsiteExtrasTest.php +++ b/tests/Feature/Tenant/WebsiteExtrasTest.php @@ -2,6 +2,8 @@ namespace Tests\Feature\Tenant; +use App\Domains\Attachable\Enums\AttachmentType; +use App\Domains\Attachable\Models\Attachment; use App\Domains\Tenant\Models\Tenant; use App\Domains\Tenant\Models\WebsiteType; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -20,6 +22,11 @@ class WebsiteExtrasTest extends TestCase 'id', 'codigo', 'nombre', + 'primary_color', + 'secondary_color', + 'danger_color', + 'success_color', + 'site_logo', 'created_at', 'updated_at', ], Schema::getColumnListing('website_type')); @@ -49,9 +56,21 @@ class WebsiteExtrasTest extends TestCase public function test_models_map_the_diagram_relations_and_casts(): void { + $siteLogo = Attachment::query()->create([ + 'key' => '92bb3986-fb22-4871-b98c-fc5663d78aa6', + 'path' => 'website-types/site-logo.png', + 'filename' => 'site-logo.png', + 'type' => AttachmentType::Image, + 'mime_type' => 'image/png', + ]); $type = WebsiteType::query()->create([ 'codigo' => 'store', 'nombre' => 'Tienda', + 'primary_color' => '#111111', + 'secondary_color' => '#222222', + 'danger_color' => '#ff0000', + 'success_color' => '#00aa55', + 'site_logo' => $siteLogo->id, ]); $typeExtra = $type->extras()->create([ 'codigo' => 'whatsapp', @@ -72,6 +91,11 @@ class WebsiteExtrasTest extends TestCase ]); $this->assertTrue($type->extras()->firstOrFail()->is($typeExtra)); + $this->assertSame('#111111', $type->primary_color); + $this->assertSame('#222222', $type->secondary_color); + $this->assertSame('#ff0000', $type->danger_color); + $this->assertSame('#00aa55', $type->success_color); + $this->assertTrue($type->siteLogo()->firstOrFail()->is($siteLogo)); $this->assertSame($type->codigo, $typeExtra->website_type_code); $this->assertSame('whatsapp', $typeExtra->codigo); $this->assertTrue($type->tenants()->firstOrFail()->is($tenant));