From e314450b6f7359d707735578e5a0b109f279923e Mon Sep 17 00:00:00 2001 From: zak39 Date: Thu, 31 Oct 2024 11:55:43 +0100 Subject: [PATCH 1/4] refactor: Move groupfolder renaming to back-end --- appinfo/routes.php | 3 +-- lib/Controller/WorkspaceController.php | 17 +++++-------- lib/Helper/GroupfolderHelper.php | 8 ++++++ lib/Space/SpaceManager.php | 11 ++++++++ src/SpaceDetails.vue | 7 +++--- src/services/groupfoldersService.js | 1 + src/services/spaceService.js | 35 ++++++++++++++++++++++++++ 7 files changed, 65 insertions(+), 17 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index c2f6b5ea..14faae3e 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -89,8 +89,7 @@ ], [ 'name' => 'workspace#renameSpace', - // TODO move this route to /api/spaces - 'url' => '/api/space/rename', + 'url' => '/spaces/{spaceId}', 'verb' => 'PATCH' ], [ diff --git a/lib/Controller/WorkspaceController.php b/lib/Controller/WorkspaceController.php index dc49da9c..c0fb0b96 100644 --- a/lib/Controller/WorkspaceController.php +++ b/lib/Controller/WorkspaceController.php @@ -67,7 +67,7 @@ public function __construct( private UserGroup $userGroup, private WorkspaceManagerGroup $workspaceManagerGroup, private SpaceManager $spaceManager, - public $AppName, + public $AppName ) { parent::__construct($AppName, $request); } @@ -275,16 +275,12 @@ public function changeUserRole(array|string $space, * * @NoAdminRequired * @SpaceAdminRequired - * @param array|string $workspace + * @param int $spaceId * @param string $newSpaceName * - * @todo Manage errors */ - public function renameSpace(array|string $workspace, + public function renameSpace(int $spaceId, string $newSpaceName): JSONResponse { - if (gettype($workspace) === 'string') { - $workspace = json_decode($workspace, true); - } if ($this->workspaceCheck->containSpecialChar($newSpaceName)) { throw new BadRequestException('Your Workspace name must not contain the following characters: ' . implode(' ', str_split(WorkspaceCheckService::CHARACTERS_SPECIAL))); @@ -299,12 +295,11 @@ public function renameSpace(array|string $workspace, $spaceName = $this->deleteBlankSpaceName($newSpaceName); - $spaceRenamed = $this->spaceService->updateSpaceName($newSpaceName, (int)$workspace['id']); - - // TODO Handle API call failure (revert space rename and inform user) + $this->spaceManager->rename($spaceId, $spaceName); + return new JSONResponse([ 'statuscode' => Http::STATUS_NO_CONTENT, - 'space' => $spaceRenamed, + 'space' => $spaceName, ]); } } diff --git a/lib/Helper/GroupfolderHelper.php b/lib/Helper/GroupfolderHelper.php index d71af1f1..6056cd59 100644 --- a/lib/Helper/GroupfolderHelper.php +++ b/lib/Helper/GroupfolderHelper.php @@ -102,6 +102,14 @@ public function removeFolder(int $folderId): void { $this->folderManager->removeFolder($folderId); } catch (\Exception $e) { throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the removeFolder from FolderManager.'); + } + } + + public function renameFolder(int $folderId, string $newMountPoint):void { + try { + $this->folderManager->renameFolder($folderId, $newMountPoint); + } catch (\Exception $e) { + throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the renameFolder from FolderManager.'); } } } diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index 37a54c20..94cf822c 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -155,5 +155,16 @@ public function remove(string $spaceId): void { $space = $this->get($spaceId); $folderId = $space['groupfolder_id']; $this->folderHelper->removeFolder($folderId); + } + + /** + * @param int $spaceId related to the id of a space. + * @param string $newSpaceName related to the new space name. + */ + public function rename(int $spaceId, string $newSpaceName): void { + $space = $this->get($spaceId); + + $this->folderHelper->renameFolder($space['groupfolder_id'], $newSpaceName); + $this->spaceMapper->updateSpaceName($newSpaceName, $spaceId); } } diff --git a/src/SpaceDetails.vue b/src/SpaceDetails.vue index 1ae5b947..0459041d 100644 --- a/src/SpaceDetails.vue +++ b/src/SpaceDetails.vue @@ -108,8 +108,7 @@ import NcModal from '@nextcloud/vue/dist/Components/NcModal.js' import SelectUsers from './SelectUsers.vue' import RemoveSpace from './RemoveSpace.vue' import UserTable from './UserTable.vue' -import { rename, checkGroupfolderNameExist } from './services/groupfoldersService.js' -import { removeWorkspace } from './services/spaceService.js' +import { renameSpace, removeWorkspace } from './services/spaceService.js' import showNotificationError from './services/Notifications/NotificationError.js' export default { @@ -192,13 +191,13 @@ export default { // TODO: Change : the key from $root.spaces, groupnames, change the route into new spacename because // the path is `https://instance-nc/apps/workspace/workspace/Aang` const oldSpaceName = this.$route.params.space - let responseRename = await rename(this.$store.state.spaces[oldSpaceName], newSpaceName) + let responseRename = await renameSpace(this.$store.state.spaces[oldSpaceName].id, newSpaceName) responseRename = responseRename.data if (responseRename.statuscode === 204) { const space = { ...this.$store.state.spaces[oldSpaceName] } space.name = responseRename.space - space.groups = responseRename.groups + this.$store.dispatch('updateSpace', { space, }) diff --git a/src/services/groupfoldersService.js b/src/services/groupfoldersService.js index ae093b5d..a9813b0d 100644 --- a/src/services/groupfoldersService.js +++ b/src/services/groupfoldersService.js @@ -279,6 +279,7 @@ export function destroy(workspace) { * @param {object} workspace it's the object relative to workspace * @param {string} newSpaceName it's the new name for the workspace * @return {Promise} + * @deprecated */ export function rename(workspace, newSpaceName) { // Response format to return diff --git a/src/services/spaceService.js b/src/services/spaceService.js index 61dd2402..4e9092a9 100644 --- a/src/services/spaceService.js +++ b/src/services/spaceService.js @@ -143,3 +143,38 @@ export function removeWorkspace(spaceId) { }) return result } + +export function renameSpace(spaceId, newSpaceName) { + const respFormat = { + data: {}, + } + respFormat.data.statuscode = 500 + respFormat.data.message = 'Rename the space is impossible.' + + newSpaceName = deleteBlankSpacename(newSpaceName) + + const respFormatFinal = axios.patch(generateUrl(`/apps/workspace/spaces/${spaceId}`), + { + newSpaceName, + }) + .then(resp => { + if (resp.data.statuscode === 400) { + respFormat.data.statuscode = 400 + respFormat.data.space = null + return respFormat + } + + if (resp.data.statuscode === 204) { + respFormat.data.statuscode = 204 + respFormat.data.space = newSpaceName + return respFormat + } + + return respFormat + }) + .catch(error => { + console.error('Problem to rename the space', error) + }) + + return respFormatFinal +} From d4d9d186f42a40631219de87cf58d9e143b15412 Mon Sep 17 00:00:00 2001 From: zak39 Date: Thu, 7 Nov 2024 15:57:33 +0100 Subject: [PATCH 2/4] refactor: Check if the workspace already exists on the backend side --- lib/Space/SpaceManager.php | 7 +++++++ src/SpaceDetails.vue | 2 -- src/services/spaceService.js | 9 ++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index 94cf822c..b4c693be 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -164,6 +164,13 @@ public function remove(string $spaceId): void { public function rename(int $spaceId, string $newSpaceName): void { $space = $this->get($spaceId); + if ($this->workspaceCheck->isExist($newSpaceName)) { + throw new WorkspaceNameExistException( + title: 'Error - Duplicate space name', + message: "This space or groupfolder already exist. Please, input another space.\nIf \"toto\" space exist, you cannot create the \"tOTo\" space.\nMake sure you the groupfolder doesn't exist." + ); + } + $this->folderHelper->renameFolder($space['groupfolder_id'], $newSpaceName); $this->spaceMapper->updateSpaceName($newSpaceName, $spaceId); } diff --git a/src/SpaceDetails.vue b/src/SpaceDetails.vue index 0459041d..effe1961 100644 --- a/src/SpaceDetails.vue +++ b/src/SpaceDetails.vue @@ -186,8 +186,6 @@ export default { const newSpaceName = e.target[0].value - await checkGroupfolderNameExist(newSpaceName) - // TODO: Change : the key from $root.spaces, groupnames, change the route into new spacename because // the path is `https://instance-nc/apps/workspace/workspace/Aang` const oldSpaceName = this.$route.params.space diff --git a/src/services/spaceService.js b/src/services/spaceService.js index 4e9092a9..41132479 100644 --- a/src/services/spaceService.js +++ b/src/services/spaceService.js @@ -173,7 +173,14 @@ export function renameSpace(spaceId, newSpaceName) { return respFormat }) .catch(error => { - console.error('Problem to rename the space', error) + if ('response' in error && 'data' in error.response) { + showNotificationError(error.response.data.title, error.response.data.message, 5000) + throw new Error(error.response.data.message) + } else { + showNotificationError('Error to rename a workspace', error.message, 5000) + console.error('Problem to rename the space', error) + throw new Error(error.message) + } }) return respFormatFinal From 54d7b7b04c1cfb828eb0c818cd493f4cde3a4e0c Mon Sep 17 00:00:00 2001 From: zak39 Date: Thu, 7 Nov 2024 15:59:34 +0100 Subject: [PATCH 3/4] style: composer run cs:fix --- lib/Helper/GroupfolderHelper.php | 4 ++-- lib/Space/SpaceManager.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Helper/GroupfolderHelper.php b/lib/Helper/GroupfolderHelper.php index 6056cd59..2f42cafc 100644 --- a/lib/Helper/GroupfolderHelper.php +++ b/lib/Helper/GroupfolderHelper.php @@ -102,8 +102,8 @@ public function removeFolder(int $folderId): void { $this->folderManager->removeFolder($folderId); } catch (\Exception $e) { throw new GroupFolderFunctionException($e->getMessage() . 'Impossible to use the removeFolder from FolderManager.'); - } - } + } + } public function renameFolder(int $folderId, string $newMountPoint):void { try { diff --git a/lib/Space/SpaceManager.php b/lib/Space/SpaceManager.php index b4c693be..14c67d8d 100644 --- a/lib/Space/SpaceManager.php +++ b/lib/Space/SpaceManager.php @@ -155,7 +155,7 @@ public function remove(string $spaceId): void { $space = $this->get($spaceId); $folderId = $space['groupfolder_id']; $this->folderHelper->removeFolder($folderId); - } + } /** * @param int $spaceId related to the id of a space. @@ -164,7 +164,7 @@ public function remove(string $spaceId): void { public function rename(int $spaceId, string $newSpaceName): void { $space = $this->get($spaceId); - if ($this->workspaceCheck->isExist($newSpaceName)) { + if ($this->workspaceCheck->isExist($newSpaceName)) { throw new WorkspaceNameExistException( title: 'Error - Duplicate space name', message: "This space or groupfolder already exist. Please, input another space.\nIf \"toto\" space exist, you cannot create the \"tOTo\" space.\nMake sure you the groupfolder doesn't exist." From a09b101db53ec4bcf64f3c05c35025a83090122b Mon Sep 17 00:00:00 2001 From: zak39 Date: Thu, 7 Nov 2024 16:04:11 +0100 Subject: [PATCH 4/4] style(js): Indent with tabs --- src/services/spaceService.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/spaceService.js b/src/services/spaceService.js index 41132479..dc8fe7ac 100644 --- a/src/services/spaceService.js +++ b/src/services/spaceService.js @@ -173,13 +173,13 @@ export function renameSpace(spaceId, newSpaceName) { return respFormat }) .catch(error => { - if ('response' in error && 'data' in error.response) { + if ('response' in error && 'data' in error.response) { showNotificationError(error.response.data.title, error.response.data.message, 5000) - throw new Error(error.response.data.message) + throw new Error(error.response.data.message) } else { showNotificationError('Error to rename a workspace', error.message, 5000) - console.error('Problem to rename the space', error) - throw new Error(error.message) + console.error('Problem to rename the space', error) + throw new Error(error.message) } })