Skip to content

Commit

Permalink
Merge branch 'develop' into seed-groups-with-no-workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
deelanM committed Apr 6, 2021
2 parents 9091167 + 54438dc commit 6410702
Show file tree
Hide file tree
Showing 51 changed files with 2,127 additions and 515 deletions.
15 changes: 14 additions & 1 deletion packages/server/constant/error-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@ module.exports = {
TEMPLATE_NOT_FOUND: 'TEMPLATE_NOT_FOUND',
FORBIDDEN_MAILING_RENAME: 'FORBIDDEN_MAILING_RENAME',
FAILED_MAILING_RENAME: 'FAILED_MAILING_RENAME',
FAILED_FOLDER_RENAME: 'FAILED_FOLDER_RENAME',
FORBIDDEN_MAILING_DELETE: 'FORBIDDEN_MAILING_DELETE',
FAILED_MAILING_DELETE: 'FAILED_MAILING_DELETE',
FORBIDDEN_MAILING_COPY: 'FORBIDDEN_MAILING_COPY',
MAILING_MISSING_SOURCE: 'MAILING_MISSING_SOURCE',
FAILED_MAILING_COPY: 'FAILED_MAILING_COPY',
FORBIDDEN_MAILING_MOVE: 'FORBIDDEN_MAILING_MOVE',
FAILED_MAILING_MOVE: 'FAILED_MAILING_MOVE',
NAME_NOT_PROVIDED: 'NAME_NOT_PROVIDED',
TEMPLATE_NOT_PROVIDED: 'TEMPLATE_NOT_PROVIDED',
PARENT_NOT_PROVIDED: 'PARENT_NOT_PROVIDED',
TWO_PARENTS_PROVIDED: 'TWO_PARENTS_PROVIDED',
FORBIDDEN_FOLDER_CREATION: 'FORBIDDEN_FOLDER_CREATION',
FOLDER_NOT_FOUND: 'FOLDER_NOT_FOUND',
MAILING_NOT_FOUND: 'MAILING_NOT_FOUND',
NAME_ALREADY_TAKEN_AT_SAME_LEVEL: 'NAME_ALREADY_TAKEN_AT_SAME_LEVEL',
PARENT_FOLDER_IS_SUBFOLDER: 'PARENT_FOLDER_IS_SUBFOLDER',
FORBIDDEN_RESOURCE_OR_ACTION: 'FORBIDDEN_RESOURCE_OR_ACTION',
FAILED_FOLDER_DELETE: 'FAILED_FOLDER_DELETE',
FAILED_FOLDER_UPDATE: 'FAILED_FOLDER_UPDATE',
FAILED_FOLDER_MOVE: 'FAILED_FOLDER_MOVE',
FOLDER_HAS_CHILDREN: 'FOLDER_HAS_CHILDREN',
FAILED_WORKSPACE_CREATION: 'FAILED_WORKSPACE_CREATION'
};
162 changes: 159 additions & 3 deletions packages/server/folder/folder.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

const asyncHandler = require('express-async-handler');

const { listFolders } = require('./folder.service');
const folderService = require('./folder.service');
const workspaceService = require('../workspace/workspace.service.js');

module.exports = {
list: asyncHandler(list),
hasAccess: asyncHandler(hasAccess),
create: asyncHandler(create),
getFolder: asyncHandler(getFolder),
deleteFolder: asyncHandler(deleteFolder),
rename: asyncHandler(rename),
move: asyncHandler(move),
};

/**
Expand All @@ -16,10 +24,158 @@ module.exports = {
* @apiUse folder
* @apiSuccess {folders[]} items list of folders
*/

async function list(req, res) {
const folders = await listFolders();
const folders = await folderService.listFolders();
res.json({
items: folders,
});
}
/**
* @api {get} /folders/:folderId/has-access check folder access
* @apiPermission regular_user
* @apiName GetFolderAccess
* @apiGroup Folders
*
* @apiUse folder
* @apiSuccess {hasAccess} boolean indicating access
*/
async function hasAccess(req, res) {
const {
user,
params: { folderId },
} = req;

const hasAccess = await folderService.hasAccess(folderId, user);

res.json({ hasAccess });
}

/**
* @api {post} /folders folder creation
* @apiPermission user
* @apiName CreateFolder
* @apiGroup Folders
*
* @apiParam (Body) {String} name
* @apiParam (Body) {String} workspaceId
* @apiParam (Body) {String} parentFolderId
*
* @apiUse folder
* @apiSuccess {folder} created folder
*/
async function create(req, res) {
const {
user,
body: { name, workspaceId, parentFolderId },
} = req;

const folder = {
name,
workspaceId,
parentFolderId,
};

const createdFolder = await folderService.create(folder, user);

res.send(createdFolder);
}

/**
* @api {get} /folders/:folderId folder
* @apiPermission user
* @apiName GetFolder
* @apiGroup Folders
*
* @apiUse folder
* @apiSuccess {folder} folder
*/
async function getFolder(req, res) {
const {
user,
params: { folderId },
} = req;

const folder = await folderService.getFolder(folderId);

const workspace = await folderService.getWorkspaceForFolder(folderId);
workspaceService.doesUserHaveReadAccess(user, workspace);

res.json(folder);
}

/**
* @api {get} /folders/:folderId folder
* @apiPermission user
* @apiName PatchFolder
* @apiGroup Folders
*
* @apiParam {string} folderId
*
* @apiParam (Body) {String} folderName
*
* @apiUse folder
* @apiSuccess {folder} folder renamed
*/

async function rename(req, res) {
const {
user,
params: { folderId },
body: { folderName },
} = req;

await folderService.rename({ folderId, folderName }, user);

res.status(204).send();
}

/**
* @api {delete} /folders/:folderId folder delete
* @apiPermission regular_user
* @apiName DeleteFolder
* @apiGroup Folders
*
* @apiUse folder
* @apiSuccess {folder} folder deleted
*/

async function deleteFolder(req, res) {
const {
user,
params: { folderId },
} = req;

await folderService.deleteFolder(user, folderId);

res.status(204).send();
}

/**
* @api {post} /folders/:folderId/move move folder from its source to a destination
* @apiPermission regular_user
* @apiName MoveFolder
* @apiGroup Folders
*
* @apiParam {string} folderId
*
* @apiParam (Body) {String} workspaceId
* @apiParam (Body) {String} destinationFolderId
*
* @apiUse folder
*/

async function move(req, res) {
const {
user,
params: { folderId },
body: { workspaceId, destinationFolderId },
} = req;

await folderService.move(
folderId,
{ workspaceId, destinationFolderId },
user
);

res.status(204).send();
}
8 changes: 8 additions & 0 deletions packages/server/folder/folder.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const express = require('express');
const router = express.Router();

const folders = require('./folder.controller.js');
const { GUARD_USER } = require('../account/auth.guard');

router.get('', folders.list);
router.get('/:folderId/has-access', folders.hasAccess);
router.post('/:folderId/move', GUARD_USER, folders.move);
router.post('', folders.create);
router.get('/:folderId', folders.getFolder);
router.delete('/:folderId', folders.deleteFolder);
router.patch('/:folderId', GUARD_USER, folders.rename);

module.exports = router;
17 changes: 12 additions & 5 deletions packages/server/folder/folder.schema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

const { Schema } = require('mongoose');
const { normalizeString } = require('../utils/model');
const { ObjectId } = Schema.Types;
const { FolderModel, WorkspaceModel } = require('../constant/model.names.js');
const {
FolderModel,
WorkspaceModel,
MailingModel,
} = require('../constant/model.names.js');

/**
* @apiDefine folder
Expand All @@ -19,14 +22,11 @@ const FolderSchema = Schema(
{
name: {
type: String,
unique: true,
set: normalizeString,
required: [true, 'Folder name is required'],
},
_workspace: {
type: ObjectId,
ref: WorkspaceModel,
required: [true, 'Workspace is required'],
},
},
{
Expand All @@ -51,4 +51,11 @@ FolderSchema.virtual('childFolders', {
justOne: false,
});

FolderSchema.virtual('mails', {
ref: MailingModel,
localField: '_id',
foreignField: '_parentFolder',
justOne: false,
});

module.exports = FolderSchema;
Loading

0 comments on commit 6410702

Please sign in to comment.