mandant-crm/backend/app/Modules/CustomField/Actions/CreateCustomFieldDefinitionAction.php
Tim Stollberg f0bb80707a feat(backend): commit uncommitted CustomField, dynamic HistoryType, and Import modules
Mirrors the frontend catch-up in 570f17e: a substantial amount of
finished-but-never-committed backend work had accumulated locally.

- New CustomField module: admin-managed field definitions per client,
  reorderable, typed values stored per client (backing the frontend's
  ClientCustomFieldCard + settings/fields page)
- History entries switch from a hardcoded type enum to a dynamic,
  admin-managed HistoryType module (reorderable, backing
  settings/history-types)
- New Import module: CSV client import (backing settings/import)
- Clients table gains soft deletes (deleted_at)
- Supporting factories, seeders, policies, and feature tests for all
  of the above

All 63 backend tests pass locally before this commit.
2026-07-18 16:34:18 +07:00

37 lines
1.2 KiB
PHP

<?php
namespace App\Modules\CustomField\Actions;
use App\Modules\CustomField\DTOs\CustomFieldDefinitionData;
use App\Modules\CustomField\Models\CustomFieldDefinition;
use App\Modules\CustomField\Models\CustomFieldType;
use Illuminate\Support\Str;
final class CreateCustomFieldDefinitionAction
{
public function handle(CustomFieldDefinitionData $data): CustomFieldDefinition
{
return CustomFieldDefinition::create([
'label' => $data->label,
'key' => $this->uniqueKey($data->label),
'type' => $data->type,
'options' => $data->type === CustomFieldType::Select->value ? array_values($data->options ?? []) : null,
'is_required' => $data->isRequired,
// New entries always land at the end of the list.
'sort_order' => (CustomFieldDefinition::max('sort_order') ?? -1) + 1,
]);
}
private function uniqueKey(string $label): string
{
$base = Str::slug($label, '_') ?: 'field';
$key = $base;
$i = 1;
while (CustomFieldDefinition::withTrashed()->where('key', $key)->exists()) {
$key = $base.'_'.(++$i);
}
return $key;
}
}