فهرست منبع

feat: :sparkles: feat(carteirinha dependente) foi criada a opção de listagens dos dados do dependente

Implementada a disponibilização dos dados dos dependentes vinculados ao associado na resposta da API de usuário autenticado.

fase:dev | origin:escopo
kayo henrique 3 هفته پیش
والد
کامیت
fe8871a31e

+ 7 - 0
app/Http/Resources/AssociateValidationResource.php

@@ -20,6 +20,13 @@ class AssociateValidationResource extends JsonResource
                                 ? Storage::disk('s3')->temporaryUrl($this->photo_path, now()->addHours(24))
                                 : null,
             'expiry_date'  => $this->expiry_date?->format('d/m/Y'),
+
+            'dependents' => $this->dependents->map(function ($dependent) {
+                return [
+                    'name'    => $dependent->name,
+                    'kinship' => $dependent->kinship,
+                ];
+            })->values(),
         ];
     }
 }

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

@@ -35,15 +35,24 @@ class UserResource extends JsonResource
             'sector_id'      => $this->sector_id,
             'sector'         => $this->whenLoaded('sector', fn() => ['id' => $this->sector->id, 'name' => $this->sector->name]),
             'photo_url'                  => $this->photo_path
-                                                ? Storage::disk('s3')->temporaryUrl($this->photo_path, now()->addHours(24))
-                                                : null,
+                ? Storage::disk('s3')->temporaryUrl($this->photo_path, now()->addHours(24))
+                : null,
+
+            'dependents' => $this->whenLoaded('dependents', function () {
+                return $this->dependents->map(function ($dependent) {
+                    return [
+                        'name' => $dependent->name,
+                        'kinship' => $dependent->kinship,
+                    ];
+                })->values();
+            }),
             'first_access_completed_at'  => $this->first_access_completed_at?->format('Y-m-d H:i:s'),
             'terms_accepted_at'          => $this->terms_accepted_at?->format('Y-m-d H:i:s'),
             'on_leave_at'                => $this->on_leave_at?->format('Y-m-d H:i:s'),
             'on_leave_by_user_id'        => $this->on_leave_by_user_id,
             'first_access_at'            => $this->access_logs_min_accessed_at
-                                                ? Carbon::parse($this->access_logs_min_accessed_at)->format('Y-m-d H:i:s')
-                                                : null,
+                ? Carbon::parse($this->access_logs_min_accessed_at)->format('Y-m-d H:i:s')
+                : null,
             'unread_notifications_count' => (int) ($this->unread_notifications_count ?? 0),
             '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'),

+ 7 - 3
app/Services/AssociateValidationService.php

@@ -9,11 +9,15 @@ class AssociateValidationService
 {
     public function validate(array $data): ?User
     {
-      $search = $data['search'] ?? null;
-      $cpfSearch = Cpf::digits($search);
+        $search = $data['search'] ?? null;
+        $cpfSearch = Cpf::digits($search);
 
         return User::query()
-
+            ->with([
+                'dependents' => function ($query) {
+                    $query->where('status', 'active');
+                },
+            ])
             ->where(function ($query) use ($data, $search, $cpfSearch) {
                 if (!empty($search)) {
                     $query->where(function ($query) use ($search, $cpfSearch) {

+ 14 - 7
app/Services/UserService.php

@@ -24,8 +24,15 @@ class UserService
             $user->loadMissing('partnerAgreement');
         }
 
-        return $user->load(['position', 'sector'])
-                    ->loadCount(['notificationSends as unread_notifications_count' => fn($q) => $q->where('read', false)]);
+        return $user->load([
+            'position',
+            'sector',
+            'dependents' => function ($query) {
+                $query->where('status', 'approved');
+            },
+        ])->loadCount([
+            'notificationSends as unread_notifications_count' => fn($q) => $q->where('read', false),
+        ]);
     }
 
     public function getAll(): Collection
@@ -36,9 +43,9 @@ class UserService
     public function getAssociadosForSelect(?string $search, int $perPage): \Illuminate\Pagination\LengthAwarePaginator
     {
         return $this->baseQuery([
-                'type'   => UserTypeEnum::ASSOCIADO->value,
-                'search' => $search,
-            ])
+            'type'   => UserTypeEnum::ASSOCIADO->value,
+            'search' => $search,
+        ])
             ->where('status', UserStatusEnum::ACTIVE)
             ->orderBy('name')
             ->paginate($perPage, ['id', 'name', 'registration']);
@@ -96,8 +103,8 @@ class UserService
 
             $query->where(function ($q) use ($term, $cpfTerm) {
                 $q->whereRaw('UNACCENT(LOWER(name)) LIKE UNACCENT(?)', [$term])
-                  ->orWhereRaw('UNACCENT(LOWER(email)) LIKE UNACCENT(?)', [$term])
-                  ->orWhereRaw('UNACCENT(LOWER(COALESCE(registration, \'\'))) LIKE UNACCENT(?)', [$term]);
+                    ->orWhereRaw('UNACCENT(LOWER(email)) LIKE UNACCENT(?)', [$term])
+                    ->orWhereRaw('UNACCENT(LOWER(COALESCE(registration, \'\'))) LIKE UNACCENT(?)', [$term]);
 
                 if ($cpfTerm) {
                     $q->orWhere('cpf', 'LIKE', $cpfTerm);