fix student registration

This commit is contained in:
root
2026-03-26 16:04:03 -04:00
parent c582bfc242
commit 1977a513df
9 changed files with 220 additions and 27 deletions
@@ -30,6 +30,7 @@ use App\Services\Students\StudentScoreCardService;
use App\Services\Students\StudentStatusService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
@@ -133,7 +134,15 @@ class StudentController extends BaseApiController
public function store(Request $request): JsonResponse
{
$data = array_merge($request->query->all(), $request->all(), $request->json()->all());
$validator = Validator::make($data, [
$parentId = (int) ($data['parent_id'] ?? 0);
$isFirstForParent = $parentId > 0
&& Student::query()->where('parent_id', $parentId)->count() === 0;
if (empty($data['school_id'])) {
$nextId = (int) Student::query()->max('id') + 1;
$data['school_id'] = $this->generateSchoolId($nextId);
}
$rules = [
'school_id' => ['nullable', 'string', 'max:50'],
'firstname' => ['required', 'string', 'max:150'],
'lastname' => ['required', 'string', 'max:150'],
@@ -147,11 +156,25 @@ class StudentController extends BaseApiController
'parent_id' => ['nullable', 'integer', 'min:1'],
'registration_date' => ['nullable', 'date'],
'tuition_paid' => ['nullable', 'boolean'],
'rfid_tag' => ['nullable', 'string', 'max:100'],
'semester' => ['nullable', 'string', 'max:50'],
'year_of_registration' => ['nullable', 'integer', 'min:1900'],
'school_year' => ['required', 'string', 'max:50'],
]);
'medical_conditions' => ['nullable', 'string'],
'allergies' => ['nullable', 'string'],
];
if ($isFirstForParent) {
$rules = array_merge($rules, [
'name' => ['required', 'string', 'max:150'],
'cellphone' => ['required', 'string', 'max:30'],
'email' => ['required', 'email', 'max:150'],
'relation' => ['nullable', 'string', 'max:80'],
'semester' => ['nullable', 'string', 'max:50'],
'school_year' => ['nullable', 'string', 'max:50'],
]);
}
$validator = Validator::make($data, $rules);
if ($validator->fails()) {
return response()->json([
@@ -168,6 +191,34 @@ class StudentController extends BaseApiController
$student->save();
}
$healthPayload = [];
if (array_key_exists('medical_conditions', $payload)) {
$healthPayload['medical_conditions'] = $payload['medical_conditions'];
}
if (array_key_exists('allergies', $payload)) {
$healthPayload['allergies'] = $payload['allergies'];
}
if (!empty($healthPayload)) {
$this->profileService->updateStudent($student->id, $healthPayload);
}
if ($isFirstForParent) {
$contactPayload = [
'parent_id' => $parentId,
'emergency_contact_name' => $payload['name'],
'cellphone' => $payload['cellphone'],
'email' => $payload['email'],
'relation' => $payload['relation'] ?? null,
'semester' => $payload['semester'] ?? null,
'school_year' => $payload['school_year'] ?? null,
];
if (Schema::hasColumn('emergency_contacts', 'student_id')) {
$contactPayload['student_id'] = $student->id;
}
EmergencyContact::query()->create($contactPayload);
}
return response()->json(['ok' => true, 'student' => $student], 201);
}
@@ -178,7 +229,7 @@ class StudentController extends BaseApiController
(int) $payload['student_id'],
$payload['class_section_id'],
(bool) ($payload['is_event_only'] ?? false),
(int) (auth()->id() ?? 0)
(int) (Auth::id() ?? 0)
);
$status = $result['ok'] ? 200 : 422;
@@ -192,7 +243,7 @@ class StudentController extends BaseApiController
$result = $this->assignmentService->removeClass(
(int) $payload['student_id'],
(int) $payload['class_section_id'],
(int) (auth()->id() ?? 0)
(int) (Auth::id() ?? 0)
);
$status = $result['ok'] ? 200 : 422;
@@ -218,7 +269,7 @@ class StudentController extends BaseApiController
$result = $this->statusService->setActive(
(int) $payload['student_id'],
(bool) $payload['is_active'],
(int) (auth()->id() ?? 0)
(int) (Auth::id() ?? 0)
);
$status = $result['ok'] ? 200 : 422;
@@ -234,7 +285,7 @@ class StudentController extends BaseApiController
(int) $payload['students_per_section'],
$payload['school_year'] ?? null,
(int) ($payload['class_section_id'] ?? 0),
(int) (auth()->id() ?? 0)
(int) (Auth::id() ?? 0)
);
$status = $result['ok'] ? 200 : 422;
@@ -340,7 +391,7 @@ class StudentController extends BaseApiController
$studentId,
$ids,
(bool) ($payload['is_event_only'] ?? false),
(int) (auth()->id() ?? 0)
(int) (Auth::id() ?? 0)
);
$status = $result['ok'] ? 200 : 422;
@@ -353,7 +404,7 @@ class StudentController extends BaseApiController
$result = $this->assignmentService->removeClass(
$studentId,
$classSectionId,
(int) (auth()->id() ?? 0)
(int) (Auth::id() ?? 0)
);
$status = $result['ok'] ? 200 : 422;
@@ -380,7 +431,7 @@ class StudentController extends BaseApiController
$studentId,
[(int) $payload['class_section_id']],
false,
(int) (auth()->id() ?? 0)
(int) (Auth::id() ?? 0)
);
$status = $result['ok'] ? 200 : 422;