Commit ecf6909c authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix UserForm to detect existing entities and repair orphan role states

- Detect on mount if user already has Trainer/Employee/Guardian records
- Show green notice when record exists, amber warning when role implies
  record but none exists (orphan state)
- Allow entity creation on save even without role change when entities
  are missing (repair orphaned users from before the redesign)
- Disable create toggles when record already exists to prevent duplicates
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent c000ea07
......@@ -63,6 +63,10 @@ class UserForm extends Component
// ─── State Tracking ──────────────────────────────────────────
public bool $roleChanged = false;
public bool $hasExistingTrainer = false;
public bool $hasExistingEmployee = false;
public bool $hasExistingGuardian = false;
public bool $missingRoleEntities = false;
public function mount(?User $user = null): void
{
......@@ -77,12 +81,49 @@ public function mount(?User $user = null): void
$this->person_id = $user->person_id;
$this->status = $user->status ?? 'active';
$this->resolveRoleSlug();
$this->detectExistingEntities();
} else {
$this->authorize('users.create');
$this->employeeStartDate = now()->toDateString();
}
}
private function detectExistingEntities(): void
{
$person = $this->user->person;
if ($person) {
$this->hasExistingEmployee = Employee::where('person_id', $person->id)->exists();
$this->hasExistingTrainer = Trainer::where('person_id', $person->id)
->orWhere(function ($q) use ($person) {
$employee = Employee::where('person_id', $person->id)->first();
if ($employee) {
$q->where('employee_id', $employee->id);
}
})->exists();
$this->hasExistingGuardian = Guardian::where('person_id', $person->id)->exists();
}
// Detect orphan state: role says trainer but no trainer record
if ($this->shouldShowTrainerSection() && !$this->hasExistingTrainer) {
$this->missingRoleEntities = true;
$this->createTrainer = true;
} elseif ($this->shouldShowTrainerSection()) {
$this->createTrainer = false;
}
if ($this->shouldShowGuardianSection() && !$this->hasExistingGuardian) {
$this->missingRoleEntities = true;
}
if ($this->shouldShowEmployeeSection() && !$this->hasExistingEmployee) {
$this->missingRoleEntities = true;
$this->createEmployee = true;
} elseif ($this->shouldShowEmployeeSection()) {
$this->createEmployee = false;
}
}
public function updatedRoleId(): void
{
$this->resolveRoleSlug();
......@@ -263,8 +304,8 @@ private function needsPersonRecord(): bool
private function handleRoleEntities(User $user, ?Person $person, int $academyId, User $actor): void
{
// Only process for new users or role changes
if ($this->editing && ! $this->roleChanged) {
// Process for: new users, role changes, or missing entity repair
if ($this->editing && ! $this->roleChanged && ! $this->missingRoleEntities) {
return;
}
......
......@@ -97,9 +97,22 @@ class="bg-white rounded-xl shadow-sm border border-emerald-200 p-4 sm:p-6">
</button>
<div x-show="trainerOpen" x-transition class="mt-4">
@if($editing && $hasExistingTrainer)
<div class="mb-4 p-3 bg-emerald-50 rounded-lg border border-emerald-200">
<p class="text-sm text-emerald-800 font-medium">✓ {{ __('يوجد سجل مدرب مربوط بهذا المستخدم') }}</p>
<p class="text-xs text-emerald-600 mt-1">{{ __('يمكنك تعديل بيانات المدرب من صفحة المدربين') }}</p>
</div>
@elseif($editing && $missingRoleEntities && $this->shouldShowTrainerSection())
<div class="mb-4 p-3 bg-amber-50 rounded-lg border border-amber-200">
<p class="text-sm text-amber-800 font-medium">⚠️ {{ __('هذا المستخدم له دور مدرب لكن لا يوجد سجل مدرب') }}</p>
<p class="text-xs text-amber-600 mt-1">{{ __('أكمل البيانات أدناه لإنشاء السجل المطلوب') }}</p>
</div>
@endif
{{-- Toggle: Create trainer record --}}
<label class="flex items-center gap-3 mb-4 cursor-pointer">
<input type="checkbox" wire:model.live="createTrainer" class="w-5 h-5 text-emerald-600 border-gray-300 rounded focus:ring-emerald-500">
<input type="checkbox" wire:model.live="createTrainer" class="w-5 h-5 text-emerald-600 border-gray-300 rounded focus:ring-emerald-500"
@if($editing && $hasExistingTrainer) disabled @endif>
<span class="text-sm font-medium text-gray-700">{{ __('إنشاء سجل مدرب؟') }}</span>
</label>
......@@ -167,9 +180,22 @@ class="bg-white rounded-xl shadow-sm border border-blue-200 p-4 sm:p-6">
</button>
<div x-show="employeeOpen" x-transition class="mt-4">
@if($editing && $hasExistingEmployee)
<div class="mb-4 p-3 bg-blue-50 rounded-lg border border-blue-200">
<p class="text-sm text-blue-800 font-medium">✓ {{ __('يوجد سجل موظف مربوط بهذا المستخدم') }}</p>
<p class="text-xs text-blue-600 mt-1">{{ __('يمكنك تعديل بيانات الموظف من صفحة الموظفين') }}</p>
</div>
@elseif($editing && $missingRoleEntities && $this->shouldShowEmployeeSection())
<div class="mb-4 p-3 bg-amber-50 rounded-lg border border-amber-200">
<p class="text-sm text-amber-800 font-medium">⚠️ {{ __('هذا المستخدم له دور موظف لكن لا يوجد سجل موظف') }}</p>
<p class="text-xs text-amber-600 mt-1">{{ __('أكمل البيانات أدناه لإنشاء السجل المطلوب') }}</p>
</div>
@endif
{{-- Toggle: Create employee record --}}
<label class="flex items-center gap-3 mb-4 cursor-pointer">
<input type="checkbox" wire:model.live="createEmployee" class="w-5 h-5 text-blue-600 border-gray-300 rounded focus:ring-blue-500">
<input type="checkbox" wire:model.live="createEmployee" class="w-5 h-5 text-blue-600 border-gray-300 rounded focus:ring-blue-500"
@if($editing && $hasExistingEmployee) disabled @endif>
<span class="text-sm font-medium text-gray-700">{{ __('إنشاء سجل موظف؟') }}</span>
</label>
......@@ -235,6 +261,18 @@ class="bg-white rounded-xl shadow-sm border border-amber-200 p-4 sm:p-6">
</button>
<div x-show="guardianOpen" x-transition class="mt-4">
@if($editing && $hasExistingGuardian)
<div class="mb-4 p-3 bg-amber-50 rounded-lg border border-amber-200">
<p class="text-sm text-amber-800 font-medium">✓ {{ __('يوجد سجل ولي أمر مربوط بهذا المستخدم') }}</p>
<p class="text-xs text-amber-600 mt-1">{{ __('يمكنك إدارة الأبناء المربوطين من صفحة أولياء الأمور') }}</p>
</div>
@elseif($editing && $missingRoleEntities && $this->shouldShowGuardianSection())
<div class="mb-4 p-3 bg-red-50 rounded-lg border border-red-200">
<p class="text-sm text-red-800 font-medium">⚠️ {{ __('هذا المستخدم له دور ولي أمر لكن لا يوجد سجل ولي أمر') }}</p>
<p class="text-xs text-red-600 mt-1">{{ __('أكمل البيانات أدناه لربط ولي الأمر') }}</p>
</div>
@endif
{{-- Mode selector --}}
<div class="flex gap-4 mb-4">
<label class="flex items-center gap-2 cursor-pointer">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment