This commit is contained in:
99
api/app/Services/ProntoAtendimento/Fila/ChamadaService.php
Normal file
99
api/app/Services/ProntoAtendimento/Fila/ChamadaService.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ProntoAtendimento\Fila;
|
||||
|
||||
use App\Events\PainelSenha\ChamadaEvent;
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
||||
use App\Models\PainelSenha\Chamada;
|
||||
use App\Models\PainelSenha\ParametrizacaoInfraestrutura;
|
||||
use App\Models\ProntoAtendimento\Atendimento\Atendimento;
|
||||
use App\Services\Service;
|
||||
use Exception;
|
||||
|
||||
class ChamadaService extends Service
|
||||
{
|
||||
public static function index($request)
|
||||
{
|
||||
return Chamada::where('tela_id', $request['tela_id'])
|
||||
->when($request['modulo_ids'] ?? null, fn ($q, $moduloIds) => $q
|
||||
->whereHas('fila', fn ($q) => $q
|
||||
->whereHas('opcaoEntrada', fn ($q) => $q
|
||||
->whereIn('modulo_id', $moduloIds)
|
||||
)
|
||||
)
|
||||
)
|
||||
->orderBy('created_at', 'desc')
|
||||
->take(4)
|
||||
->get()
|
||||
->map(function($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'paciente' => [
|
||||
'cidadao' => [
|
||||
'preferencia_nome_id' => $query->paciente->cidadao->preferencia_nome_id ?? null,
|
||||
'nome' => $query->paciente->cidadao->nome ?? null,
|
||||
'nome_social' => $query->paciente->cidadao->nome_social ?? null,
|
||||
],
|
||||
],
|
||||
'sala' => [
|
||||
'nome' => $query->sala->nome ?? null,
|
||||
],
|
||||
'senha' => $query->fila->senha ?? null,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
$atendimento = Atendimento::where('fila_id', $request->fila_id)
|
||||
->orderBy('created_at', 'desc')
|
||||
->first();
|
||||
|
||||
$fila = FilaService::show($request->fila_id);
|
||||
|
||||
$chamada = Chamada::create([
|
||||
'fila_id' => $fila->id,
|
||||
'status_id' => $fila->status_id,
|
||||
'tela_id' => $request->tela_id,
|
||||
'sala_id' => $request->sala_id,
|
||||
'paciente_id' => $atendimento->faa->paciente_id ?? null,
|
||||
]);
|
||||
|
||||
$centroCusto = CentroCusto::findOrFail($fila->centro_custo_id);
|
||||
|
||||
FilaService::dispatch($centroCusto, $request->sala_id);
|
||||
|
||||
$parametrizacaoInfraestrutura = ParametrizacaoInfraestrutura::where(
|
||||
'infraestrutura_id',
|
||||
$centroCusto->infraestrutura_id
|
||||
)->first();
|
||||
|
||||
$moduloIds = is_null($parametrizacaoInfraestrutura)
|
||||
? collect()
|
||||
: $parametrizacaoInfraestrutura->modulos->pluck('id');
|
||||
|
||||
try {
|
||||
|
||||
$paramsChamada = [
|
||||
'tela_id' => $chamada->tela_id,
|
||||
'modulo_ids' => null,
|
||||
];
|
||||
|
||||
$moduloIds->each(function ($moduloId) use ($paramsChamada) {
|
||||
|
||||
$paramsChamada['modulo_ids'] = [$moduloId];
|
||||
|
||||
ChamadaEvent::dispatch(
|
||||
$paramsChamada['tela_id'],
|
||||
$moduloId,
|
||||
self::index($paramsChamada)
|
||||
);
|
||||
});
|
||||
} catch (Exception $error) {
|
||||
|
||||
// TODO: Gravar exception em um arquivo.
|
||||
}
|
||||
|
||||
return $chamada;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user