user()->id)->exists()) { Session::flash('error', get_phrase('Your request is in process. Please wait for admin to response.')); return redirect()->route('become.instructor'); } $rules = [ 'nidn' => ['required', 'string', 'max:11'], 'phone' => 'required', 'document' => ['required', 'file', 'mimes:pdf,doc,docx', 'max:2048'], 'description' => 'required', ]; // Validate data $validator = Validator::make($request->all(), $rules, [ 'nidn.required' => get_phrase('NIDN is required'), 'nidn.string' => get_phrase('NIDN must be a valid number'), 'nidn.max' => get_phrase('NIDN may not be greater than 11 characters'), 'phone.required' => get_phrase('Phone number is required'), 'document.required' => get_phrase('Document is required'), 'document.file' => get_phrase('Document must be a valid file'), 'document.mimes' => get_phrase('Document must be PDF, DOC, or DOCX'), 'document.max' => get_phrase('Document size must be less than 2MB'), 'description.required' => get_phrase('Description is required'), ]); if ($validator->fails()) { $firstError = $validator->errors()->first(); Session::flash('error', $firstError); return redirect()->back()->withErrors($validator)->withInput(); } $user = auth()->user(); try { DB::beginTransaction(); $result = $this->processInstructorApplication($request, $user); if ($result instanceof RedirectResponse && $result->getSession()->get('error')) { DB::rollBack(); return $result; } DB::commit(); Session::flash('success', get_phrase('Your application has been submitted successfully.')); return redirect(RouteServiceProvider::HOME); } catch (\Exception $e) { DB::rollBack(); Log::error('Instructor application error:', ['error' => $e->getMessage()]); Session::flash('error', get_phrase('Error during application submission. Please try again.')); return redirect()->back()->withInput(); } } private function processInstructorApplication(Request $request, User $user): RedirectResponse { // Check if application already exists (redundant but safe) if (Application::where('user_id', $user->id)->exists()) { Session::flash('error', get_phrase('Your request is in process. Please wait for admin to respond.')); return redirect()->route('become.instructor'); } // Check if NIDN already exists in Instructors table with status = 1 (active) $nidn = $request->nidn; $existingInstructor = Instructors::where('nidn', $nidn) ->where('status', 1) ->first(); if ($existingInstructor) { Session::flash('error', get_phrase('This NIDN is already registered as an active instructor.')); return redirect()->back()->withInput(); } // Check NIDN with the API $api_url = "https://sindig.unesa.ac.id/apipddikti/api?nidn={$nidn}&auto=1"; $response = Http::timeout(30)->get($api_url); $data = $response->json(); Log::info('API Response for NIDN: ' . $nidn, ['response' => $data]); // Extract matched dosen data $matched_dosen = $data['matched_dosen'][0]; Log::info('Instructor data to be saved:', $matched_dosen); if (!isset($data['ok']) || !isset($data['matched_dosen']) || count($data['matched_dosen']) == 0) { Session::flash('error', get_phrase('NIDN not found in the system. Please check your NIDN.')); return redirect()->back()->withInput(); } else if (strtolower($matched_dosen['nama']) != strtolower($user->name)) { Session::flash('error', get_phrase('Name does not match PDDikti records. Please check your name.')); return redirect()->back()->withInput(); } // Upload document $fileName = null; if ($request->hasFile('document') && $request->file('document')->isValid()) { $doc = $request->file('document'); $fileName = 'uploads/applications/' . $user->id . Str::random(20) . '.' . $doc->getClientOriginalExtension(); $uploadResult = FileUploader::upload($doc, $fileName, null, null, 300); if (!$uploadResult) { Session::flash('error', get_phrase('Document upload failed. Please try again.')); return redirect()->back()->withInput(); } } else { Session::flash('error', get_phrase('Document upload failed or no document selected.')); return redirect()->back()->withInput(); } // Prepare instructor data $instructorData = [ 'user_id' => $user->id, 'nidn' => $nidn, 'name' => $matched_dosen['nama'] ?? $user->name, 'id_sdm' => $matched_dosen['id'] ?? null, 'id_sms' => $matched_dosen['nama_prodi'] ?? null, 'id_pt' => $matched_dosen['nama_pt'] ?? null, 'status' => 0 ]; Log::info('Instructor data to be saved:', $instructorData); // Prepare application data - INCLUDING DOCUMENT PATH $application = [ 'user_id' => $user->id, 'nidn' => $nidn, 'phone' => $request->phone, 'description' => $request->description, 'document' => $fileName, 'status' => 0 ]; //send notification to user $user->notify(new \App\Notifications\InstructorApplicant($application)); Log::info('Application data to be saved:', $application); // Create both application and instructor records Application::create($application); Instructors::create($instructorData); return redirect()->back(); // This return won't be used due to the try-catch structure } }