diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index 77b1aa3..6873e4a 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -11,6 +11,7 @@ use RVR\Repository\CommunityRepository; use RVR\Repository\CommunityMemberRepository; use RVR\Repository\CurrencyExchangeRateRepository; use RVR\Repository\CurrencyRepository; +use RVR\Repository\TransactionRepository; use RVR\Repository\UserRepository; use SokoWeb\Interfaces\Authentication\IAuthenticationRequired; use SokoWeb\Interfaces\Response\IContent; @@ -29,6 +30,8 @@ class CommunityController implements IAuthenticationRequired private CurrencyExchangeRateRepository $currencyExchangeRatesRepository; + private TransactionRepository $transactionRepository; + public function __construct() { $this->userRepository = new UserRepository(); @@ -36,6 +39,7 @@ class CommunityController implements IAuthenticationRequired $this->communityMemberRepository = new CommunityMemberRepository(); $this->currencyRepository = new CurrencyRepository(); $this->currencyExchangeRatesRepository = new CurrencyExchangeRateRepository(); + $this->transactionRepository = new TransactionRepository(); } public function isAuthenticationRequired(): bool @@ -220,6 +224,13 @@ class CommunityController implements IAuthenticationRequired ]); } + \Container::$persistentDataManager->loadRelationsFromDb($communityMember, false, ['user']); + if ($this->transactionRepository->isAnyForUser($communityMember->getUser())) { + return new JsonContent([ + 'error' => ['errorText' => 'There are transactions where the member is payer or payee!'] + ]); + } + \Container::$persistentDataManager->deleteFromDb($communityMember); return new JsonContent(['success' => true]); diff --git a/src/Repository/TransactionRepository.php b/src/Repository/TransactionRepository.php index 4ef85bc..6ab3f78 100644 --- a/src/Repository/TransactionRepository.php +++ b/src/Repository/TransactionRepository.php @@ -4,6 +4,7 @@ use Container; use Generator; use RVR\PersistentData\Model\Community; use RVR\PersistentData\Model\Transaction; +use RVR\PersistentData\Model\User; use SokoWeb\Database\Query\Select; class TransactionRepository @@ -25,6 +26,16 @@ class TransactionRepository return $this->selectAllByCommunity($community)->count(); } + public function isAnyForUser(User $user): bool + { + $select = new Select(Container::$dbConnection, Transaction::getTable()); + $select->where('payer_user_id', '=', $user->getId()); + $select->orWhere('payee_user_id', '=', $user->getId()); + $select->orWhere('payee_user_id', '=', null); + + return $select->count() > 0; + } + public function getPagedByCommunity(Community $community, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator { $select = new Select(Container::$dbConnection);