diff --git a/app/module/multiaccount/manager/MultiaccountManager.php b/app/module/multiaccount/manager/MultiaccountManager.php index 7589b164..5f8156ca 100644 --- a/app/module/multiaccount/manager/MultiaccountManager.php +++ b/app/module/multiaccount/manager/MultiaccountManager.php @@ -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; @@ -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(); } @@ -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; } @@ -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); } @@ -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(); } @@ -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); }