Browse Source

feat: adiciona tab de responsavel

ebagabee 1 month ago
parent
commit
6e1e9855e4
2 changed files with 30 additions and 0 deletions
  1. 18 0
      app/Http/Requests/StudentRequest.php
  2. 12 0
      app/Services/StudentService.php

+ 18 - 0
app/Http/Requests/StudentRequest.php

@@ -19,11 +19,29 @@ public function rules(): array
             'street'              => 'sometimes|nullable|string|max:255',
             'address_number'      => 'sometimes|nullable|string|max:20',
             'neighborhood'        => 'sometimes|nullable|string|max:255',
+            'city_id'             => 'sometimes|nullable|integer|exists:cities,id',
             'state_id'            => 'sometimes|nullable|integer|exists:states,id',
             'complement'          => 'sometimes|nullable|string|max:255',
             'payer_name'          => 'sometimes|nullable|string|max:255',
             'how_did_you_know_us' => 'sometimes|nullable|string|in:referral,social_media,google,other',
             'notes'               => 'sometimes|nullable|string',
+
+            'responsible'                   => 'sometimes|nullable|array',
+            'responsible.name'              => 'sometimes|nullable|string|max:255',
+            'responsible.birth_date'        => 'sometimes|nullable|date',
+            'responsible.cpf'               => 'sometimes|nullable|string|max:20',
+            'responsible.gender'            => 'sometimes|nullable|string|in:no_preference,male,female,other',
+            'responsible.degree'            => 'sometimes|nullable|string|max:255',
+            'responsible.email'             => 'sometimes|nullable|email',
+            'responsible.phone'             => 'sometimes|nullable|string|max:20',
+            'responsible.postal_code'       => 'sometimes|nullable|string|max:10',
+            'responsible.street'            => 'sometimes|nullable|string|max:255',
+            'responsible.address_number'    => 'sometimes|nullable|string|max:20',
+            'responsible.neighborhood'      => 'sometimes|nullable|string|max:255',
+            'responsible.city_id'           => 'sometimes|nullable|integer|exists:cities,id',
+            'responsible.state_id'          => 'sometimes|nullable|integer|exists:states,id',
+            'responsible.complement'        => 'sometimes|nullable|string|max:255',
+            'responsible.notes'             => 'sometimes|nullable|string',
         ];
 
         if ($this->isMethod('post')) {

+ 12 - 0
app/Services/StudentService.php

@@ -3,6 +3,7 @@
 namespace App\Services;
 
 use App\Models\Student;
+use App\Models\StudentResponsible;
 use App\Models\User;
 use Illuminate\Database\Eloquent\Collection;
 use Illuminate\Http\UploadedFile;
@@ -40,8 +41,19 @@ public function update(int $id, array $data): ?Student
             return null;
         }
 
+        $responsibleData = $data['responsible'] ?? null;
+        unset($data['responsible']);
+
         $data = $this->handlePhoto($data, $model->photo_url);
         $model->update($data);
+
+        if ($responsibleData !== null) {
+            StudentResponsible::updateOrCreate(
+                ['student_id' => $id],
+                array_merge($responsibleData, ['student_id' => $id]),
+            );
+        }
+
         return $model->fresh();
     }