service = $service; } /** * POST reporting/summarize — queue one call. Answers in milliseconds: the * provider is contacted by the queued job, never here. */ public function summarize(Request $request) { $data = $request->validate([ 'linkedid' => ['required', 'string', 'max:100'], 'direction' => ['nullable', 'in:inbound,outbound'], ]); $direction = $data['direction'] ?? 'inbound'; try { $analysis = $this->service->request($data['linkedid'], $direction, auth()->id()); } catch (Throwable $e) { if (!$request->ajax() && !$request->wantsJson()) { flash($e->getMessage())->error(); return redirect()->back(); } return response()->json([ 'status' => false, 'message' => $e->getMessage(), ], 422); } // The analysis page has a plain form (Re-run / Retry), so a normal // submit lands back on the page instead of on a JSON body. if (!$request->ajax() && !$request->wantsJson()) { flash('The call was queued for analysis.')->success(); return redirect()->route('reporting.analysis', ['linkedid' => $analysis->linkedid]); } return response()->json([ 'status' => true, 'data' => [ 'id' => $analysis->id, 'state' => $analysis->status, 'url' => route('reporting.analysis', ['linkedid' => $analysis->linkedid]), 'message' => 'The call was queued for analysis.', ], ]); } /** * GET reporting/analysis/{linkedid} — the stored analysis for one call. */ public function show(Request $request, $linkedid) { $analysis = CallAiAnalysis::latestFor((string) $linkedid); $direction = $analysis->direction ?? ($request->get('direction') === 'outbound' ? 'outbound' : 'inbound'); return view('reporting::call_analysis', [ 'linkedid' => (string) $linkedid, 'direction' => $direction, 'analysis' => $analysis, 'facts' => $this->service->facts((string) $linkedid, $direction), 'activeProvider' => $this->service->activeProvider(), 'listingUrl' => $direction === 'outbound' ? route('reporting.ccoutbound') : route('reporting.ccinbound'), 'recordingUrl' => route('reporting.listen.page', ['id' => $linkedid]), ]); } /** * GET reporting/analysis/{linkedid}/status — the page polls this while a run * is in flight, so the operator watches it finish without reloading. */ public function status($linkedid) { $analysis = CallAiAnalysis::latestFor((string) $linkedid); return response()->json([ 'status' => true, 'data' => [ 'state' => $analysis->status ?? null, 'label' => $analysis ? $analysis->statusLabel() : null, 'id' => $analysis->id ?? null, 'error' => $analysis->error ?? null, 'summary' => $analysis && $analysis->isCompleted() ? $analysis->summary : null, 'url' => route('reporting.analysis', ['linkedid' => $linkedid]), ], ]); } }