Skip to content

Commit

Permalink
feat: Sanitize situations when multiaccount already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
KminekMatej committed Sep 6, 2024
1 parent fe22996 commit 08daa08
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions app/module/multiaccount/manager/MultiaccountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tymy\Module\Multiaccount\Manager;

use Contributte\Translation\Translator;
use Nette\Database\Table\ActiveRow;
use Nette\Database\UniqueConstraintViolationException;
use Nette\NotImplementedException;
use Nette\Utils\DateTime;
use Tymy\Module\Core\Factory\ManagerFactory;
Expand Down Expand Up @@ -74,7 +76,7 @@ public function create(array $data, $resourceId = null): BaseModel
$sourceTeam = $this->teamManager->getTeam();
$sourceUserId = $this->user->getId();

if (!$targetTeam instanceof \Tymy\Module\Team\Model\Team) {
if (!$targetTeam instanceof Team) {
$this->respondNotFound();
}

Expand Down Expand Up @@ -136,11 +138,16 @@ private function addTeamUnderAccount(int $teamId, int $userId, ?int $accountId =
$accountId = $this->mainDatabase->table($this->getTable())->select("MAX(account_id) + 1 AS nextId")->fetch()->nextId;
}

$this->mainDatabase->table($this->getTable())->insert([
"account_id" => $accountId,
"user_id" => $userId,
"team_id" => $teamId,
]);
try {
$this->mainDatabase->table($this->getTable())->insert([
"account_id" => $accountId,
"user_id" => $userId,
"team_id" => $teamId,
]);
} catch (UniqueConstraintViolationException $exc) {
//this multiaccount already exists - simply return the created id
return $accountId;
}

return $accountId;
}
Expand All @@ -161,7 +168,7 @@ public function delete($resourceId, ?int $subResourceId = null): int
//delete multi account
$targetTeam = $this->teamManager->getBySysname($resourceId);

if (!$targetTeam instanceof \Tymy\Module\Team\Model\Team) {
if (!$targetTeam instanceof Team) {
$this->responder->E4005_OBJECT_NOT_FOUND(Team::MODULE, $resourceId);
}

Expand Down Expand Up @@ -222,7 +229,7 @@ public function generateNewTk(string $targetTeamSysName): TransferKey
{
$targetTeam = $this->teamManager->getBySysname($targetTeamSysName);

if (!$targetTeam instanceof \Tymy\Module\Team\Model\Team) {
if (!$targetTeam instanceof Team) {
$this->respondNotFound();
}

Expand Down Expand Up @@ -256,7 +263,7 @@ private function getTargetUserId(int $accountId, int $teamId): int
->where("team_id", $teamId)
->fetch();

if (!$row instanceof \Nette\Database\Table\ActiveRow) {
if (!$row instanceof ActiveRow) {
$this->responder->E4005_OBJECT_NOT_FOUND(Team::MODULE, $teamId);
}

Expand Down

0 comments on commit 08daa08

Please sign in to comment.