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

Merge branch 'feature/serprati-kay-carteirinha-dependente' of Softpar/sfp_api_laravel_serprati into development

zntt 2 недель назад
Родитель
Сommit
8513ceb886

+ 13 - 3
app/Http/Resources/AssociateValidationResource.php

@@ -17,9 +17,19 @@ class AssociateValidationResource extends JsonResource
             'cpf'          => $this->cpf,
             'registration' => $this->registration,
             '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,
             'expiry_date'  => $this->expiry_date?->format('d/m/Y'),
+
+            'dependents' => $this->dependents->map(function ($dependent) {
+                return [
+                    'id'          => $dependent->id,
+                    'name'        => $dependent->name,
+                    'kinship'     => $dependent->kinship,
+                    'status'      => $dependent->status,
+                    'expiry_date' => $this->expiry_date?->format('d/m/Y'),
+                ];
+            })->values(),
         ];
     }
-}
+}

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

@@ -35,15 +35,23 @@ 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->dependents->map(fn($dependent) => [
+                'id' => $dependent->id,
+                'name' => $dependent->name,
+                'kinship' => $dependent->kinship,
+                'status' => $dependent->status,
+                'expiry_date' => $this->expiry_date?->format('d/m/Y'),
+            ]),
             '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) {

+ 6 - 7
app/Services/UserService.php

@@ -24,8 +24,7 @@ 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',])->loadCount(['notificationSends as unread_notifications_count' => fn($q) => $q->where('read', false),]);
     }
 
     public function getAll(): Collection
@@ -36,9 +35,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 +95,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);