<?php
namespace App\Controller;
use App\Document\AppealExecution;
use App\Document\Appeal;
use App\Document\Resource;
use App\Service\AppealExecutionsHelper;
use Doctrine\ODM\MongoDB\DocumentManager;
use Psr\Log\LoggerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DesktopController extends AbstractController
{
public function __construct(
private DocumentManager $dm,
private LoggerInterface $logger,
private AppealExecutionsHelper $appealExecutionsHelper
)
{
}
#[Route('/desktop', name: 'desktop', methods: ['GET',])]
#[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PROVIDER')")]
public function index(Request $request): Response
{
return $this->render('desktop/index.html.twig');
}
#[Route('/get_executions_count', name: 'get_executions_count', methods: ['GET',])]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
public function getExecutionsCount(Request $request): JsonResponse
{
$appealsByResourcesTypeNumber = count($this->dm->getRepository(Appeal::class)->getByResourcesType(
$this->dm->getRepository(Resource::class)->getByUserId($this->getUser()->getId())
)->toArray());
$appealExecutionNumber = count($this->appealExecutionsHelper->removeBrokenExecutions(
$this->dm->createQueryBuilder(AppealExecution::class)
->field('resourceOwner.id')->equals($this->getUser()->getId())
->field('status')->equals(AppealExecution::STATUS_CONFIRMATION)
->getQuery()->execute()->toArray())
);
$resourceAppealsCount = count($this->appealExecutionsHelper->removeBrokenExecutions(
$this->dm->createQueryBuilder(AppealExecution::class)
->field('resourceOwner.id')->equals($this->getUser()->getId())
->field('status')->equals(AppealExecution::STATUS_BOOKED)
->getQuery()->execute()->toArray())
);
$executionAppealsNumber = count($this->appealExecutionsHelper->removeBrokenExecutions(
$this->dm->createQueryBuilder(AppealExecution::class)
->field('appealOwner.id')->equals($this->getUser()->getId())
->field('status')->equals(AppealExecution::STATUS_BOOKED)
->getQuery()->execute()->toArray())
);
$executionInWorkNumber = count($this->appealExecutionsHelper->removeBrokenExecutions(
$this->dm->createQueryBuilder(AppealExecution::class)
->field('resourceOwner.id')->equals($this->getUser()->getId())
->field('status')->equals(AppealExecution::STATUS_WORK)
->getQuery()->execute()->toArray())
);
return $this->json([
'sum' => $appealsByResourcesTypeNumber + $appealExecutionNumber + $resourceAppealsCount +
$executionAppealsNumber + $executionInWorkNumber,
'appealsByResourcesTypeNumber' => $appealsByResourcesTypeNumber,
'appealExecutionNumber' => $appealExecutionNumber,
'resourceAppealsCount' => $resourceAppealsCount,
'executionAppealsNumber' => $executionAppealsNumber,
'executionInWorkNumber' => $executionInWorkNumber
]);
}
#[Route('/desktop/appeals', name: 'catalog_appeals', methods: ['GET',])]
#[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PROVIDER')")]
public function appealsCatalog(Request $request): Response
{
$userResources = $this->dm->getRepository(Resource::class)
->getByUserId($this->getUser()->getId());
$appealsByResourcesType = $this->dm->getRepository(Appeal::class)
->getByResourcesType($userResources);
return $this->render('/desktop/appeals-catalog.html.twig', [
'appeals' => $appealsByResourcesType,
]);
}
#[Route('/desktop/appeals/confirmation', name: 'resource_confirmation_list', methods: ['GET',])]
#[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PROVIDER')")]
public function resourcesForConfirmation(Request $request): Response
{
$appealExecution = $this->appealExecutionsHelper->removeBrokenExecutions(
$this->dm->createQueryBuilder(AppealExecution::class)
->field('resourceOwner.id')->equals($this->getUser()->getId())
->field('status')->equals(AppealExecution::STATUS_CONFIRMATION)
->getQuery()->execute()
);
return $this->render(
'/desktop/resources-confirmation.html.twig', [
'appealExecutions' => $appealExecution,
]
);
}
#[Route('desktop/booked-appeals', name: 'appeals_to_perform', methods: ['GET',])]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
public function appealsToPerform(Request $request): Response
{
$resourcesAppeals = $this->appealExecutionsHelper->removeBrokenExecutions(
$this->dm->createQueryBuilder(AppealExecution::class)
->field('resourceOwner.id')->equals($this->getUser()->getId())
->field('status')->equals(AppealExecution::STATUS_BOOKED)
->getQuery()->execute()
);
return $this->render('desktop/appeals-ready-to-perform.html.twig', [
'appeal_resource' => $resourcesAppeals,
]);
}
#[Route('execution/booking-appeals', name: 'booked_appeals', methods: ['POST', 'GET'])]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
public function bookedAppeals(Request $request): Response
{
$executionAppeals = $this->appealExecutionsHelper->removeBrokenExecutions(
$this->dm->createQueryBuilder(AppealExecution::class)
->field('appealOwner.id')->equals($this->getUser()->getId())
->field('status')->equals(AppealExecution::STATUS_BOOKED)
->getQuery()->execute()
);
return $this->render('desktop/booked-appeals.html.twig', [
'executions' => $executionAppeals,
]);
}
#[Route('desktop/appeals-in-work', name: 'appeals_in_work', methods: ['GET'])]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
public function appealsInWork(Request $request): Response
{
return $this->render('desktop/appeals-in-work.html.twig', [
'executions' => $this->appealExecutionsHelper->removeBrokenExecutions(
$this->dm->getRepository(AppealExecution::class)
->getAppeals($this->getUser(), [AppealExecution::STATUS_WORK])
),
]);
}
}