Skip to content

Commit

Permalink
removing user x unity x service
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeriolino committed Jul 14, 2018
1 parent 2195ba0 commit d95e32f
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,11 @@ public function addServico(Request $request, ServicoService $servicoService)
*/
public function removeServicoUnidade(Request $request, Servico $servico, TranslatorInterface $translator)
{
$em = $this->getDoctrine()->getManager();
$unidade = $this->getUser()->getLotacao()->getUnidade();
$envelope = new Envelope();

$su = $this
->getDoctrine()
->getManager()
$su = $em
->getRepository(ServicoUnidade::class)
->get($unidade, $servico);

Expand All @@ -283,21 +282,31 @@ public function removeServicoUnidade(Request $request, Servico $servico, Transla
throw new Exception($translator->trans('error.cannot_remove_disabled_service', [], self::DOMAIN));
}

$contador = $this
->getDoctrine()
->getManager()
->getRepository(Contador::class)
->findOneBy([
'unidade' => $unidade,
'servico' => $servico,
]);

$em = $this->getDoctrine()->getManager();
$em->remove($su);
if ($contador) {
$em->remove($contador);
}
$em->flush();
$em->transactional(function ($em) use ($su, $unidade, $servico) {
$em->remove($su);

$em
->createQueryBuilder()
->delete(Contador::class, 'e')
->where('e.unidade = :unidade AND e.servico = :servico')
->setParameters([
'unidade' => $unidade,
'servico' => $servico,
])
->getQuery()
->execute();

$em
->createQueryBuilder()
->delete(ServicoUsuario::class, 'e')
->where('e.unidade = :unidade AND e.servico = :servico')
->setParameters([
'unidade' => $unidade,
'servico' => $servico,
])
->getQuery()
->execute();
});

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

0 comments on commit d95e32f

Please sign in to comment.