From 8b271eb159a318481b7873d8d42c07338c7ee52f Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 4 Sep 2025 14:47:59 +0300 Subject: [PATCH 1/6] Catch error --- app/controllers/api/teams.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index fe411d53ab..8aa041505c 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -36,6 +36,7 @@ use Utopia\Database\Exception\Authorization as AuthorizationException; use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Order as OrderException; use Utopia\Database\Exception\Query as QueryException; +use Utopia\Database\Exception\Structure as StructureException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -357,6 +358,11 @@ App::put('/v1/teams/:teamId/prefs') ->inject('dbForProject') ->inject('queueForEvents') ->action(function (string $teamId, array $prefs, Response $response, Database $dbForProject, Event $queueForEvents) { + try { + $prefs = new Document($prefs); + } catch (StructureException $e) { + throw new Exception(Exception::DOCUMENT_INVALID_STRUCTURE, $e->getMessage()); + } $team = $dbForProject->getDocument('teams', $teamId); @@ -368,7 +374,7 @@ App::put('/v1/teams/:teamId/prefs') $queueForEvents->setParam('teamId', $team->getId()); - $response->dynamic(new Document($prefs), Response::MODEL_PREFERENCES); + $response->dynamic($prefs, Response::MODEL_PREFERENCES); }); App::delete('/v1/teams/:teamId') From 2e26ea3a1baa71b80adc272282e95eaa8dd67f90 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 4 Sep 2025 14:54:22 +0300 Subject: [PATCH 2/6] $prefsDocument --- app/controllers/api/teams.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index ed2e2d2140..ae6ad686e0 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -359,7 +359,7 @@ App::put('/v1/teams/:teamId/prefs') ->inject('queueForEvents') ->action(function (string $teamId, array $prefs, Response $response, Database $dbForProject, Event $queueForEvents) { try { - $prefs = new Document($prefs); + $prefsDocument = new Document($prefs); } catch (StructureException $e) { throw new Exception(Exception::DOCUMENT_INVALID_STRUCTURE, $e->getMessage()); } @@ -374,7 +374,7 @@ App::put('/v1/teams/:teamId/prefs') $queueForEvents->setParam('teamId', $team->getId()); - $response->dynamic($prefs, Response::MODEL_PREFERENCES); + $response->dynamic($prefsDocument, Response::MODEL_PREFERENCES); }); App::delete('/v1/teams/:teamId') From 3fc05c54fb0bf76a18de4ecf16e5f7abe465aec9 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 4 Sep 2025 14:59:59 +0300 Subject: [PATCH 3/6] Fix get prefs --- app/controllers/api/teams.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index ae6ad686e0..7b297fb17b 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -283,7 +283,13 @@ App::get('/v1/teams/:teamId/prefs') $prefs = $team->getAttribute('prefs', []); - $response->dynamic(new Document($prefs), Response::MODEL_PREFERENCES); + try { + $prefs = new Document($prefs); + } catch (StructureException $e) { + throw new Exception(Exception::DOCUMENT_INVALID_STRUCTURE, $e->getMessage()); + } + + $response->dynamic($prefs, Response::MODEL_PREFERENCES); }); App::put('/v1/teams/:teamId') From 8cc5efa2cb6ec26710b8bbd063951857828bb13a Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 4 Sep 2025 15:06:38 +0300 Subject: [PATCH 4/6] Use Document --- app/controllers/api/teams.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index 7b297fb17b..c15d91a3bb 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -365,7 +365,7 @@ App::put('/v1/teams/:teamId/prefs') ->inject('queueForEvents') ->action(function (string $teamId, array $prefs, Response $response, Database $dbForProject, Event $queueForEvents) { try { - $prefsDocument = new Document($prefs); + $prefs = new Document($prefs); } catch (StructureException $e) { throw new Exception(Exception::DOCUMENT_INVALID_STRUCTURE, $e->getMessage()); } @@ -376,11 +376,12 @@ App::put('/v1/teams/:teamId/prefs') throw new Exception(Exception::TEAM_NOT_FOUND); } - $team = $dbForProject->updateDocument('teams', $team->getId(), $team->setAttribute('prefs', $prefs)); + $team->setAttribute('prefs', $prefs->getArrayCopy()); + $team = $dbForProject->updateDocument('teams', $team->getId(), $team); $queueForEvents->setParam('teamId', $team->getId()); - $response->dynamic($prefsDocument, Response::MODEL_PREFERENCES); + $response->dynamic($prefs, Response::MODEL_PREFERENCES); }); App::delete('/v1/teams/:teamId') From ac9181a2e6236329bc99785a4768035491dcdf82 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 4 Sep 2025 15:06:58 +0300 Subject: [PATCH 5/6] Line --- app/controllers/api/teams.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index c15d91a3bb..e7857b0f01 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -377,6 +377,7 @@ App::put('/v1/teams/:teamId/prefs') } $team->setAttribute('prefs', $prefs->getArrayCopy()); + $team = $dbForProject->updateDocument('teams', $team->getId(), $team); $queueForEvents->setParam('teamId', $team->getId()); From 15b56593f4436764f772b5a43427cc77b03e0f8c Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 4 Sep 2025 15:28:59 +0300 Subject: [PATCH 6/6] Partial update --- app/controllers/api/teams.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index e7857b0f01..7398e451b5 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -376,9 +376,9 @@ App::put('/v1/teams/:teamId/prefs') throw new Exception(Exception::TEAM_NOT_FOUND); } - $team->setAttribute('prefs', $prefs->getArrayCopy()); - - $team = $dbForProject->updateDocument('teams', $team->getId(), $team); + $team = $dbForProject->updateDocument('teams', $team->getId(), new Document([ + 'prefs' => $prefs->getArrayCopy() + ])); $queueForEvents->setParam('teamId', $team->getId());