Commit c22fdd7b authored by Mahmoud Aglan's avatar Mahmoud Aglan

Fix UsesBranchScope: handle missing branch gracefully, assign branch to admin

- UsesBranchScope falls back to withoutGlobalScopes if no branch found in current academy
- Shows proper error message instead of TypeError when no branch exists
- DatabaseSeeder assigns branch_id to admin user on every seed run
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 4cecc6ef
......@@ -16,9 +16,14 @@ public function getActiveBranchIdOrFail(): int
$id = $this->getActiveBranchId();
if (!$id) {
$id = Branch::where('is_active', true)->first()?->id;
if ($id) {
$branch = Branch::where('is_active', true)->first()
?? Branch::withoutGlobalScopes()->where('is_active', true)->first();
if ($branch) {
$id = $branch->id;
session(['active_branch_id' => $id]);
} else {
abort(403, 'لا يوجد فرع نشط. يرجى إنشاء فرع أولاً.');
}
}
......
......@@ -59,13 +59,17 @@ public function run(): void
->where('slug', 'academy_owner')
->first();
// Ensure admin has branch assigned
$branch = \App\Domain\Identity\Models\Branch::where('academy_id', $admin->academy_id)->first();
$branchId = $branch?->id ?? $admin->branch_id;
if ($ownerRole) {
$admin->update(['role_id' => $ownerRole->id, 'is_super_admin' => true]);
$admin->update(['role_id' => $ownerRole->id, 'is_super_admin' => true, 'branch_id' => $branchId]);
if (!$admin->roles()->where('role_id', $ownerRole->id)->exists()) {
$admin->roles()->attach($ownerRole->id);
}
} else {
$admin->update(['is_super_admin' => true]);
$admin->update(['is_super_admin' => true, 'branch_id' => $branchId]);
}
}
}
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