Przeglądaj źródła

feat: adiciona layout para adicionar usuarios a unidade

ebagabee 1 miesiąc temu
rodzic
commit
fdb997b7da

+ 9 - 0
app/Http/Controllers/UserController.php

@@ -28,6 +28,15 @@ public function index(): JsonResponse
         );
     }
 
+    public function indexByUnit(): JsonResponse
+    {
+        $items = $this->service->getAllByUnit();
+
+        return $this->successResponse(
+            payload: UserResource::collection($items),
+        );
+    }
+
     public function store(UserRequest $request): JsonResponse
     {
         $item = $this->service->create($request->validated());

+ 4 - 3
app/Http/Resources/UserResource.php

@@ -18,7 +18,8 @@ public function toArray(Request $request): array
             'cpf'           => $this->cpf,
             'email'         => $this->email,
             'language'      => $this->language,
-            'user_type'     => $this->user_type,
+            'user_type'       => $this->user_type,
+            'user_type_label' => $this->user_type?->label(),
             'status'        => $this->status,
             'state_id'      => $this->state_id,
             'unit_id'       => $this->whenLoaded('units', fn() => $this->units->first()?->id),
@@ -28,8 +29,8 @@ public function toArray(Request $request): array
             'last_login_at' => $this->last_login_at
                 ? Carbon::parse($this->last_login_at)->format('Y-m-d H:i:s')
                 : null,
-            'created_at'    => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
-            'updated_at'    => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
+            'created_at'    => Carbon::parse($this->created_at)->format('Y-m-d'),
+            'updated_at'    => Carbon::parse($this->updated_at)->format('Y-m-d'),
 
             'state' => $this->whenLoaded('state', fn() => [
                 'id'   => $this->state->id,

+ 14 - 0
app/Services/UserService.php

@@ -23,6 +23,20 @@ public function getAll(): Collection
         return User::with(['state', 'units'])->orderBy('name')->get();
     }
 
+    public function getAllByUnit(): Collection
+    {
+        $unitId = Auth::user()->units->first()?->id;
+
+        if (!$unitId) {
+            return collect();
+        }
+
+        return User::with(['state', 'units'])
+            ->whereHas('units', fn($q) => $q->where('units.id', $unitId))
+            ->orderBy('name')
+            ->get();
+    }
+
     public function findById(int $id): ?User
     {
         return User::with(['state', 'units'])->find($id);

+ 2 - 0
routes/authRoutes/user.php

@@ -8,6 +8,8 @@
 
     Route::put('/me', 'updateMe');
 
+    Route::get('/unit', 'indexByUnit');
+
     Route::get('/', 'index')
         ->middleware('permission:config.user,view');