RVRNEXT-30 check if member already has associated transaction #43

Merged
bence merged 1 commits from bugfix/RVRNEXT-29-prevent-deleting-member-who-has-transaction into master 2023-05-02 02:51:02 +02:00
2 changed files with 22 additions and 0 deletions

View File

@ -11,6 +11,7 @@ use RVR\Repository\CommunityRepository;
use RVR\Repository\CommunityMemberRepository; use RVR\Repository\CommunityMemberRepository;
use RVR\Repository\CurrencyExchangeRateRepository; use RVR\Repository\CurrencyExchangeRateRepository;
use RVR\Repository\CurrencyRepository; use RVR\Repository\CurrencyRepository;
use RVR\Repository\TransactionRepository;
use RVR\Repository\UserRepository; use RVR\Repository\UserRepository;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired; use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Response\IContent; use SokoWeb\Interfaces\Response\IContent;
@ -29,6 +30,8 @@ class CommunityController implements IAuthenticationRequired
private CurrencyExchangeRateRepository $currencyExchangeRatesRepository; private CurrencyExchangeRateRepository $currencyExchangeRatesRepository;
private TransactionRepository $transactionRepository;
public function __construct() public function __construct()
{ {
$this->userRepository = new UserRepository(); $this->userRepository = new UserRepository();
@ -36,6 +39,7 @@ class CommunityController implements IAuthenticationRequired
$this->communityMemberRepository = new CommunityMemberRepository(); $this->communityMemberRepository = new CommunityMemberRepository();
$this->currencyRepository = new CurrencyRepository(); $this->currencyRepository = new CurrencyRepository();
$this->currencyExchangeRatesRepository = new CurrencyExchangeRateRepository(); $this->currencyExchangeRatesRepository = new CurrencyExchangeRateRepository();
$this->transactionRepository = new TransactionRepository();
} }
public function isAuthenticationRequired(): bool 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); \Container::$persistentDataManager->deleteFromDb($communityMember);
return new JsonContent(['success' => true]); return new JsonContent(['success' => true]);

View File

@ -4,6 +4,7 @@ use Container;
use Generator; use Generator;
use RVR\PersistentData\Model\Community; use RVR\PersistentData\Model\Community;
use RVR\PersistentData\Model\Transaction; use RVR\PersistentData\Model\Transaction;
use RVR\PersistentData\Model\User;
use SokoWeb\Database\Query\Select; use SokoWeb\Database\Query\Select;
class TransactionRepository class TransactionRepository
@ -25,6 +26,16 @@ class TransactionRepository
return $this->selectAllByCommunity($community)->count(); 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 public function getPagedByCommunity(Community $community, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator
{ {
$select = new Select(Container::$dbConnection); $select = new Select(Container::$dbConnection);