lucas cardoso
2 years ago
1 changed files with 101 additions and 0 deletions
@ -0,0 +1,101 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace app\Actions; |
||||||
|
|
||||||
|
use app\Core\UtilsChannel; |
||||||
|
use app\Models\AtendimentoModel; |
||||||
|
use app\Models\EventosAtendimentosModel; |
||||||
|
use app\Models\SupervisorModel; |
||||||
|
use app\Repositories\AtendimentoRepository; |
||||||
|
use app\Repositories\EventosAtendimentoRepository; |
||||||
|
use app\Repositories\MessageRepository; |
||||||
|
use app\Repositories\NumberChannelRepository; |
||||||
|
use app\Repositories\SupervisorRepository; |
||||||
|
use Exception; |
||||||
|
use websocket\WsInterface; |
||||||
|
|
||||||
|
class FinalizarAtemdimentoAction |
||||||
|
{ |
||||||
|
protected EventosAtendimentoRepository $eventosAtendimentosRepository; |
||||||
|
protected NumberChannelRepository $numberChannelRepository; |
||||||
|
|
||||||
|
protected SupervisorRepository $supervisorRepository; |
||||||
|
protected MessageRepository $messageRepository; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function __construct() |
||||||
|
{ |
||||||
|
$this->eventosAtendimentosRepository = new EventosAtendimentoRepository; |
||||||
|
$this->numberChannelRepository = new NumberChannelRepository; |
||||||
|
$this->supervisorRepository = new SupervisorRepository; |
||||||
|
$this->atendimentoRepository = new AtendimentoRepository; |
||||||
|
} |
||||||
|
function __invoke(AtendimentoModel $atendimento, SupervisorModel $agente, string $eventoType) |
||||||
|
{ |
||||||
|
$event = $this->eventosAtendimentosRepository->findEventFinish($atendimento->uniqueid); |
||||||
|
//verifica se o atendimento ja foi finalizado |
||||||
|
if (!empty($event)) { |
||||||
|
throw new Exception("Atendimento já foi finalizado"); |
||||||
|
} |
||||||
|
|
||||||
|
$ret = $this->eventosAtendimentosRepository->create( |
||||||
|
new EventosAtendimentosModel( |
||||||
|
id: null, |
||||||
|
uniqueid: $atendimento->uniqueid, |
||||||
|
evento: $eventoType, |
||||||
|
fila: $atendimento->fila, |
||||||
|
id_usuario: $eventoType == CONF_EVENT_TIMERMINO_AGENTE ? $agente->id_usuario : null, |
||||||
|
data_evento: 'now()', |
||||||
|
data_reg: 'now()' |
||||||
|
) |
||||||
|
); |
||||||
|
if (empty($ret)) { |
||||||
|
throw new Exception("Não foi possivel criar o evento", 1); |
||||||
|
} |
||||||
|
|
||||||
|
$ws = new WsInterface(); |
||||||
|
$mesg = $this->returnMessageWhatsapp($eventoType); |
||||||
|
$number = $this->numberChannelRepository->get(['id' => $atendimento->id_number_channel]); |
||||||
|
$provedor = UtilsChannel::factoryChannel($number); |
||||||
|
|
||||||
|
$this->messageRepository->addMessage( |
||||||
|
$atendimento->uniqueid, |
||||||
|
$eventoType == CONF_EVENT_TIMERMINO_AGENTE ? $agente->matricula : $atendimento->cliente_id, |
||||||
|
$agente->matricula, |
||||||
|
'finish', |
||||||
|
$mesg, |
||||||
|
$eventoType == CONF_EVENT_TIMERMINO_AGENTE ? $agente->nome : $atendimento->nome, |
||||||
|
$atendimento->context, |
||||||
|
'read' |
||||||
|
); |
||||||
|
$action = new AtualizaStatusAgenteAction; |
||||||
|
$statusAgente = $action(SupervisorModel::ArrayTo(get_object_vars($agente))); |
||||||
|
$this->supervisorRepository->db->commit(); |
||||||
|
|
||||||
|
$ws->enviaMsg($ws->enviaActions("Agente em '$statusAgente'", 'att_status', $agente->matricula, $atendimento->uniqueid)); |
||||||
|
|
||||||
|
$systemMessageAction = new SystemMessageAction; |
||||||
|
$systemMessageAction( |
||||||
|
momento: CONF_MOMENT_FINALIZAR_ATENDIMENTO, |
||||||
|
variavels: [["nome" => "@autor_name", "valor" => $agente->nome]], |
||||||
|
api: $provedor, |
||||||
|
numero: $atendimento->cliente_id |
||||||
|
); |
||||||
|
$ws->enviaMsg($ws->enviaActions($mesg, 'finish', $agente->matricula, $atendimento->uniqueid)); |
||||||
|
} |
||||||
|
|
||||||
|
function returnMessageWhatsapp(string $evento): string |
||||||
|
{ |
||||||
|
switch ($evento) { |
||||||
|
case CONF_EVENT_TIMERMINO_AGENTE: |
||||||
|
return "Atendimento finalizado pelo agente "; |
||||||
|
case CONF_EVENT_TIMEOUT_CLIENTE: |
||||||
|
return 'Atendimento finalizado pelo sistema por inatividade do cliente'; |
||||||
|
case CONF_EVENT_TIMERMINO_CLIENTE: |
||||||
|
return 'Atendimento finalizado pelo cliente'; |
||||||
|
default: |
||||||
|
return ''; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue