feat(website-type): add new color fields and update related methods in WebsiteType model and service

This commit is contained in:
2026-07-31 12:55:40 -03:00
parent 6858b387c3
commit b478c23f3b
7 changed files with 164 additions and 18 deletions

View File

@@ -17,6 +17,12 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
'secondary_color',
'danger_color',
'success_color',
'warning_color',
'body_color',
'darker_body_color',
'surface_color',
'background_color',
'border_color',
'login_header_footer_color',
'site_logo',
])]

View File

@@ -21,27 +21,58 @@ class WebsiteTypeService
*/
public function create(array $data): WebsiteType
{
return DB::transaction(function () use ($data): WebsiteType {
return $this->save(new WebsiteType, $data);
}
/**
* Create or update a website type and replace its site logo when provided.
*
* @param array<string, mixed> $attributes
* @param array<string, mixed> $values
*/
public function updateOrCreate(array $attributes, array $values = []): WebsiteType
{
/** @var WebsiteType $websiteType */
$websiteType = WebsiteType::query()->firstOrNew($attributes);
return $this->save($websiteType, [...$attributes, ...$values]);
}
/**
* @param array<string, mixed> $data
*/
private function save(WebsiteType $websiteType, array $data): WebsiteType
{
return DB::transaction(function () use ($websiteType, $data): WebsiteType {
$hasSiteLogo = array_key_exists('site_logo', $data);
$siteLogo = $data['site_logo'] ?? null;
$previousSiteLogo = $websiteType->exists
? $websiteType->siteLogo()->first()
: null;
unset($data['site_logo']);
$siteLogoAttachmentId = null;
if ($hasSiteLogo) {
$attachment = null;
if ($siteLogo) {
$attachment = Str::isUuid($siteLogo)
? Attachment::query()->where('key', $siteLogo)->first()
: $this->attachmentService->store($siteLogo, 'website-types');
if ($attachment) {
$siteLogoAttachmentId = $attachment->id;
if ($siteLogo) {
$attachment = is_string($siteLogo) && Str::isUuid($siteLogo)
? Attachment::query()->where('key', $siteLogo)->first()
: $this->attachmentService->store($siteLogo, 'website-types');
}
$data['site_logo'] = $attachment?->id;
}
$data['site_logo'] = $siteLogoAttachmentId;
$websiteType->fill($data)->save();
/** @var WebsiteType $websiteType */
$websiteType = WebsiteType::query()->create($data);
if (
$hasSiteLogo
&& $previousSiteLogo instanceof Attachment
&& $previousSiteLogo->id !== $websiteType->site_logo
) {
$this->attachmentService->delete($previousSiteLogo);
}
return $websiteType;
});