feat(tenant): implement TenantProvisioningService and OnTicketTenantSeeder for tenant management
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
|
||||
namespace App\Domains\Core\Tenant\Services;
|
||||
|
||||
use App\Domains\Core\Tenant\Models\AdminWebsiteType;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use App\Shared\Attachable\Services\AttachmentService;
|
||||
use App\Domains\Core\Tenant\Models\AdminWebsiteType;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
@@ -96,12 +97,12 @@ class AdminWebsiteTypeService
|
||||
{
|
||||
return Tenant::query()->where('favicon_id', $attachment->id)->exists()
|
||||
|| AdminWebsiteType::query()
|
||||
->where(function ($query) use ($attachment): void {
|
||||
$query
|
||||
->where('site_logo', $attachment->id)
|
||||
->orWhere('footer_logo', $attachment->id)
|
||||
->orWhere('favicon_id', $attachment->id);
|
||||
})
|
||||
->exists();
|
||||
->where(function ($query) use ($attachment): void {
|
||||
$query
|
||||
->where('site_logo', $attachment->id)
|
||||
->orWhere('footer_logo', $attachment->id)
|
||||
->orWhere('favicon_id', $attachment->id);
|
||||
})
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
|
||||
125
app/Domains/Core/Tenant/Services/TenantProvisioningService.php
Normal file
125
app/Domains/Core/Tenant/Services/TenantProvisioningService.php
Normal file
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Core\Tenant\Services;
|
||||
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Shared\Attachable\Models\Attachment;
|
||||
use App\Shared\Attachable\Services\AttachmentService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Internal provisioning used by migrations and seeders.
|
||||
*
|
||||
* General tenant writes are intentionally not exposed through the HTTP API.
|
||||
*/
|
||||
class TenantProvisioningService
|
||||
{
|
||||
public function __construct(
|
||||
protected AttachmentService $attachmentService,
|
||||
protected WebsiteExtraService $websiteExtraService,
|
||||
) {}
|
||||
|
||||
/** @param array<string, mixed> $data */
|
||||
public function create(array $data): Tenant
|
||||
{
|
||||
return DB::transaction(function () use ($data): Tenant {
|
||||
$images = [
|
||||
'header_logo' => $data['header_logo'] ?? null,
|
||||
'footer_logo' => $data['footer_logo'] ?? null,
|
||||
'favicon' => $data['favicon'] ?? null,
|
||||
'header_bg_image' => $data['header_bg_image'] ?? null,
|
||||
'footer_bg_image' => $data['footer_bg_image'] ?? null,
|
||||
];
|
||||
$socialMedia = $data['social_media'] ?? [];
|
||||
$extras = $data['extras'] ?? [];
|
||||
|
||||
unset(
|
||||
$data['header_logo'],
|
||||
$data['footer_logo'],
|
||||
$data['favicon'],
|
||||
$data['header_bg_image'],
|
||||
$data['footer_bg_image'],
|
||||
$data['social_media'],
|
||||
$data['extras'],
|
||||
);
|
||||
|
||||
foreach ($images as $key => $image) {
|
||||
$data[$key.'_id'] = $this->storeTenantImage($image);
|
||||
}
|
||||
|
||||
/** @var Tenant $tenant */
|
||||
$tenant = Tenant::query()->create($data);
|
||||
$this->syncSocialMedia($tenant, $socialMedia);
|
||||
|
||||
foreach ($extras as $code => $config) {
|
||||
$this->websiteExtraService->updateForTenant($tenant, $code, $config);
|
||||
}
|
||||
|
||||
return $tenant;
|
||||
});
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $data */
|
||||
public function update(Tenant $tenant, array $data): Tenant
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $data): Tenant {
|
||||
$imageKeys = [
|
||||
'header_logo',
|
||||
'footer_logo',
|
||||
'favicon',
|
||||
'header_bg_image',
|
||||
'footer_bg_image',
|
||||
];
|
||||
$hasSocialMedia = array_key_exists('social_media', $data);
|
||||
$socialMedia = $data['social_media'] ?? [];
|
||||
unset($data['social_media']);
|
||||
|
||||
foreach ($imageKeys as $key) {
|
||||
if (! array_key_exists($key, $data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data[$key.'_id'] = $this->storeTenantImage($data[$key]);
|
||||
unset($data[$key]);
|
||||
}
|
||||
|
||||
$tenant->update($data);
|
||||
|
||||
if ($hasSocialMedia) {
|
||||
$this->syncSocialMedia($tenant, $socialMedia);
|
||||
}
|
||||
|
||||
return $tenant->refresh();
|
||||
});
|
||||
}
|
||||
|
||||
/** @param array<int, array{code: string, url: string, orden?: int}> $socialMedia */
|
||||
private function syncSocialMedia(Tenant $tenant, array $socialMedia): void
|
||||
{
|
||||
$associations = [];
|
||||
|
||||
foreach (array_values($socialMedia) as $index => $item) {
|
||||
$associations[$item['code']] = [
|
||||
'url' => $item['url'],
|
||||
'orden' => $item['orden'] ?? $index,
|
||||
];
|
||||
}
|
||||
|
||||
$tenant->socialMedia()->sync($associations);
|
||||
$tenant->unsetRelation('socialMedia');
|
||||
}
|
||||
|
||||
private function storeTenantImage(mixed $image): ?int
|
||||
{
|
||||
if (! $image) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$attachment = is_string($image) && Str::isUuid($image)
|
||||
? Attachment::query()->where('key', $image)->first()
|
||||
: $this->attachmentService->store($image, 'tenants');
|
||||
|
||||
return $attachment?->id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user