mirror of https://github.com/Lukibeg/OmniBoard.git
334 lines
10 KiB
PHP
334 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Tenant;
|
|
use App\Models\Agent;
|
|
use App\Models\Queue;
|
|
use App\Models\WaitingList;
|
|
use App\Models\DailyMetric;
|
|
use Carbon\Carbon;
|
|
use App\Events\DashboardUpdate;
|
|
|
|
class AmiEventController extends Controller
|
|
{
|
|
public function handle(Request $request)
|
|
{
|
|
$token = $request->header('X-Tenant-Token');
|
|
$tenant = Tenant::where('api_key', $token)->first();
|
|
|
|
if (!$tenant) {
|
|
return response()->json(['error' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
$data = $request->all();
|
|
$eventName = $data['Event'] ?? null;
|
|
$queueNumber = $data['Queue'] ?? null;
|
|
|
|
// Tenta achar a interface. Se for MemberStatus, vem em 'Interface'. Se for AgentConnect, vem em 'Interface' ou 'MemberName'
|
|
$interface = $data['Interface'] ?? $data['MemberName'] ?? null;
|
|
|
|
// 1. Auto-discovery (Com proteção para não salvar a fila fake do Auto-Hangup)
|
|
if ($queueNumber && $queueNumber !== 'Auto-Hangup') {
|
|
$this->saveQueues($queueNumber, $tenant);
|
|
}
|
|
if ($interface) {
|
|
$this->saveAgent($interface, $tenant);
|
|
}
|
|
|
|
if (!$eventName || !$queueNumber) {
|
|
return response()->json(['status' => 'ignored']);
|
|
}
|
|
|
|
// 2. Busca o Agente (Movi para cima, pois precisamos dele para o AgentFinished)
|
|
$agent = $this->findAgent($tenant->id, $data);
|
|
|
|
// 3. INTERCEPTADOR ANTI-FANTASMA
|
|
// Processa o AgentFinished ANTES de validar a fila, pois 'Auto-Hangup' não existe no BD
|
|
if ($eventName === 'AgentFinished') {
|
|
if ($agent) {
|
|
$this->handleFinished($agent, $tenant->id);
|
|
}
|
|
return response()->json(['status' => 'fixed']);
|
|
}
|
|
|
|
// 4. Recupera Fila (Fluxo normal)
|
|
$queue = Queue::where('tenant_id', $tenant->id)
|
|
->where('source_id', $queueNumber)
|
|
->first();
|
|
|
|
if (!$queue) {
|
|
return response()->json(['error' => 'Fila não encontrada'], 404);
|
|
}
|
|
|
|
switch ($eventName) {
|
|
case 'QueueCallerJoin':
|
|
$this->handleJoin($queue, $data);
|
|
break;
|
|
|
|
case 'AgentConnect':
|
|
$this->handleConnect($queue, $data);
|
|
break;
|
|
|
|
case 'AgentComplete':
|
|
$this->handleComplete($queue, $data);
|
|
break;
|
|
|
|
case 'QueueCallerAbandon':
|
|
$this->handleAbandon($queue, $data);
|
|
break;
|
|
|
|
case 'QueueMemberPause':
|
|
if ($agent) $this->handlePause($agent, $data, $tenant->id);
|
|
break;
|
|
|
|
case 'AgentRingNoAnswer':
|
|
if ($agent) $this->handleRingNoAnswer($agent, $tenant->id);
|
|
break;
|
|
|
|
case 'QueueMemberStatus':
|
|
if ($agent) $this->handleMemberStatus($agent, $data, $tenant->id);
|
|
break;
|
|
}
|
|
|
|
return response()->json(['status' => 'success']);
|
|
}
|
|
|
|
// =========================================================================
|
|
// HANDLERS
|
|
// =========================================================================
|
|
|
|
// --- NOVO HANDLER: Solução para Agentes Fantasmas ---
|
|
private function handleFinished($agent, $tenantId)
|
|
{
|
|
// Só forçamos 'available' se ele estiver 'talking'.
|
|
// Se ele estiver 'paused', não mexemos (para respeitar pausas feitas durante chamada)
|
|
if ($agent->status === 'talking') {
|
|
$agent->status = 'available';
|
|
$agent->last_status_change = now();
|
|
$agent->save();
|
|
|
|
broadcast(new DashboardUpdate($tenantId));
|
|
}
|
|
}
|
|
|
|
private function handleMemberStatus($agent, $data, $tenantId)
|
|
{
|
|
$isPaused = ($data['Paused'] ?? '0') == '1';
|
|
$asteriskStatus = $data['Status'] ?? null;
|
|
|
|
$targetStatus = 'offline';
|
|
$targetReason = null;
|
|
|
|
if ($isPaused) {
|
|
$targetStatus = 'paused';
|
|
$targetReason = !empty($data['PausedReason']) ? $data['PausedReason'] : ($agent->pause_reason ?? 'Pausa');
|
|
} else {
|
|
$targetReason = null;
|
|
switch ($asteriskStatus) {
|
|
case '1':
|
|
$targetStatus = 'available';
|
|
break;
|
|
case '2':
|
|
case '3':
|
|
case '6':
|
|
$targetStatus = 'talking';
|
|
break;
|
|
case '5':
|
|
$targetStatus = 'offline';
|
|
break;
|
|
default:
|
|
$targetStatus = 'offline';
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($agent->status === $targetStatus && $agent->pause_reason === $targetReason) {
|
|
return;
|
|
}
|
|
|
|
$agent->status = $targetStatus;
|
|
$agent->pause_reason = $targetReason;
|
|
$agent->last_status_change = now();
|
|
$agent->save();
|
|
|
|
broadcast(new DashboardUpdate($tenantId));
|
|
}
|
|
|
|
private function handleConnect($queue, $data)
|
|
{
|
|
WaitingList::where('queue_id', $queue->id)
|
|
->where('caller_number', $data['CallerIDNum'])
|
|
->delete();
|
|
|
|
$metric = $this->getTodayMetric($queue);
|
|
$holdTime = intval($data['HoldTime'] ?? 0);
|
|
$newAvg = (($metric->avg_wait_time * $metric->answered_count) + $holdTime) / ($metric->answered_count + 1);
|
|
|
|
$metric->avg_wait_time = $newAvg;
|
|
$metric->answered_count += 1;
|
|
$metric->save();
|
|
|
|
$agent = $this->findAgent($queue->tenant_id, $data);
|
|
if ($agent) {
|
|
$agent->status = 'talking';
|
|
$agent->last_status_change = now();
|
|
$agent->increment('total_calls_answered');
|
|
$agent->save();
|
|
}
|
|
|
|
$this->broadcastUpdate($queue);
|
|
}
|
|
|
|
private function handleComplete($queue, $data)
|
|
{
|
|
$talkTime = intval($data['TalkTime'] ?? 0);
|
|
$metric = $this->getTodayMetric($queue);
|
|
|
|
if ($metric->answered_count > 0) {
|
|
$newAvg = (($metric->avg_talk_time * ($metric->answered_count - 1)) + $talkTime) / $metric->answered_count;
|
|
$metric->avg_talk_time = $newAvg;
|
|
$metric->save();
|
|
}
|
|
|
|
$this->broadcastUpdate($queue);
|
|
}
|
|
|
|
private function handlePause($agent, $data, $tenantId)
|
|
{
|
|
$isPaused = isset($data['Paused']) && $data['Paused'] == '1';
|
|
$targetStatus = $isPaused ? 'paused' : 'available';
|
|
|
|
$targetReason = null;
|
|
if ($isPaused) {
|
|
$targetReason = $data['Reason'] ?? $data['PausedReason'] ?? 'Pausa';
|
|
if (trim($targetReason) === '') $targetReason = 'Pausa';
|
|
}
|
|
|
|
if ($agent->status === $targetStatus && $agent->pause_reason === $targetReason) {
|
|
return;
|
|
}
|
|
|
|
$agent->status = $targetStatus;
|
|
$agent->pause_reason = $targetReason;
|
|
$agent->last_status_change = now();
|
|
$agent->save();
|
|
|
|
broadcast(new DashboardUpdate($tenantId));
|
|
}
|
|
|
|
private function handleAbandon($queue, $data)
|
|
{
|
|
WaitingList::where('queue_id', $queue->id)
|
|
->where('caller_number', $data['CallerIDNum'])
|
|
->delete();
|
|
|
|
$this->updateMetric($queue, 'abandoned_count', 1);
|
|
$this->broadcastUpdate($queue);
|
|
}
|
|
|
|
private function handleRingNoAnswer($agent, $tenantId)
|
|
{
|
|
$agent->increment('total_ring_no_answer');
|
|
broadcast(new DashboardUpdate($tenantId));
|
|
}
|
|
|
|
private function handleJoin($queue, $data)
|
|
{
|
|
WaitingList::create([
|
|
'tenant_id' => $queue->tenant_id,
|
|
'queue_id' => $queue->id,
|
|
'caller_number' => $data['CallerIDNum'],
|
|
'caller_name' => $data['CallerIDName'] ?? 'Desconhecido',
|
|
'entered_at' => now(),
|
|
]);
|
|
|
|
$this->updateMetric($queue, 'received_count', 1);
|
|
$this->broadcastUpdate($queue);
|
|
}
|
|
|
|
// =========================================================================
|
|
// AUXILIARES
|
|
// =========================================================================
|
|
|
|
private function saveAgent($interface, $tenant)
|
|
{
|
|
$exists = Agent::where('tenant_id', $tenant->id)
|
|
->where('interface', $interface)
|
|
->exists();
|
|
|
|
if (!$exists) {
|
|
$name = $interface;
|
|
if (strpos($interface, '/') !== false) {
|
|
$parts = explode('/', $interface);
|
|
$name = end($parts);
|
|
}
|
|
|
|
Agent::create([
|
|
'tenant_id' => $tenant->id,
|
|
'name' => "Agente $name",
|
|
'interface' => $interface,
|
|
'status' => 'available'
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function saveQueues($queueSourceId, $tenant)
|
|
{
|
|
// Proteção extra para não criar fila suja
|
|
if ($queueSourceId === 'Auto-Hangup') return;
|
|
|
|
$exists = Queue::where('tenant_id', $tenant->id)
|
|
->where('source_id', $queueSourceId)
|
|
->exists();
|
|
|
|
if (!$exists) {
|
|
Queue::create([
|
|
'tenant_id' => $tenant->id,
|
|
'type' => 'voice',
|
|
'source_id' => $queueSourceId,
|
|
'name' => "Fila $queueSourceId",
|
|
'sector' => null
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function findAgent($tenantId, $data)
|
|
{
|
|
$interface = $data['Interface'] ?? $data['MemberName'] ?? null;
|
|
|
|
return Agent::where('tenant_id', $tenantId)
|
|
->where('interface', $interface)
|
|
->first();
|
|
}
|
|
|
|
private function getTodayMetric($queue)
|
|
{
|
|
return DailyMetric::firstOrCreate(
|
|
[
|
|
'queue_id' => $queue->id,
|
|
'date' => Carbon::today()
|
|
],
|
|
[
|
|
'tenant_id' => $queue->tenant_id,
|
|
'received_count' => 0,
|
|
'answered_count' => 0,
|
|
'abandoned_count' => 0
|
|
]
|
|
);
|
|
}
|
|
|
|
private function updateMetric($queue, $field, $value)
|
|
{
|
|
$metric = $this->getTodayMetric($queue);
|
|
$metric->increment($field, $value);
|
|
}
|
|
|
|
private function broadcastUpdate($queueOrTenantId)
|
|
{
|
|
$tenantId = is_object($queueOrTenantId) ? $queueOrTenantId->tenant_id : $queueOrTenantId;
|
|
broadcast(new DashboardUpdate($tenantId));
|
|
}
|
|
} |