src/Controller/ApiV2/Residents/HomeController.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ApiV2\Residents;
  3. use App\Controller\ApiV2\AbstractController;
  4. use App\Entity\Building;
  5. use App\Entity\Flat;
  6. use App\Entity\NotificationToken;
  7. use App\Entity\Resident;
  8. use App\Form\ApiV2\NotificationTokenType;
  9. use App\Repository\BuildingRepository;
  10. use App\Repository\FlatRepository;
  11. use App\Repository\MoneyLogRepository;
  12. use App\Repository\NotificationRepository;
  13. use App\Repository\NotificationTokenRepository;
  14. use App\Repository\SettingsRepository;
  15. use App\Security\Voter\FlatVoter;
  16. use App\Services\BillService;
  17. use App\Services\MoneyLogService;
  18. use Doctrine\ORM\NonUniqueResultException;
  19. use Doctrine\ORM\NoResultException;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use FOS\RestBundle\Controller\Annotations as Rest;
  22. class HomeController extends AbstractController
  23. {
  24.     /**
  25.      * @param BuildingRepository $buildingRepository
  26.      * @param MoneyLogRepository $moneyLogRepository
  27.      * @param NotificationRepository $notificationRepository
  28.      * @param MoneyLogService $moneyLogService
  29.      * @param BillService $billService
  30.      * @param NotificationTokenRepository $notificationTokenRepository
  31.      * @param SettingsRepository $settingsRepository
  32.      * @param FlatRepository $flatRepository
  33.      */
  34.     public function __construct(
  35.         BuildingRepository $buildingRepository,
  36.         private readonly MoneyLogRepository $moneyLogRepository,
  37.         private readonly NotificationRepository $notificationRepository,
  38.         private readonly MoneyLogService $moneyLogService,
  39.         private readonly BillService $billService,
  40.         private readonly NotificationTokenRepository $notificationTokenRepository,
  41.         private readonly SettingsRepository $settingsRepository,
  42.         private readonly FlatRepository $flatRepository
  43.     ) {
  44.         parent::__construct($buildingRepository);
  45.     }
  46.     /**
  47.      * @Rest\Get ("/dashboard", name="residents_dashboard_index")
  48.      * @param Request $request
  49.      * @return array<string, array<string, Flat|Building>>
  50.      * @throws NoResultException
  51.      * @throws NonUniqueResultException
  52.      */
  53.     public function index(Request $request): array
  54.     {
  55.         /** @var Resident $resident */
  56.         $resident $this->getUser();
  57.         $showAccountBalance false;
  58.         $showIncomesOutcomes false;
  59.         $flat null;
  60.         if ($id $request->get('id')) {
  61.             $flat $this->flatRepository->findOneBy(['id' => $id]);
  62.         }
  63.         if (!$flat) {
  64.             $flat $this->flatRepository->getAllFlatsByResident($resident)[0];
  65.         }
  66.         $this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT$flat);
  67.         $building $flat->getBuilding();
  68.         $settings $this->settingsRepository->getSettingForBuilding($building);
  69.         if ($settings && $settings->getOptions() !== null) {
  70.             $help =  json_decode($settings->getOptions(), true);
  71.             $showAccountBalance $help['showAccountBalance'];
  72.             $showIncomesOutcomes $help['showIncomesOutcomes'];
  73.         }
  74.         return [
  75.             'data' => [
  76.                 'flat' => $flat,
  77.                 'building' => $building,
  78.                 'bills' => $this->moneyLogRepository->getLastFourMoneyLogsByFlat($flat),
  79.                 'statistics' => $this->moneyLogService->getPaymentsStatisticsByFlat($flat'1970-01-01'),
  80.                 'notifications' => $this->notificationRepository->getLastNotificationsByBuilding(
  81.                     $flat->getBuilding(),
  82.                     4
  83.                 ),
  84.                 'showAccountBalance' => $showAccountBalance,
  85.                 'showIncomesOutcomes' => $showIncomesOutcomes,
  86.                 'accountBalance' => $showAccountBalance ?
  87.                     $this->billService->getPaymentsForTekuciRacunCardCurrent($building->getUser(), $building)['saldo']
  88.                     : null
  89.             ]
  90.         ];
  91.     }
  92.     /**
  93.      * @Rest\Post ("/save-token", name="residents_save_token")
  94.      * @param Request $request
  95.      * @return array<mixed>
  96.      */
  97.     public function saveToken(Request $request): array
  98.     {
  99.         /** @var Resident $resident */
  100.         $resident $this->getUser();
  101.         $data $this->validateForm($requestNotificationTokenType::class);
  102.         /** @var NotificationToken|null $notificationToken */
  103.         $notificationToken $this->notificationTokenRepository->findOneBy(['token' => $data['token']]);
  104.         if ($notificationToken) {
  105.             $notificationToken->setResident($resident);
  106.         } else {
  107.             $notificationToken = new NotificationToken();
  108.             $notificationToken->setResident($resident);
  109.             $notificationToken->setUserType(0);
  110.             $notificationToken->setToken($data['token']);
  111.         }
  112.         $this->notificationTokenRepository->save($notificationToken);
  113.         return [
  114.             'data' => [],
  115.             'message' => "Token saved"
  116.         ];
  117.     }
  118. }