Forráskód Böngészése

feat: :sparkles: feat (documento dependente) adicionado documento no dependente

foi adicionada a obrigatoriedade para preencher um documento ao adicionar dependente, que fica visualizavel para o adm aprovar/recusar

fase:dev | origin:escopo
Gustavo Zanatta 10 órája
szülő
commit
f9c8bbf1bb

+ 42 - 1
app/Http/Controllers/UserDependentController.php

@@ -7,13 +7,25 @@ use App\Http\Resources\UserDependentResource;
 use App\Services\UserDependentService;
 use Illuminate\Http\JsonResponse;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
 
 class UserDependentController extends Controller
 {
     public function __construct(protected UserDependentService $service) {}
 
+    private function deniesAccessToUser(int $userId): bool
+    {
+        $user = Auth::user();
+
+        return $user?->isAssociado() && $user->id !== $userId;
+    }
+
     public function indexByUser(int $userId): JsonResponse
     {
+        if ($this->deniesAccessToUser($userId)) {
+            return $this->errorResponse(message: __('messages.unauthorized'), code: 403);
+        }
+
         $items = $this->service->getAllByUser($userId);
         return $this->successResponse(payload: UserDependentResource::collection($items));
     }
@@ -34,7 +46,7 @@ class UserDependentController extends Controller
 
     public function store(UserDependentRequest $request): JsonResponse
     {
-        $item = $this->service->create($request->validated());
+        $item = $this->service->create($request->validated(), $request->file('document'));
         return $this->successResponse(
             payload: new UserDependentResource($item),
             message: __('messages.created'),
@@ -45,11 +57,30 @@ class UserDependentController extends Controller
     public function show(int $id): JsonResponse
     {
         $item = $this->service->findById($id);
+
+        if (!$item) {
+            return $this->errorResponse(message: __('messages.not_found'), code: 404);
+        }
+
+        if ($this->deniesAccessToUser($item->responsible_user_id)) {
+            return $this->errorResponse(message: __('messages.unauthorized'), code: 403);
+        }
+
         return $this->successResponse(payload: new UserDependentResource($item));
     }
 
     public function update(UserDependentRequest $request, int $id): JsonResponse
     {
+        $current = $this->service->findById($id);
+
+        if (!$current) {
+            return $this->errorResponse(message: __('messages.not_found'), code: 404);
+        }
+
+        if ($this->deniesAccessToUser($current->responsible_user_id)) {
+            return $this->errorResponse(message: __('messages.unauthorized'), code: 403);
+        }
+
         $item = $this->service->update($id, $request->validated());
         return $this->successResponse(
             payload: new UserDependentResource($item),
@@ -59,6 +90,16 @@ class UserDependentController extends Controller
 
     public function destroy(int $id): JsonResponse
     {
+        $current = $this->service->findById($id);
+
+        if (!$current) {
+            return $this->errorResponse(message: __('messages.not_found'), code: 404);
+        }
+
+        if ($this->deniesAccessToUser($current->responsible_user_id)) {
+            return $this->errorResponse(message: __('messages.unauthorized'), code: 403);
+        }
+
         $this->service->delete($id);
         return $this->successResponse(message: __('messages.deleted'), code: 204);
     }

+ 10 - 2
app/Http/Requests/UserDependentRequest.php

@@ -3,25 +3,33 @@
 namespace App\Http\Requests;
 
 use App\Enums\KinshipEnum;
-use App\Enums\UserDependentStatusEnum;
 use Illuminate\Foundation\Http\FormRequest;
 use Illuminate\Validation\Rule;
 
 class UserDependentRequest extends FormRequest
 {
+    protected function prepareForValidation(): void
+    {
+        $user = $this->user();
+
+        if ($user?->isAssociado()) {
+            $this->merge(['responsible_user_id' => $user->id]);
+        }
+    }
+
     public function rules(): array
     {
         $rules = [
             'responsible_user_id' => 'sometimes|integer|exists:users,id',
             'name'                => 'sometimes|string|max:255',
             'kinship'             => ['sometimes', Rule::enum(KinshipEnum::class)],
-            'status'              => ['sometimes', Rule::enum(UserDependentStatusEnum::class)],
         ];
 
         if ($this->isMethod('post')) {
             $rules['responsible_user_id'] = 'required|integer|exists:users,id';
             $rules['name']                = 'required|string|max:255';
             $rules['kinship']             = ['required', Rule::enum(KinshipEnum::class)];
+            $rules['document']            = 'required|file|mimes:jpg,jpeg,png,webp,pdf|max:10240';
         }
 
         return $rules;

+ 12 - 0
app/Http/Resources/UserDependentResource.php

@@ -5,6 +5,7 @@ namespace App\Http\Resources;
 use Carbon\Carbon;
 use Illuminate\Http\Request;
 use Illuminate\Http\Resources\Json\JsonResource;
+use Illuminate\Support\Facades\Storage;
 
 class UserDependentResource extends JsonResource
 {
@@ -16,6 +17,17 @@ class UserDependentResource extends JsonResource
             'name'                => $this->name,
             'kinship'             => $this->kinship,
             'status'              => $this->status,
+            'document_name'       => $this->document_name,
+            'document_is_image'   => $this->document_path
+                                        ? in_array(
+                                            strtolower(pathinfo($this->document_path, PATHINFO_EXTENSION)),
+                                            ['jpg', 'jpeg', 'png', 'webp'],
+                                            true,
+                                        )
+                                        : false,
+            'document_url'        => $this->document_path
+                                        ? Storage::disk('s3')->temporaryUrl($this->document_path, now()->addHours(24))
+                                        : null,
             'responsible_user'    => $this->whenLoaded('responsibleUser', fn() => [
                 'id'       => $this->responsibleUser->id,
                 'name'     => $this->responsibleUser->name,

+ 29 - 2
app/Services/UserDependentService.php

@@ -5,7 +5,10 @@ namespace App\Services;
 use App\Enums\UserDependentStatusEnum;
 use App\Models\UserDependent;
 use Illuminate\Database\Eloquent\Collection;
+use Illuminate\Http\UploadedFile;
 use Illuminate\Pagination\LengthAwarePaginator;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Storage;
 
 class UserDependentService
 {
@@ -43,11 +46,29 @@ class UserDependentService
         return UserDependent::find($id);
     }
 
-    public function create(array $data): UserDependent
+    public function create(array $data, ?UploadedFile $document = null): UserDependent
     {
+        unset($data['document']);
+
         $data['status'] = UserDependentStatusEnum::PENDING->value;
 
-        return UserDependent::create($data);
+        return DB::transaction(function () use ($data, $document): UserDependent {
+            $dependent = UserDependent::create($data);
+
+            if ($document) {
+                $dependent->update([
+                    'document_path' => $document->store($this->documentDirectory($dependent), 's3'),
+                    'document_name' => $document->getClientOriginalName(),
+                ]);
+            }
+
+            return $dependent->fresh();
+        });
+    }
+
+    private function documentDirectory(UserDependent $dependent): string
+    {
+        return "dependents/{$dependent->responsible_user_id}/dependent/{$dependent->id}";
     }
 
     public function approve(int $id): ?UserDependent
@@ -82,6 +103,8 @@ class UserDependentService
             return null;
         }
 
+        unset($data['status'], $data['document'], $data['document_path'], $data['document_name']);
+
         $model->update($data);
         return $model->fresh();
     }
@@ -94,6 +117,10 @@ class UserDependentService
             return false;
         }
 
+        if ($model->document_path) {
+            Storage::disk('s3')->deleteDirectory($this->documentDirectory($model));
+        }
+
         return $model->delete();
     }
 }

+ 23 - 0
database/migrations/2026_08_10_000001_add_document_path_to_user_dependents_table.php

@@ -0,0 +1,23 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    public function up(): void
+    {
+        Schema::table('user_dependents', function (Blueprint $table) {
+            $table->string('document_path', 500)->nullable()->after('status');
+            $table->string('document_name')->nullable()->after('document_path');
+        });
+    }
+
+    public function down(): void
+    {
+        Schema::table('user_dependents', function (Blueprint $table) {
+            $table->dropColumn(['document_path', 'document_name']);
+        });
+    }
+};