when($params['modulo_ids'] ?? null, fn ($q, $moduloIds) => $q ->whereHas('fila', fn ($q) => $q ->whereHas('opcaoEntrada', fn ($q) => $q ->whereIn('modulo_id', $moduloIds) ) ) ) ->orderByDesc('created_at') ->take(4) ->get() ->map(fn ($chamada) => [ 'id' => $chamada->id, 'paciente' => [ 'cidadao' => [ 'preferencia_nome_id' => $chamada->paciente->cidadao->preferencia_nome_id ?? null, 'nome' => $chamada->paciente->cidadao->nome ?? null, 'nome_social' => $chamada->paciente->cidadao->nome_social ?? null, ], ], 'sala' => [ 'nome' => $chamada->sala->nome ?? null, ], 'senha' => $chamada->fila->senha ?? null, ]); } public static function store() { self::validateStore(); $fila = Fila::findOrFail(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' => $fila->paciente_id ?? null, ]); FilaService::dispatch($fila->centro_custo_id); $parametrizacaoInfraestrutura = ParametrizacaoInfraestrutura::where( 'infraestrutura_id', $fila->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; } private static function validateStore() { request()->validate([ 'fila_id' => [ 'required', 'integer', Rule::exists(Fila::class, 'id'), ], 'tela_id' => [ 'required', 'integer', Rule::exists(Tela::class, 'id'), ], 'sala_id' => [ 'nullable', 'integer', Rule::exists(Sala::class, 'id'), ], ]); } }