Skip to content

Commit

Permalink
Merge pull request #292 from Badsender-com/fix-folder-name-uniqueness
Browse files Browse the repository at this point in the history
Folder move : check if name taken in destination
  • Loading branch information
deelanM committed Apr 8, 2021
2 parents f1cf324 + 8611c48 commit 862eddc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/server/constant/error-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ module.exports = {
FAILED_FOLDER_MOVE: 'FAILED_FOLDER_MOVE',
FOLDER_HAS_CHILDREN: 'FOLDER_HAS_CHILDREN',
FAILED_WORKSPACE_CREATION: 'FAILED_WORKSPACE_CREATION',
FAILED_USERS_UPDATE: 'FAILED_USERS_UPDATE'
FAILED_USERS_UPDATE: 'FAILED_USERS_UPDATE',
FOLDER_MISSING_NAME: 'FOLDER_MISSING_NAME'
};
32 changes: 32 additions & 0 deletions packages/server/folder/folder.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
Conflict,
NotAcceptable,
InternalServerError,
UnprocessableEntity
} = require('http-errors');

const ERROR_CODES = require('../constant/error-codes.js');
Expand Down Expand Up @@ -160,12 +161,31 @@ async function isNameUniqueAtSameLevel(folder) {
}
}

async function isNameTakenInDestination(destination, folderName) {
const { workspaceId, destinationFolderId } = destination;


const destinationFilter = destinationFolderId ?
{ _parentFolder: destinationFolderId } : { _workspace : workspaceId }

return Folders.exists( {
...destinationFilter,
name: folderName
})
}

async function move(folderId, destination, user) {
const { workspaceId, destinationFolderId } = destination;
checkEitherWorkspaceOrFolderDefined(workspaceId, destinationFolderId);

await hasAccess(folderId, user);

const folder = await getFolder(folderId);

if (!folder.name) {
throw new UnprocessableEntity(ERROR_CODES.FOLDER_MISSING_NAME)
}

// moving to another folder
if (destinationFolderId) {
await hasAccess(destinationFolderId, user);
Expand All @@ -176,6 +196,12 @@ async function move(folderId, destination, user) {
// folder being moved can't have children, otherwise it becomes a sub-subfolder
await checkIsParent(folderId);

if (await isNameTakenInDestination({ destinationFolderId }, folder.name)){
throw new Conflict(
`${ERROR_CODES.NAME_ALREADY_TAKEN_AT_SAME_LEVEL} : ${folder.name}`
);
}

const moveToFolder = await Folders.updateOne(
{ _id: mongoose.Types.ObjectId(folderId) },
{
Expand All @@ -196,6 +222,12 @@ async function move(folderId, destination, user) {
const workspace = await workspaceService.getWorkspace(workspaceId);
workspaceService.doesUserHaveWriteAccess(user, workspace);

if (await isNameTakenInDestination({ workspaceId }, folder.name)){
throw new Conflict(
`${ERROR_CODES.NAME_ALREADY_TAKEN_AT_SAME_LEVEL} : ${folder.name}`
);
}

const moveToWorkspace = await Folders.updateOne(
{ _id: mongoose.Types.ObjectId(folderId) },
{
Expand Down
6 changes: 5 additions & 1 deletion packages/ui/routes/mailings/__partials/workspace-tree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ export default {
});
await this.fetchData();
} catch (error) {
let errorKey = 'global.errors.errorOccured';
if (error.response.status === 409) {
errorKey = 'folders.conflict'
}
this.showSnackbar({
text: this.$t('global.errors.errorOccured'),
text: this.$t(errorKey),
color: 'error',
});
}
Expand Down

0 comments on commit 862eddc

Please sign in to comment.