92 lines
2.8 KiB
PHP
92 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Models\JobPosition;
|
|
use App\Models\Candidate;
|
|
use App\Models\User;
|
|
use App\Models\Document;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Str;
|
|
use Inertia\Inertia;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class PublicJobApplicationController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$jobs = JobPosition::with('tenant')->orderBy('created_at', 'desc')->get();
|
|
return Inertia::render('Public/Jobs/Index', [
|
|
'jobs' => $jobs
|
|
]);
|
|
}
|
|
|
|
public function show(JobPosition $jobPosition)
|
|
{
|
|
return Inertia::render('Public/Jobs/Show', [
|
|
'jobPosition' => $jobPosition
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request, JobPosition $jobPosition)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'email' => 'required|string|email|max:255|unique:users',
|
|
'phone' => 'nullable|string|max:20',
|
|
'linkedin_url' => 'nullable|url|max:255',
|
|
'city' => 'nullable|string|max:255',
|
|
'cv' => 'nullable|mimes:pdf|max:5120',
|
|
'cover_letter' => 'nullable|mimes:pdf|max:5120',
|
|
]);
|
|
|
|
$password = Str::random(10);
|
|
|
|
$user = User::create([
|
|
'name' => $request->name,
|
|
'email' => $request->email,
|
|
'password' => Hash::make($password),
|
|
'role' => 'candidate',
|
|
'tenant_id' => $jobPosition->tenant_id,
|
|
]);
|
|
|
|
$candidate = $user->candidate()->create([
|
|
'phone' => $request->phone,
|
|
'linkedin_url' => $request->linkedin_url,
|
|
'city' => $request->city,
|
|
'status' => 'en_attente',
|
|
'tenant_id' => $jobPosition->tenant_id,
|
|
'job_position_id' => $jobPosition->id,
|
|
]);
|
|
|
|
if ($request->hasFile('cv')) {
|
|
$this->storeDocument($candidate, $request->file('cv'), 'cv');
|
|
}
|
|
if ($request->hasFile('cover_letter')) {
|
|
$this->storeDocument($candidate, $request->file('cover_letter'), 'cover_letter');
|
|
}
|
|
|
|
// Auto-login the candidate so they can take the quiz immediately if they want
|
|
Auth::login($user);
|
|
|
|
return redirect()->route('dashboard')->with('success', 'Votre candidature a bien été enregistrée. Voici votre mot de passe temporaire pour vous reconnecter : ' . $password);
|
|
}
|
|
|
|
private function storeDocument(Candidate $candidate, $file, string $type)
|
|
{
|
|
if (!$file) {
|
|
return;
|
|
}
|
|
|
|
$path = $file->store('private/documents/' . $candidate->id, 'local');
|
|
|
|
Document::create([
|
|
'candidate_id' => $candidate->id,
|
|
'type' => $type,
|
|
'file_path' => $path,
|
|
'original_name' => $file->getClientOriginalName(),
|
|
]);
|
|
}
|
|
}
|