Просмотр исходного кода

feat: enhance getUserTypes method to support franchisor assignment and unit filtering

ebagabee 1 неделя назад
Родитель
Сommit
a07506f608
1 измененных файлов с 15 добавлено и 3 удалено
  1. 15 3
      app/Services/UserService.php

+ 15 - 3
app/Services/UserService.php

@@ -129,12 +129,24 @@ public function delete(int $id): bool
     public function getUserTypes(): \Illuminate\Database\Eloquent\Collection
     {
         $scope = request()->input('access_scope', Auth::user()?->access_scope);
-        $unitId = request()->input('active_unit_id') ?? Auth::user()?->units()->value('units.id');
+        $isFranchisorAssignment = request()->is('api/user/all/types');
+        $unitId = request()->integer('unit_id')
+            ?: request()->input('active_unit_id')
+            ?: Auth::user()?->units()->value('units.id');
 
         return \App\Models\UserType::query()
-            ->when($scope === 'unit', fn ($query) => $query->where('unit_id', $unitId))
+            ->when($scope === 'unit' && $unitId, fn ($query) => $query->where('unit_id', $unitId))
+            ->when($scope === 'unit' && !$unitId, fn ($query) => $query->where('system_key', 'ADMIN_FRANCHISEE'))
             ->when($scope === 'franchisor', fn ($query) => $query->whereNull('unit_id'))
-            ->where('is_system', false)
+            ->when(
+                $scope === 'unit' && $isFranchisorAssignment,
+                fn ($query) => $query->where(
+                    fn ($types) => $types
+                        ->where('is_system', false)
+                        ->orWhere('system_key', 'ADMIN_FRANCHISEE'),
+                ),
+                fn ($query) => $query->where('is_system', false),
+            )
             ->orderBy('label')
             ->get();
     }