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.             /** @var Flat $flat */
  62.             $flat $this->flatRepository->findOneBy(['id' => $id]);
  63.         }
  64.         if (!$flat) {
  65.             $flat $this->flatRepository->getAllFlatsByResident($resident)[0];
  66.         }
  67.         $this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT$flat);
  68.         $building $flat->getBuilding();
  69.         $settings $this->settingsRepository->getSettingForBuilding($building);
  70.         if ($settings && $settings->getOptions() !== null) {
  71.             $help =  json_decode($settings->getOptions(), true);
  72.             $showAccountBalance $help['showAccountBalance'];
  73.             $showIncomesOutcomes $help['showIncomesOutcomes'];
  74.         }
  75.         return [
  76.             'data' => [
  77.                 'flat' => $flat,
  78.                 'building' => $building,
  79.                 'bills' => $this->moneyLogRepository->getLastFourMoneyLogsByFlat($flat),
  80.                 'statistics' => $this->moneyLogService->getPaymentsStatisticsByFlat($flat'1970-01-01'),
  81.                 'notifications' => $this->notificationRepository->getLastNotificationsByBuilding(
  82.                     $flat->getBuilding(),
  83.                     4
  84.                 ),
  85.                 'showAccountBalance' => $showAccountBalance,
  86.                 'showIncomesOutcomes' => $showIncomesOutcomes,
  87.                 'currency' => $flat->getUser()->getCurrency(),
  88.                 'accountBalance' => $showAccountBalance ?
  89.                     $this->billService->getPaymentsForTekuciRacunCardCurrentOptimized($building->getUser(), $building)['saldo']
  90.                     : null
  91.             ]
  92.         ];
  93.     }
  94.     /**
  95.      * @Rest\Post ("/save-token", name="residents_save_token")
  96.      * @param Request $request
  97.      * @return array<mixed>
  98.      */
  99.     public function saveToken(Request $request): array
  100.     {
  101.         /** @var Resident $resident */
  102.         $resident $this->getUser();
  103.         $data $this->validateForm($requestNotificationTokenType::class);
  104.         /** @var NotificationToken|null $notificationToken */
  105.         $notificationToken $this->notificationTokenRepository->findOneBy(['token' => $data['token']]);
  106.         if ($notificationToken) {
  107.             $notificationToken->setResident($resident);
  108.         } else {
  109.             $notificationToken = new NotificationToken();
  110.             $notificationToken->setResident($resident);
  111.             $notificationToken->setUserType(0);
  112.             $notificationToken->setToken($data['token']);
  113.         }
  114.         $this->notificationTokenRepository->save($notificationToken);
  115.         return [
  116.             'data' => [],
  117.             'message' => "Token saved"
  118.         ];
  119.     }
  120. }