feat: implement tenant management system including database schema, CRUD services, and bootstrap functionality
This commit is contained in:
105
app/Domains/Tenant/Services/TenantService.php
Normal file
105
app/Domains/Tenant/Services/TenantService.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Services;
|
||||
|
||||
use App\Domains\Attachable\Services\AttachmentService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class TenantService
|
||||
{
|
||||
public function __construct(protected AttachmentService $attachmentService)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new tenant and store its logos.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
* @return Tenant
|
||||
*/
|
||||
public function create(array $data): Tenant
|
||||
{
|
||||
return DB::transaction(function () use ($data): Tenant {
|
||||
$headerLogo = $data['header_logo'] ?? null;
|
||||
$footerLogo = $data['footer_logo'] ?? null;
|
||||
|
||||
unset($data['header_logo'], $data['footer_logo']);
|
||||
|
||||
/** @var Tenant $tenant */
|
||||
$tenant = Tenant::query()->create($data);
|
||||
|
||||
if ($headerLogo) {
|
||||
$attachment = $this->attachmentService->store($headerLogo, 'tenants');
|
||||
$tenant->header_logo = $attachment->key;
|
||||
$tenant->attachments()->attach($attachment->id);
|
||||
}
|
||||
|
||||
if ($footerLogo) {
|
||||
$attachment = $this->attachmentService->store($footerLogo, 'tenants');
|
||||
$tenant->footer_logo = $attachment->key;
|
||||
$tenant->attachments()->attach($attachment->id);
|
||||
}
|
||||
|
||||
if ($headerLogo || $footerLogo) {
|
||||
$tenant->save();
|
||||
}
|
||||
|
||||
return $tenant;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing tenant and store new logos if uploaded.
|
||||
*
|
||||
* @param Tenant $tenant
|
||||
* @param array<string, mixed> $data
|
||||
* @return Tenant
|
||||
*/
|
||||
public function update(Tenant $tenant, array $data): Tenant
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $data): Tenant {
|
||||
$hasHeaderLogoKey = array_key_exists('header_logo', $data);
|
||||
$hasFooterLogoKey = array_key_exists('footer_logo', $data);
|
||||
$headerLogo = $data['header_logo'] ?? null;
|
||||
$footerLogo = $data['footer_logo'] ?? null;
|
||||
|
||||
unset($data['header_logo'], $data['footer_logo']);
|
||||
|
||||
$tenant->fill($data);
|
||||
|
||||
if ($hasHeaderLogoKey) {
|
||||
if ($headerLogo) {
|
||||
if (! Str::isUuid($headerLogo)) {
|
||||
$attachment = $this->attachmentService->store($headerLogo, 'tenants');
|
||||
$tenant->header_logo = $attachment->key;
|
||||
$tenant->attachments()->attach($attachment->id);
|
||||
} else {
|
||||
$tenant->header_logo = $headerLogo;
|
||||
}
|
||||
} else {
|
||||
$tenant->header_logo = null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($hasFooterLogoKey) {
|
||||
if ($footerLogo) {
|
||||
if (! Str::isUuid($footerLogo)) {
|
||||
$attachment = $this->attachmentService->store($footerLogo, 'tenants');
|
||||
$tenant->footer_logo = $attachment->key;
|
||||
$tenant->attachments()->attach($attachment->id);
|
||||
} else {
|
||||
$tenant->footer_logo = $footerLogo;
|
||||
}
|
||||
} else {
|
||||
$tenant->footer_logo = null;
|
||||
}
|
||||
}
|
||||
|
||||
$tenant->save();
|
||||
|
||||
return $tenant;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user