QuestionBankSyncService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Services;
  3. use App\Models\PreQuestionCandidate;
  4. use Illuminate\Support\Facades\Http;
  5. use Illuminate\Support\Facades\Log;
  6. class QuestionBankSyncService
  7. {
  8. public function pushCandidate(PreQuestionCandidate $candidate): bool
  9. {
  10. if (!$candidate->structured_json) {
  11. return false;
  12. }
  13. $payload = [
  14. 'candidate_id' => $candidate->id,
  15. 'source_file_id' => $candidate->source_file_id,
  16. 'source_paper_id' => $candidate->source_paper_id,
  17. 'part_id' => $candidate->part_id,
  18. 'question_number' => $candidate->question_number ?? $candidate->index,
  19. 'structured' => json_decode($candidate->structured_json, true),
  20. ];
  21. $base = rtrim(config('services.question_bank.base_url'), '/');
  22. $endpoint = $base . '/ingest/from-filament';
  23. try {
  24. $response = Http::timeout(config('services.question_bank.timeout', 60))
  25. ->post($endpoint, $payload);
  26. if ($response->failed()) {
  27. Log::warning('Question bank sync failed', [
  28. 'candidate_id' => $candidate->id,
  29. 'status' => $response->status(),
  30. 'body' => $response->body(),
  31. ]);
  32. return false;
  33. }
  34. return true;
  35. } catch (\Throwable $e) {
  36. Log::error('Question bank sync error', [
  37. 'candidate_id' => $candidate->id,
  38. 'error' => $e->getMessage(),
  39. ]);
  40. return false;
  41. }
  42. }
  43. }