feat(tenant): separate admin and storefront website types

This commit is contained in:
2026-09-17 14:25:46 -03:00
parent b9b5f8ec9c
commit 6bc784519f
31 changed files with 325 additions and 143 deletions

View File

@@ -4,11 +4,11 @@ namespace App\Domains\Tenant\Services;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Attachable\Services\AttachmentService;
use App\Domains\Tenant\Models\WebsiteType;
use App\Domains\Tenant\Models\AdminWebsiteType;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class WebsiteTypeService
class AdminWebsiteTypeService
{
public function __construct(
protected AttachmentService $attachmentService,
@@ -19,9 +19,9 @@ class WebsiteTypeService
*
* @param array<string, mixed> $data
*/
public function create(array $data): WebsiteType
public function create(array $data): AdminWebsiteType
{
return $this->save(new WebsiteType, $data);
return $this->save(new AdminWebsiteType, $data);
}
/**
@@ -30,10 +30,10 @@ class WebsiteTypeService
* @param array<string, mixed> $attributes
* @param array<string, mixed> $values
*/
public function updateOrCreate(array $attributes, array $values = []): WebsiteType
public function updateOrCreate(array $attributes, array $values = []): AdminWebsiteType
{
/** @var WebsiteType $websiteType */
$websiteType = WebsiteType::query()->firstOrNew($attributes);
/** @var AdminWebsiteType $websiteType */
$websiteType = AdminWebsiteType::query()->firstOrNew($attributes);
return $this->save($websiteType, [...$attributes, ...$values]);
}
@@ -41,9 +41,9 @@ class WebsiteTypeService
/**
* @param array<string, mixed> $data
*/
private function save(WebsiteType $websiteType, array $data): WebsiteType
private function save(AdminWebsiteType $websiteType, array $data): AdminWebsiteType
{
return DB::transaction(function () use ($websiteType, $data): WebsiteType {
return DB::transaction(function () use ($websiteType, $data): AdminWebsiteType {
$previousLogos = [];
$attachmentFields = [
@@ -68,7 +68,7 @@ class WebsiteTypeService
if ($logo) {
$attachment = is_string($logo) && Str::isUuid($logo)
? Attachment::query()->where('key', $logo)->first()
: $this->attachmentService->store($logo, 'website-types');
: $this->attachmentService->store($logo, 'admin-website-types');
}
$data[$attachmentField['column']] = $attachment?->id;
@@ -82,7 +82,7 @@ class WebsiteTypeService
&& $previousLogo->id !== $websiteType->site_logo
&& $previousLogo->id !== $websiteType->footer_logo
&& $previousLogo->id !== $websiteType->favicon_id
&& ! $this->isReferencedByWebsiteType($previousLogo)
&& ! $this->isReferencedByBrand($previousLogo)
) {
$this->attachmentService->delete($previousLogo);
}
@@ -92,9 +92,10 @@ class WebsiteTypeService
});
}
private function isReferencedByWebsiteType(Attachment $attachment): bool
private function isReferencedByBrand(Attachment $attachment): bool
{
return WebsiteType::query()
return Tenant::query()->where('favicon_id', $attachment->id)->exists()
|| AdminWebsiteType::query()
->where(function ($query) use ($attachment): void {
$query
->where('site_logo', $attachment->id)

View File

@@ -14,7 +14,6 @@ class TenantInformationService
'headerLogo',
'footerLogo',
'favicon',
'websiteType.favicon',
'headerBackgroundImage',
'footerBackgroundImage',
'socialMedia',

View File

@@ -9,8 +9,8 @@ use App\Domains\Shared\Rules\CroppedImageOrBase64Rule;
use App\Domains\Shared\Rules\ImageOrBase64Rule;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteExtra;
use App\Domains\Tenant\Models\WebsiteType;
use App\Domains\Tenant\Models\WebsiteTypeExtra;
use App\Domains\Tenant\Models\StorefrontWebsiteType;
use App\Domains\Tenant\Models\StorefrontWebsiteTypeExtra;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
@@ -37,7 +37,7 @@ class WebsiteExtraService
$definitions = $this->definitionsFor($websiteTypeCode);
$allowedCodes = $definitions->pluck('codigo')->all();
$hasRequiredExtras = $definitions->contains(
fn (WebsiteTypeExtra $definition): bool => $definition->is_required
fn (StorefrontWebsiteTypeExtra $definition): bool => $definition->is_required
);
$rules = [
@@ -95,11 +95,11 @@ class WebsiteExtraService
return;
}
$definitions = $this->definitionsFor((string) $tenant->website_type_code)
$definitions = $this->definitionsFor((string) $tenant->storefront_website_type_code)
->keyBy('codigo');
foreach ($extras as $name => $config) {
/** @var WebsiteTypeExtra|null $definition */
/** @var StorefrontWebsiteTypeExtra|null $definition */
$definition = $definitions->get($name);
if (! $definition) {
@@ -193,20 +193,20 @@ class WebsiteExtraService
});
}
public function definitionForTenant(Tenant $tenant, string $extraCode): WebsiteTypeExtra
public function definitionForTenant(Tenant $tenant, string $extraCode): StorefrontWebsiteTypeExtra
{
return WebsiteTypeExtra::query()
->where('website_type_code', $tenant->website_type_code)
return StorefrontWebsiteTypeExtra::query()
->where('storefront_website_type_code', $tenant->storefront_website_type_code)
->where('codigo', $extraCode)
->firstOrFail();
}
/**
* @return Collection<int, WebsiteTypeExtra>
* @return Collection<int, StorefrontWebsiteTypeExtra>
*/
private function definitionsFor(string $websiteTypeCode): Collection
{
$websiteType = WebsiteType::query()
$websiteType = StorefrontWebsiteType::query()
->where('codigo', $websiteTypeCode)
->with('extras')
->first();
@@ -252,7 +252,7 @@ class WebsiteExtraService
private function applyTransforms(
Tenant $tenant,
WebsiteTypeExtra $definition,
StorefrontWebsiteTypeExtra $definition,
mixed $config,
string $requestRoot
): mixed {
@@ -318,7 +318,7 @@ class WebsiteExtraService
*/
private function transformValue(
Tenant $tenant,
WebsiteTypeExtra $definition,
StorefrontWebsiteTypeExtra $definition,
string $path,
mixed $value,
array $transform,