Skip to content

Commit

Permalink
feat: Application behavior settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriolino committed Jan 19, 2025
1 parent 7c5eb24 commit dcf3aa3
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 50 deletions.
148 changes: 107 additions & 41 deletions src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
use Novosga\AttendanceBundle\Dto\SetLocalDto;
use Novosga\AttendanceBundle\NovosgaAttendanceBundle;
use Novosga\Entity\ServicoUsuarioInterface;
use Novosga\Entity\UnidadeInterface;
use Novosga\Entity\UsuarioInterface;
use Novosga\Event\PreUserSetLocalEvent;
use Novosga\Event\UserSetLocalEvent;
use Novosga\Form\ClienteType;
use Novosga\Http\Envelope;
use Novosga\Repository\AtendimentoRepositoryInterface;
use Novosga\Repository\ClienteRepositoryInterface;
use Novosga\Repository\LocalRepositoryInterface;
use Novosga\Repository\ServicoRepositoryInterface;
use Novosga\Repository\ServicoUnidadeRepositoryInterface;
use Novosga\Repository\UsuarioRepositoryInterface;
use Novosga\Service\ApplicationSettingsServiceInterface;
use Novosga\Service\AtendimentoServiceInterface;
use Novosga\Service\ClienteServiceInterface;
use Novosga\Service\FilaServiceInterface;
Expand All @@ -51,14 +54,19 @@
#[Route("/", name: "novosga_attendance_")]
class DefaultController extends AbstractController
{
public function __construct(
private readonly TranslatorInterface $translator,
private readonly ApplicationSettingsServiceInterface $settingsService,
) {
}

#[Route("/", name: "index", methods: ["GET"])]
public function index(
ServicoRepositoryInterface $servicoRepository,
LocalRepositoryInterface $localRepository,
AtendimentoServiceInterface $atendimentoService,
UsuarioServiceInterface $usuarioService,
ServicoServiceInterface $servicoService,
TranslatorInterface $translator
): Response {
/** @var UsuarioInterface */
$usuario = $this->getUser();
Expand All @@ -81,10 +89,10 @@ public function index(

$domain = NovosgaAttendanceBundle::getDomain();
$tiposAtendimento = [
FilaServiceInterface::TIPO_TODOS => $translator->trans('label.all', [], $domain),
FilaServiceInterface::TIPO_NORMAL => $translator->trans('label.no_priority', [], $domain),
FilaServiceInterface::TIPO_PRIORIDADE => $translator->trans('label.priority', [], $domain),
FilaServiceInterface::TIPO_AGENDAMENTO => $translator->trans('label.schedule', [], $domain),
FilaServiceInterface::TIPO_TODOS => $this->translator->trans('label.all', [], $domain),
FilaServiceInterface::TIPO_NORMAL => $this->translator->trans('label.no_priority', [], $domain),
FilaServiceInterface::TIPO_PRIORIDADE => $this->translator->trans('label.priority', [], $domain),
FilaServiceInterface::TIPO_AGENDAMENTO => $this->translator->trans('label.schedule', [], $domain),
];

$atendimentoAtual = $atendimentoService->getAtendimentoAndamento($usuario, $unidade);
Expand All @@ -100,6 +108,7 @@ public function index(
}, $servicosUsuario);

$servicosIndisponiveis = $servicoService->servicosIndisponiveis($unidade, $usuario);
$settings = $this->settingsService->loadBehaviorSettings();

return $this->render('@NovosgaAttendance/default/index.html.twig', [
'time' => time() * 1000,
Expand All @@ -113,6 +122,7 @@ public function index(
'local' => $local,
'numeroLocal' => $numeroLocal,
'tipoAtendimento' => $tipo,
'settings' => $settings,
]);
}

Expand Down Expand Up @@ -193,7 +203,6 @@ public function setLocal(
LocalRepositoryInterface $localRepository,
UsuarioServiceInterface $usuarioService,
EventDispatcherInterface $dispatcher,
TranslatorInterface $translator,
#[MapRequestPayload()] SetLocalDto $data,
): Response {
$envelope = new Envelope();
Expand All @@ -202,7 +211,7 @@ public function setLocal(
$tipo = ($data->tipoAtendimento ?? FilaServiceInterface::TIPO_TODOS);
if ($data->numeroLocal <= 0) {
throw new Exception(
$translator->trans(
$this->translator->trans(
'error.place_number',
[],
NovosgaAttendanceBundle::getDomain(),
Expand All @@ -219,7 +228,7 @@ public function setLocal(

if (!in_array($tipo, $tipos)) {
throw new Exception(
$translator->trans(
$this->translator->trans(
'error.queue_type',
[],
NovosgaAttendanceBundle::getDomain(),
Expand All @@ -230,7 +239,7 @@ public function setLocal(
$local = $localRepository->find($data->local);
if (!$local) {
throw new Exception(
$translator->trans('error.place', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.place', [], NovosgaAttendanceBundle::getDomain())
);
}

Expand Down Expand Up @@ -316,7 +325,6 @@ public function chamar(
LocalRepositoryInterface $localRepository,
AtendimentoServiceInterface $atendimentoService,
UsuarioServiceInterface $usuarioService,
TranslatorInterface $translator
): Response {
$envelope = new Envelope();
/** @var UsuarioInterface */
Expand Down Expand Up @@ -347,7 +355,7 @@ public function chamar(

if (!$atendimento) {
throw new Exception(
$translator->trans('error.queue.empty', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.queue.empty', [], NovosgaAttendanceBundle::getDomain())
);
}

Expand All @@ -368,12 +376,11 @@ public function chamarServico(
ServicoRepositoryInterface $servicoRepository,
AtendimentoServiceInterface $atendimentoService,
UsuarioServiceInterface $usuarioService,
TranslatorInterface $translator,
int $id,
): Response {
$servico = $servicoRepository->find($id);
if (!$servico) {
throw $this->createNotFoundException();
$settings = $this->settingsService->loadBehaviorSettings();
if (!$settings->callTicketByService) {
throw new Exception('Chamar senha por serviço não é permitido');
}

$envelope = new Envelope();
Expand All @@ -382,12 +389,11 @@ public function chamarServico(
$unidade = $usuario->getLotacao()->getUnidade();

// verifica se ja esta atendendo alguem
$atendimento = $atendimentoService->getAtendimentoAndamento($usuario->getId(), $unidade);
$this->checkAtendimentoEmAndamento($atendimentoService, $usuario, $unidade);

if ($atendimento) {
throw new Exception(
$translator->trans('error.attendance.in_process', [], NovosgaAttendanceBundle::getDomain())
);
$servico = $servicoRepository->find($id);
if (!$servico) {
throw $this->createNotFoundException();
}

$localId = $this->getLocalAtendimento($usuarioService, $usuario);
Expand All @@ -413,7 +419,7 @@ public function chamarServico(

if (!$atendimento) {
throw new Exception(
$translator->trans('error.queue.empty', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.queue.empty', [], NovosgaAttendanceBundle::getDomain())
);
}

Expand All @@ -426,21 +432,73 @@ public function chamarServico(
}

/**
* Inicia o atendimento com o proximo da fila.
* Chama ou rechama um atendimento mesmo que fora de ordem
*/
#[Route("/iniciar", name: "iniciar", methods: ["POST"])]
public function iniciar(
#[Route("/chamar/atendimento/{id}", name: "chamar_atendimento", methods: ["POST"])]
public function chamarAtendimento(
LocalRepositoryInterface $localRepository,
AtendimentoRepositoryInterface $atendimentoRepository,
AtendimentoServiceInterface $atendimentoService,
TranslatorInterface $translator
UsuarioServiceInterface $usuarioService,
int $id,
): Response {
$settings = $this->settingsService->loadBehaviorSettings();
if (!$settings->callTicketOutOfOrder) {
throw new Exception('Chamar senha fora de ordem serviço não é permitido');
}

$envelope = new Envelope();
/** @var UsuarioInterface */
$usuario = $this->getUser();
$unidade = $usuario->getLotacao()->getUnidade();

// verifica se ja esta atendendo alguem
$this->checkAtendimentoEmAndamento($atendimentoService, $usuario, $unidade);

$atendimento = $atendimentoRepository->find($id);
if (!$atendimento) {
throw $this->createNotFoundException();
}

$localId = $this->getLocalAtendimento($usuarioService, $usuario);
$numeroLocal = $this->getNumeroLocalAtendimento($usuarioService, $usuario);
$local = $localRepository->find($localId);

$sucesso = $atendimentoService->chamarAtendimento(
$atendimento,
$usuario,
$local,
$numeroLocal,
);

if (!$sucesso) {
throw new Exception(
$this->translator->trans('error.queue.empty', [], NovosgaAttendanceBundle::getDomain())
);
}

$atendimentoService->chamarSenha($atendimento, $usuario);

$data = $atendimento->jsonSerialize();
$envelope->setData($data);

return $this->json($envelope);
}

/**
* Inicia o atendimento com o proximo da fila.
*/
#[Route("/iniciar", name: "iniciar", methods: ["POST"])]
public function iniciar(AtendimentoServiceInterface $atendimentoService): Response
{
/** @var UsuarioInterface */
$usuario = $this->getUser();
$unidade = $usuario->getLotacao()->getUnidade();
$atual = $atendimentoService->getAtendimentoAndamento($usuario->getId(), $unidade);

if (!$atual) {
throw new Exception(
$translator->trans('error.attendance.empty', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.attendance.empty', [], NovosgaAttendanceBundle::getDomain())
);
}

Expand All @@ -457,18 +515,16 @@ public function iniciar(
* Marca o atendimento como nao compareceu.
*/
#[Route("/nao_compareceu", name: "naocompareceu", methods: ["POST"])]
public function naoCompareceu(
AtendimentoServiceInterface $atendimentoService,
TranslatorInterface $translator
): Response {
public function naoCompareceu(AtendimentoServiceInterface $atendimentoService): Response
{
/** @var UsuarioInterface */
$usuario = $this->getUser();
$unidade = $usuario->getLotacao()->getUnidade();
$atual = $atendimentoService->getAtendimentoAndamento($usuario->getId(), $unidade);

if (!$atual) {
throw new Exception(
$translator->trans('error.attendance.empty', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.attendance.empty', [], NovosgaAttendanceBundle::getDomain())
);
}

Expand All @@ -489,7 +545,6 @@ public function encerrar(
UsuarioRepositoryInterface $usuarioRepository,
ServicoRepositoryInterface $servicoRepository,
AtendimentoServiceInterface $atendimentoService,
TranslatorInterface $translator,
#[MapRequestPayload] EncerrarAtendimentoDto $data,
): Response {
$envelope = new Envelope();
Expand All @@ -501,13 +556,13 @@ public function encerrar(

if (!$atual) {
throw new Exception(
$translator->trans('error.attendance.not_in_process', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.attendance.not_in_process', [], NovosgaAttendanceBundle::getDomain())
);
}

if (empty($data->servicos)) {
throw new Exception(
$translator->trans('error.attendance.no_service', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.attendance.no_service', [], NovosgaAttendanceBundle::getDomain())
);
}

Expand Down Expand Up @@ -545,7 +600,6 @@ public function encerrar(
#[Route("/redirecionar", name: "redirecionar", methods: ["POST"])]
public function redirecionar(
AtendimentoServiceInterface $atendimentoService,
TranslatorInterface $translator,
#[MapRequestPayload] RedirecionarAtendimentoDto $data,
): Response {
$envelope = new Envelope();
Expand All @@ -557,14 +611,14 @@ public function redirecionar(

if (!$atual) {
throw new Exception(
$translator->trans('error.attendance.not_in_process', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.attendance.not_in_process', [], NovosgaAttendanceBundle::getDomain())
);
}

$redirecionado = $atendimentoService->redirecionar($atual, $usuario, $data->servico, $data->usuario);
if (!$redirecionado->getId()) {
throw new Exception(
$translator->trans(
$this->translator->trans(
'error.attendance.redirect',
[
'%atendimento%' => $atual->getId(),
Expand All @@ -581,7 +635,6 @@ public function redirecionar(
#[Route("/info_senha/{id}", name: "infosenha", methods: ["GET"])]
public function infoSenha(
AtendimentoServiceInterface $atendimentoService,
TranslatorInterface $translator,
int $id,
): Response {
$envelope = new Envelope();
Expand All @@ -592,7 +645,7 @@ public function infoSenha(

if (!$atendimento) {
throw new Exception(
$translator->trans('error.attendance.invalid', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.attendance.invalid', [], NovosgaAttendanceBundle::getDomain())
);
}

Expand Down Expand Up @@ -623,7 +676,6 @@ public function consultaSenha(Request $request, AtendimentoServiceInterface $ate
public function usuarios(
UsuarioRepositoryInterface $usuarioRepository,
ServicoUnidadeRepositoryInterface $servicoUnidadeRepository,
TranslatorInterface $translator,
int $servicoId
): Response {
$envelope = new Envelope();
Expand All @@ -634,7 +686,7 @@ public function usuarios(

if (!$servicoUnidade) {
throw new Exception(
$translator->trans('error.service.invalid', [], NovosgaAttendanceBundle::getDomain())
$this->translator->trans('error.service.invalid', [], NovosgaAttendanceBundle::getDomain())
);
}

Expand Down Expand Up @@ -668,4 +720,18 @@ private function getTipoAtendimento(UsuarioServiceInterface $usuarioService, Usu

return $tipoAtendimento;
}

private function checkAtendimentoEmAndamento(
AtendimentoServiceInterface $atendimentoService,
UsuarioInterface $usuario,
UnidadeInterface $unidade,
): void {
// verifica se ja esta atendendo alguem
$atendimento = $atendimentoService->getAtendimentoAndamento($usuario, $unidade);
if ($atendimento) {
throw new Exception(
$this->translator->trans('error.attendance.in_process', [], NovosgaAttendanceBundle::getDomain())
);
}
}
}
14 changes: 14 additions & 0 deletions src/Resources/public/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@
});
},

chamarAtendimento(atendimento) {
this.busy = true;
App.ajax({
url: App.url(`/novosga.attendance/chamar/atendimento/${atendimento.id}`),
type: 'post',
success: (response) => {
this.atendimento = response.data;
},
complete: () => {
setTimeout(() => this.busy = false, 3 * 1000);
}
});
},

iniciar() {
App.ajax({
url: App.url('/novosga.attendance/iniciar'),
Expand Down
Loading

0 comments on commit dcf3aa3

Please sign in to comment.