49 lines
1.4 KiB
PHP
49 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Candidate;
|
|
use App\Services\AIAnalysisService;
|
|
use Illuminate\Http\Request;
|
|
use Carbon\Carbon;
|
|
|
|
class AIAnalysisController extends Controller
|
|
{
|
|
protected $aiService;
|
|
|
|
public function __construct(AIAnalysisService $aiService)
|
|
{
|
|
$this->aiService = $aiService;
|
|
}
|
|
|
|
public function analyze(Request $request, Candidate $candidate)
|
|
{
|
|
if (!auth()->user()->isAdmin()) {
|
|
abort(403);
|
|
}
|
|
|
|
// Restriction: Une analyse tous les 7 jours maximum par candidat
|
|
if ($candidate->ai_analysis && isset($candidate->ai_analysis['analyzed_at'])) {
|
|
$lastAnalysis = Carbon::parse($candidate->ai_analysis['analyzed_at']);
|
|
if ($lastAnalysis->diffInDays(now()) < 7) {
|
|
return response()->json([
|
|
'error' => "Une analyse a déjà été effectuée il y a moins de 7 jours. Merci de patienter avant de relancer l'IA."
|
|
], 422);
|
|
}
|
|
}
|
|
|
|
try {
|
|
$analysis = $this->aiService->analyze($candidate, $request->provider);
|
|
|
|
// Persist the analysis on the candidate profile
|
|
$candidate->update([
|
|
'ai_analysis' => $analysis
|
|
]);
|
|
|
|
return response()->json($analysis);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|