| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- namespace App\Services;
- use App\Models\PreQuestionCandidate;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- class QuestionBankSyncService
- {
- public function pushCandidate(PreQuestionCandidate $candidate): bool
- {
- if (!$candidate->structured_json) {
- return false;
- }
- $payload = [
- 'candidate_id' => $candidate->id,
- 'source_file_id' => $candidate->source_file_id,
- 'source_paper_id' => $candidate->source_paper_id,
- 'part_id' => $candidate->part_id,
- 'question_number' => $candidate->question_number ?? $candidate->index,
- 'structured' => json_decode($candidate->structured_json, true),
- ];
- $base = rtrim(config('services.question_bank.base_url'), '/');
- $endpoint = $base . '/ingest/from-filament';
- try {
- $response = Http::timeout(config('services.question_bank.timeout', 60))
- ->post($endpoint, $payload);
- if ($response->failed()) {
- Log::warning('Question bank sync failed', [
- 'candidate_id' => $candidate->id,
- 'status' => $response->status(),
- 'body' => $response->body(),
- ]);
- return false;
- }
- return true;
- } catch (\Throwable $e) {
- Log::error('Question bank sync error', [
- 'candidate_id' => $candidate->id,
- 'error' => $e->getMessage(),
- ]);
- return false;
- }
- }
- }
|