|
@@ -2,68 +2,68 @@
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
namespace App\Http\Requests;
|
|
|
|
|
|
|
|
|
|
+use App\Models\User;
|
|
|
use App\Services\StudentRegistrationDraftService;
|
|
use App\Services\StudentRegistrationDraftService;
|
|
|
-use App\ValueObjects\Cpf;
|
|
|
|
|
-use Carbon\Carbon;
|
|
|
|
|
-use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
+use Illuminate\Validation\Rule;
|
|
|
use Illuminate\Validation\Validator;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
|
|
|
|
-class StoreStudentRequest extends FormRequest
|
|
|
|
|
|
|
+class StoreStudentRequest extends StudentRequest
|
|
|
{
|
|
{
|
|
|
|
|
+ private ?array $registrationDraft = null;
|
|
|
|
|
+
|
|
|
|
|
+ protected function prepareForValidation(): void
|
|
|
|
|
+ {
|
|
|
|
|
+ $token = $this->input('registration_draft_token');
|
|
|
|
|
+
|
|
|
|
|
+ $user = Auth::user();
|
|
|
|
|
+
|
|
|
|
|
+ if (!is_string($token) || !$user instanceof User) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->registrationDraft = app(StudentRegistrationDraftService::class)
|
|
|
|
|
+ ->findOwnedDraft($user, $token);
|
|
|
|
|
+
|
|
|
|
|
+ if ($this->registrationDraft !== null) {
|
|
|
|
|
+ // Os dados validados do rascunho são a fonte oficial. Assim, o
|
|
|
|
|
+ // payload final não consegue sobrescrevê-los sem nova validação.
|
|
|
|
|
+
|
|
|
|
|
+ $this->merge($this->registrationDraft['data']);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function rules(): array
|
|
public function rules(): array
|
|
|
{
|
|
{
|
|
|
- return [
|
|
|
|
|
- 'registration_draft_token' => 'required|uuid',
|
|
|
|
|
- 'avatar' => 'sometimes|nullable|image',
|
|
|
|
|
- 'responsible' => 'sometimes|nullable|array|required_array_keys:name,birth_date,cpf,degree,email,phone,postal_code,street,neighborhood,city_id,state_id',
|
|
|
|
|
- 'responsible.name' => 'required_with:responsible|string|max:255',
|
|
|
|
|
- 'responsible.birth_date' => 'required_with:responsible|date',
|
|
|
|
|
- 'responsible.cpf' => ['required_with:responsible', 'string', 'max:20', Cpf::rule()],
|
|
|
|
|
- 'responsible.gender' => 'sometimes|nullable|string|in:no_preference,male,female,other',
|
|
|
|
|
- 'responsible.degree' => 'required_with:responsible|string|max:255',
|
|
|
|
|
- 'responsible.email' => 'required_with:responsible|email|max:255|unique:student_responsibles,email',
|
|
|
|
|
- 'responsible.phone' => 'required_with:responsible|string|max:20',
|
|
|
|
|
- 'responsible.postal_code' => 'required_with:responsible|string|max:10',
|
|
|
|
|
- 'responsible.street' => 'required_with:responsible|string|max:255',
|
|
|
|
|
- 'responsible.address_number' => 'sometimes|nullable|string|max:20',
|
|
|
|
|
- 'responsible.neighborhood' => 'required_with:responsible|string|max:255',
|
|
|
|
|
- 'responsible.city_id' => 'required_with:responsible|integer|exists:cities,id',
|
|
|
|
|
- 'responsible.state_id' => 'required_with:responsible|integer|exists:states,id',
|
|
|
|
|
- 'responsible.complement' => 'sometimes|nullable|string|max:255',
|
|
|
|
|
- 'responsible.notes' => 'sometimes|nullable|string',
|
|
|
|
|
|
|
+ $rules = parent::rules();
|
|
|
|
|
+
|
|
|
|
|
+ $rules['registration_draft_token'] = [
|
|
|
|
|
+ 'nullable',
|
|
|
|
|
+ 'uuid',
|
|
|
|
|
+ Rule::requiredIf(fn (): bool => $this->studentIsMinor()),
|
|
|
];
|
|
];
|
|
|
|
|
+
|
|
|
|
|
+ $rules['responsible'][] = 'required_array_keys:name,birth_date,cpf,degree,email,phone,postal_code,street,neighborhood,city_id,state_id';
|
|
|
|
|
+ $rules['responsible.email'] .= '|unique:student_responsibles,email';
|
|
|
|
|
+
|
|
|
|
|
+ return $rules;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function after(): array
|
|
public function after(): array
|
|
|
{
|
|
{
|
|
|
return [
|
|
return [
|
|
|
function (Validator $validator): void {
|
|
function (Validator $validator): void {
|
|
|
- if ($validator->errors()->has('registration_draft_token')) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $draft = app(StudentRegistrationDraftService::class)->findOwnedDraft(
|
|
|
|
|
- Auth::user(),
|
|
|
|
|
- $this->string('registration_draft_token')->toString(),
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- if ($draft === null) {
|
|
|
|
|
- $validator->errors()->add(
|
|
|
|
|
- 'registration_draft_token',
|
|
|
|
|
- __('validation.registration_draft_invalid'),
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
|
|
+ if ($this->input('registration_draft_token') === null) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
if (
|
|
|
- Carbon::parse($draft['data']['birth_date'])->age < 18
|
|
|
|
|
- && empty($this->input('responsible'))
|
|
|
|
|
|
|
+ !$validator->errors()->has('registration_draft_token')
|
|
|
|
|
+ && $this->registrationDraft === null
|
|
|
) {
|
|
) {
|
|
|
$validator->errors()->add(
|
|
$validator->errors()->add(
|
|
|
- 'responsible',
|
|
|
|
|
- __('validation.minor_requires_responsible'),
|
|
|
|
|
|
|
+ 'registration_draft_token',
|
|
|
|
|
+ __('validation.registration_draft_invalid'),
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|