feat: implement tenant management system with attachment support and bootstrap controller

This commit is contained in:
2026-06-26 13:17:48 -03:00
parent 1f9d876bc3
commit 54aa696145
9 changed files with 198 additions and 52 deletions

View File

@@ -15,7 +15,7 @@ class BootstrapTenantController extends Controller
$dominio = $request->validated('dominio');
return TenantResource::make(
Tenant::query()->where('dominio', $dominio)->firstOrFail()
Tenant::query()->with(['headerLogo', 'footerLogo'])->where('dominio', $dominio)->firstOrFail()
);
}
}

View File

@@ -19,26 +19,26 @@ class TenantController extends Controller
public function index(): JsonResponse
{
return TenantResource::collection(Tenant::query()->latest()->get())->response();
return TenantResource::collection(Tenant::query()->with(['headerLogo', 'footerLogo'])->latest()->get())->response();
}
public function store(StoreTenantRequest $request): JsonResponse
{
$tenant = $this->tenantService->create($request->validated());
return TenantResource::make($tenant)->response()->setStatusCode(201);
return TenantResource::make($tenant->loadMissing(['headerLogo', 'footerLogo']))->response()->setStatusCode(201);
}
public function show(Tenant $tenant): TenantResource
{
return TenantResource::make($tenant);
return TenantResource::make($tenant->loadMissing(['headerLogo', 'footerLogo']));
}
public function update(UpdateTenantRequest $request, Tenant $tenant): TenantResource
{
$tenant = $this->tenantService->update($tenant, $request->validated());
return TenantResource::make($tenant);
return TenantResource::make($tenant->loadMissing(['headerLogo', 'footerLogo']));
}
public function destroy(Tenant $tenant): Response

View File

@@ -2,11 +2,13 @@
namespace App\Domains\Tenant\Models;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Attachable\Models\Concerns\HasAttachments;
use App\Domains\Catalog\Models\Product;
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([
@@ -17,14 +19,30 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
'secondary_color',
'danger_color',
'header_footer_bg_color',
'header_logo',
'footer_logo',
'header_logo_id',
'footer_logo_id',
])]
class Tenant extends Model
{
use HasAttachments;
use HasFactory;
/**
* @return BelongsTo<Attachment, $this>
*/
public function headerLogo(): BelongsTo
{
return $this->belongsTo(Attachment::class, 'header_logo_id');
}
/**
* @return BelongsTo<Attachment, $this>
*/
public function footerLogo(): BelongsTo
{
return $this->belongsTo(Attachment::class, 'footer_logo_id');
}
/**
* @return HasMany<Product, $this>
*/

View File

@@ -24,8 +24,8 @@ class TenantResource extends JsonResource
'secondary_color' => $this->secondary_color,
'danger_color' => $this->danger_color,
'header_footer_bg_color' => $this->header_footer_bg_color,
'header_logo' => $this->header_logo,
'footer_logo' => $this->footer_logo,
'header_logo' => $this->headerLogo?->getTemporaryUrl(),
'footer_logo' => $this->footerLogo?->getTemporaryUrl(),
];
}
}

View File

@@ -31,15 +31,25 @@ class TenantService
$tenant = Tenant::query()->create($data);
if ($headerLogo) {
$attachment = $this->attachmentService->store($headerLogo, 'tenants');
$tenant->header_logo = $attachment->key;
$tenant->attachments()->attach($attachment->id);
$attachment = Str::isUuid($headerLogo)
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $headerLogo)->first()
: $this->attachmentService->store($headerLogo, 'tenants');
if ($attachment) {
$tenant->header_logo_id = $attachment->id;
$tenant->attachments()->attach($attachment->id);
}
}
if ($footerLogo) {
$attachment = $this->attachmentService->store($footerLogo, 'tenants');
$tenant->footer_logo = $attachment->key;
$tenant->attachments()->attach($attachment->id);
$attachment = Str::isUuid($footerLogo)
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $footerLogo)->first()
: $this->attachmentService->store($footerLogo, 'tenants');
if ($attachment) {
$tenant->footer_logo_id = $attachment->id;
$tenant->attachments()->attach($attachment->id);
}
}
if ($headerLogo || $footerLogo) {
@@ -71,29 +81,35 @@ class TenantService
if ($hasHeaderLogoKey) {
if ($headerLogo) {
if (! Str::isUuid($headerLogo)) {
$attachment = $this->attachmentService->store($headerLogo, 'tenants');
$tenant->header_logo = $attachment->key;
$attachment = Str::isUuid($headerLogo)
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $headerLogo)->first()
: $this->attachmentService->store($headerLogo, 'tenants');
if ($attachment) {
$tenant->header_logo_id = $attachment->id;
$tenant->attachments()->attach($attachment->id);
} else {
$tenant->header_logo = $headerLogo;
$tenant->header_logo_id = null;
}
} else {
$tenant->header_logo = null;
$tenant->header_logo_id = null;
}
}
if ($hasFooterLogoKey) {
if ($footerLogo) {
if (! Str::isUuid($footerLogo)) {
$attachment = $this->attachmentService->store($footerLogo, 'tenants');
$tenant->footer_logo = $attachment->key;
$attachment = Str::isUuid($footerLogo)
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $footerLogo)->first()
: $this->attachmentService->store($footerLogo, 'tenants');
if ($attachment) {
$tenant->footer_logo_id = $attachment->id;
$tenant->attachments()->attach($attachment->id);
} else {
$tenant->footer_logo = $footerLogo;
$tenant->footer_logo_id = null;
}
} else {
$tenant->footer_logo = null;
$tenant->footer_logo_id = null;
}
}