migrant-web/app/Http/Controllers/Employer/CandidateController.php
2026-05-20 13:26:43 +05:30

102 lines
3.4 KiB
PHP

<?php
namespace App\Http\Controllers\Employer;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Inertia\Inertia;
use App\Models\User;
use App\Models\JobApplication;
use App\Models\JobPost;
class CandidateController extends Controller
{
private function resolveCurrentUser()
{
$sess = session('user');
$sessId = is_array($sess) ? ($sess['id'] ?? null) : ($sess->id ?? null);
if (!$sessId) {
$user = User::where('role', 'employer')->first();
if ($user) {
session(['user' => (object)[
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'role' => 'employer',
'subscription_status' => $user->subscription_status ?? 'active',
]]);
return $user;
}
} else {
return User::find($sessId);
}
return null;
}
public function index(Request $request)
{
$user = $this->resolveCurrentUser();
$employerId = $user ? $user->id : 2;
// Get job posts created by this employer
$jobIds = JobPost::where('employer_id', $employerId)->pluck('id');
// Fetch applications for those jobs
$applications = JobApplication::whereIn('job_id', $jobIds)
->with(['worker.category', 'jobPost'])
->get();
$selectedWorkers = $applications->map(function ($app) {
$w = $app->worker;
if (!$w) return null;
// Map DB status to UI friendly status
$status = 'Reviewing';
if ($app->status === 'hired') $status = 'Hired';
elseif ($app->status === 'rejected') $status = 'Rejected';
elseif ($app->status === 'shortlisted' || $app->status === 'offer_sent') $status = 'Offer Sent';
elseif ($app->status === 'applied') $status = 'Reviewing';
else $status = ucfirst($app->status);
return [
'id' => $app->id, // application id
'worker_id' => $w->id,
'name' => $w->name,
'nationality' => $w->nationality,
'category' => $w->category ? $w->category->name : 'General Helper',
'salary' => (int)$w->salary,
'status' => $status,
'applied_at' => $app->created_at->format('M d, Y'),
];
})->filter()->values()->toArray();
return Inertia::render('Employer/SelectedCandidates', [
'selectedWorkers' => $selectedWorkers,
]);
}
public function updateStatus(Request $request, $id)
{
$request->validate([
'status' => 'required|string|in:Reviewing,Offer Sent,Hired,Rejected',
]);
$app = JobApplication::findOrFail($id);
// Map UI status back to DB status
$dbStatus = 'applied';
if ($request->status === 'Hired') $dbStatus = 'hired';
elseif ($request->status === 'Rejected') $dbStatus = 'rejected';
elseif ($request->status === 'Offer Sent') $dbStatus = 'shortlisted';
elseif ($request->status === 'Reviewing') $dbStatus = 'applied';
$app->update([
'status' => $dbStatus,
]);
return back()->with('success', 'Candidate status updated successfully to ' . $request->status);
}
}