diff --git a/app/config/errors.php b/app/config/errors.php index 2983d22cd8..e71fb19547 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -227,6 +227,11 @@ return [ 'description' => 'The invite does not belong to the current user.', 'code' => 401, ], + Exception::TEAM_ALREADY_EXISTS => [ + 'name' => Exception::TEAM_ALREADY_EXISTS, + 'description' => 'Team with requested ID already exists.', + 'code' => 409, + ], /** Membership */ Exception::MEMBERSHIP_NOT_FOUND => [ diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 338af70406..cdac9ba2fb 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -67,18 +67,23 @@ App::post('/v1/teams') $isAppUser = Auth::isAppUser(Authorization::getRoles()); $teamId = $teamId == 'unique()' ? ID::unique() : $teamId; - $team = Authorization::skip(fn() => $dbForProject->createDocument('teams', new Document([ - '$id' => $teamId, - '$permissions' => [ - Permission::read(Role::team($teamId)), - Permission::update(Role::team($teamId, 'owner')), - Permission::delete(Role::team($teamId, 'owner')), - ], - 'name' => $name, - 'total' => ($isPrivilegedUser || $isAppUser) ? 0 : 1, - 'prefs' => new \stdClass(), - 'search' => implode(' ', [$teamId, $name]), - ]))); + + try { + $team = Authorization::skip(fn() => $dbForProject->createDocument('teams', new Document([ + '$id' => $teamId, + '$permissions' => [ + Permission::read(Role::team($teamId)), + Permission::update(Role::team($teamId, 'owner')), + Permission::delete(Role::team($teamId, 'owner')), + ], + 'name' => $name, + 'total' => ($isPrivilegedUser || $isAppUser) ? 0 : 1, + 'prefs' => new \stdClass(), + 'search' => implode(' ', [$teamId, $name]), + ]))); + } catch (Duplicate $th) { + throw new Exception(Exception::TEAM_ALREADY_EXISTS); + } if (!$isPrivilegedUser && !$isAppUser) { // Don't add user on server mode if (!\in_array('owner', $roles)) { diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 7ed022b452..42f8876d69 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -83,6 +83,7 @@ class Exception extends \Exception public const TEAM_INVALID_SECRET = 'team_invalid_secret'; public const TEAM_MEMBERSHIP_MISMATCH = 'team_membership_mismatch'; public const TEAM_INVITE_MISMATCH = 'team_invite_mismatch'; + public const TEAM_ALREADY_EXISTS = 'team_already_exists'; /** Membership */ public const MEMBERSHIP_NOT_FOUND = 'membership_not_found'; diff --git a/tests/e2e/Services/Teams/TeamsBase.php b/tests/e2e/Services/Teams/TeamsBase.php index 04b5f93691..8e19bede17 100644 --- a/tests/e2e/Services/Teams/TeamsBase.php +++ b/tests/e2e/Services/Teams/TeamsBase.php @@ -79,6 +79,17 @@ trait TeamsBase $this->assertEquals(400, $response['headers']['status-code']); + $response = $this->client->call(Client::METHOD_POST, '/teams', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'teamId' => $teamId, + 'name' => 'John' + ]); + + $this->assertEquals(409, $response['headers']['status-code']); + $this->assertEquals('team_already_exists', $response['body']['type']); + return ['teamUid' => $teamUid, 'teamName' => $teamName]; }