<?php
namespace App\Controller\ApiV2\Residents;
use App\Controller\ApiV2\AbstractController;
use App\Entity\Building;
use App\Entity\Flat;
use App\Entity\NotificationToken;
use App\Entity\Resident;
use App\Form\ApiV2\NotificationTokenType;
use App\Repository\BuildingRepository;
use App\Repository\FlatRepository;
use App\Repository\MoneyLogRepository;
use App\Repository\NotificationRepository;
use App\Repository\NotificationTokenRepository;
use App\Repository\SettingsRepository;
use App\Security\Voter\FlatVoter;
use App\Services\BillService;
use App\Services\MoneyLogService;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Symfony\Component\HttpFoundation\Request;
use FOS\RestBundle\Controller\Annotations as Rest;
class HomeController extends AbstractController
{
/**
* @param BuildingRepository $buildingRepository
* @param MoneyLogRepository $moneyLogRepository
* @param NotificationRepository $notificationRepository
* @param MoneyLogService $moneyLogService
* @param BillService $billService
* @param NotificationTokenRepository $notificationTokenRepository
* @param SettingsRepository $settingsRepository
* @param FlatRepository $flatRepository
*/
public function __construct(
BuildingRepository $buildingRepository,
private readonly MoneyLogRepository $moneyLogRepository,
private readonly NotificationRepository $notificationRepository,
private readonly MoneyLogService $moneyLogService,
private readonly BillService $billService,
private readonly NotificationTokenRepository $notificationTokenRepository,
private readonly SettingsRepository $settingsRepository,
private readonly FlatRepository $flatRepository
) {
parent::__construct($buildingRepository);
}
/**
* @Rest\Get ("/dashboard", name="residents_dashboard_index")
* @param Request $request
* @return array<string, array<string, Flat|Building>>
* @throws NoResultException
* @throws NonUniqueResultException
*/
public function index(Request $request): array
{
/** @var Resident $resident */
$resident = $this->getUser();
$showAccountBalance = false;
$showIncomesOutcomes = false;
$flat = null;
if ($id = $request->get('id')) {
$flat = $this->flatRepository->findOneBy(['id' => $id]);
}
if (!$flat) {
$flat = $this->flatRepository->getAllFlatsByResident($resident)[0];
}
$this->denyAccessUnlessGranted(FlatVoter::FLAT_BELONGS_TO_RESIDENT, $flat);
$building = $flat->getBuilding();
$settings = $this->settingsRepository->getSettingForBuilding($building);
if ($settings && $settings->getOptions() !== null) {
$help = json_decode($settings->getOptions(), true);
$showAccountBalance = $help['showAccountBalance'];
$showIncomesOutcomes = $help['showIncomesOutcomes'];
}
return [
'data' => [
'flat' => $flat,
'building' => $building,
'bills' => $this->moneyLogRepository->getLastFourMoneyLogsByFlat($flat),
'statistics' => $this->moneyLogService->getPaymentsStatisticsByFlat($flat, '1970-01-01'),
'notifications' => $this->notificationRepository->getLastNotificationsByBuilding(
$flat->getBuilding(),
4
),
'showAccountBalance' => $showAccountBalance,
'showIncomesOutcomes' => $showIncomesOutcomes,
'accountBalance' => $showAccountBalance ?
$this->billService->getPaymentsForTekuciRacunCardCurrent($building->getUser(), $building)['saldo']
: null
]
];
}
/**
* @Rest\Post ("/save-token", name="residents_save_token")
* @param Request $request
* @return array<mixed>
*/
public function saveToken(Request $request): array
{
/** @var Resident $resident */
$resident = $this->getUser();
$data = $this->validateForm($request, NotificationTokenType::class);
/** @var NotificationToken|null $notificationToken */
$notificationToken = $this->notificationTokenRepository->findOneBy(['token' => $data['token']]);
if ($notificationToken) {
$notificationToken->setResident($resident);
} else {
$notificationToken = new NotificationToken();
$notificationToken->setResident($resident);
$notificationToken->setUserType(0);
$notificationToken->setToken($data['token']);
}
$this->notificationTokenRepository->save($notificationToken);
return [
'data' => [],
'message' => "Token saved"
];
}
}