1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-24 07:39:44 +02:00

feat: Version 2

Closes #627, closes #1047
This commit is contained in:
Maksim Eltyshev 2025-05-10 02:09:06 +02:00
parent ad7fb51cfa
commit 2ee1166747
1557 changed files with 76832 additions and 47042 deletions

View file

@ -1,4 +1,10 @@
const moment = require('moment');
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { isDueDate, isStopwatch } = require('../../../utils/validators');
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
@ -12,59 +18,40 @@ const Errors = {
},
};
const dueDateValidator = (value) => moment(value, moment.ISO_8601, true).isValid();
const stopwatchValidator = (value) => {
if (!_.isPlainObject(value) || _.size(value) !== 2) {
return false;
}
if (
!_.isNull(value.startedAt) &&
_.isString(value.startedAt) &&
!moment(value.startedAt, moment.ISO_8601, true).isValid()
) {
return false;
}
if (!_.isFinite(value.total)) {
return false;
}
return true;
};
module.exports = {
inputs: {
listId: {
...idInput,
required: true,
},
type: {
type: 'string',
regex: /^[0-9]+$/,
isIn: Object.values(Card.Types),
required: true,
},
position: {
type: 'number',
min: 0,
allowNull: true,
},
name: {
type: 'string',
maxLength: 1024,
required: true,
},
description: {
type: 'string',
isNotEmptyString: true,
maxLength: 1048576,
allowNull: true,
},
dueDate: {
type: 'string',
custom: dueDateValidator,
allowNull: true,
},
isDueDateCompleted: {
type: 'boolean',
allowNull: true,
custom: isDueDate,
},
stopwatch: {
type: 'json',
custom: stopwatchValidator,
custom: isStopwatch,
},
},
@ -84,13 +71,13 @@ module.exports = {
const { currentUser } = this.req;
const { list, board, project } = await sails.helpers.lists
.getProjectPath(inputs.listId)
.getPathToProjectById(inputs.listId)
.intercept('pathNotFound', () => Errors.LIST_NOT_FOUND);
const boardMembership = await BoardMembership.findOne({
boardId: board.id,
userId: currentUser.id,
});
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.LIST_NOT_FOUND; // Forbidden
@ -101,20 +88,20 @@ module.exports = {
}
const values = _.pick(inputs, [
'type',
'position',
'name',
'description',
'dueDate',
'isDueDateCompleted',
'stopwatch',
]);
const card = await sails.helpers.cards.createOne
.with({
project,
board,
values: {
...values,
board,
list,
creatorUser: currentUser,
},

View file

@ -1,3 +1,10 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
@ -10,8 +17,7 @@ const Errors = {
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
...idInput,
required: true,
},
},
@ -28,17 +34,17 @@ module.exports = {
async fn(inputs) {
const { currentUser } = this.req;
const path = await sails.helpers.cards
.getProjectPath(inputs.id)
const pathToProject = await sails.helpers.cards
.getPathToProjectById(inputs.id)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
let { card } = path;
const { list, board, project } = path;
let { card } = pathToProject;
const { list, board, project } = pathToProject;
const boardMembership = await BoardMembership.findOne({
boardId: board.id,
userId: currentUser.id,
});
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden

38
server/api/controllers/cards/duplicate.js Executable file → Normal file
View file

@ -1,3 +1,10 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
@ -10,16 +17,18 @@ const Errors = {
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
...idInput,
required: true,
},
position: {
type: 'number',
min: 0,
required: true,
},
name: {
type: 'string',
maxLength: 1024,
required: true,
},
},
@ -36,18 +45,23 @@ module.exports = {
const { currentUser } = this.req;
const { card, list, board, project } = await sails.helpers.cards
.getProjectPath(inputs.id)
.getPathToProjectById(inputs.id)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
const boardMembership = await BoardMembership.findOne({
boardId: board.id,
userId: currentUser.id,
});
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden
}
// TODO: allow for endless lists?
if (!sails.helpers.lists.isFinite(list)) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
@ -58,7 +72,12 @@ module.exports = {
card: nextCard,
cardMemberships,
cardLabels,
taskLists,
tasks,
attachments,
customFieldGroups,
customFields,
customFieldValues,
} = await sails.helpers.cards.duplicateOne.with({
project,
board,
@ -76,7 +95,12 @@ module.exports = {
included: {
cardMemberships,
cardLabels,
taskLists,
tasks,
customFieldGroups,
customFields,
customFieldValues,
attachments: sails.helpers.attachments.presentMany(attachments),
},
};
},

View file

@ -0,0 +1,164 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const moment = require('moment');
const { isId } = require('../../../utils/validators');
const { idInput, idsInput } = require('../../../utils/inputs');
const Errors = {
LIST_NOT_FOUND: {
listNotFound: 'List not found',
},
};
const isBefore = (value) => {
if (!_.isPlainObject(value) || _.size(value) !== 2) {
return false;
}
if (!moment(value.listChangedAt, moment.ISO_8601, true).isValid()) {
return false;
}
if (!isId(value.id)) {
return false;
}
return true;
};
module.exports = {
inputs: {
listId: {
...idInput,
required: true,
},
before: {
type: 'json',
custom: isBefore,
},
search: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
},
filterUserIds: idsInput,
filterLabelIds: idsInput,
},
exits: {
listNotFound: {
responseType: 'notFound',
},
},
async fn(inputs) {
const { currentUser } = this.req;
const { list, project } = await sails.helpers.lists
.getPathToProjectById(inputs.listId)
.intercept('pathNotFound', () => Errors.LIST_NOT_FOUND);
if (currentUser.role !== User.Roles.ADMIN || project.ownerProjectManagerId) {
const isProjectManager = await sails.helpers.users.isProjectManager(
currentUser.id,
project.id,
);
if (!isProjectManager) {
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
list.boardId,
currentUser.id,
);
if (!boardMembership) {
throw Errors.LIST_NOT_FOUND; // Forbidden
}
}
}
let filterUserIds;
if (inputs.filterUserIds) {
const boardMemberships = await BoardMembership.qm.getByBoardId(list.boardId);
const availableUserIdsSet = new Set(
sails.helpers.utils.mapRecords(boardMemberships, 'userId'),
);
filterUserIds = _.uniq(inputs.filterUserIds.split(','));
filterUserIds = filterUserIds.filter((userId) => availableUserIdsSet.has(userId));
}
let filterLabelIds;
if (inputs.filterLabelIds) {
const labels = await Label.qm.getByBoardId(list.boardId);
const availableLabelIdsSet = new Set(sails.helpers.utils.mapRecords(labels));
filterLabelIds = _.uniq(inputs.filterLabelIds.split(','));
filterLabelIds = filterLabelIds.filter((labelId) => availableLabelIdsSet.has(labelId));
}
const cards = await Card.qm.getByEndlessListId(list.id, {
filterUserIds,
filterLabelIds,
before: inputs.before,
search: inputs.search,
});
const cardIds = sails.helpers.utils.mapRecords(cards);
const userIds = sails.helpers.utils.mapRecords(cards, 'creatorUserId', true, true);
const users = await User.qm.getByIds(userIds);
const cardSubscriptions = await CardSubscription.qm.getByCardIdsAndUserId(
cardIds,
currentUser.id,
);
const cardMemberships = await CardMembership.qm.getByCardIds(cardIds);
const cardLabels = await CardLabel.qm.getByCardIds(cardIds);
const taskLists = await TaskList.qm.getByCardIds(cardIds);
const taskListIds = sails.helpers.utils.mapRecords(taskLists);
const tasks = await Task.qm.getByTaskListIds(taskListIds);
const attachments = await Attachment.qm.getByCardIds(cardIds);
const customFieldGroups = await CustomFieldGroup.qm.getByCardIds(cardIds);
const customFieldGroupIds = sails.helpers.utils.mapRecords(customFieldGroups);
const customFields = await CustomField.qm.getByCustomFieldGroupIds(customFieldGroupIds);
const customFieldValues = await CustomFieldValue.qm.getByCardIds(cardIds);
const isSubscribedByCardId = cardSubscriptions.reduce(
(result, cardSubscription) => ({
...result,
[cardSubscription.cardId]: true,
}),
{},
);
cards.forEach((card) => {
// eslint-disable-next-line no-param-reassign
card.isSubscribed = isSubscribedByCardId[card.id] || false;
});
return {
items: cards,
included: {
cardMemberships,
cardLabels,
taskLists,
tasks,
customFieldGroups,
customFields,
customFieldValues,
users: sails.helpers.users.presentMany(users, currentUser),
attachments: sails.helpers.attachments.presentMany(attachments),
},
};
},
};

View file

@ -0,0 +1,66 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { idInput } = require('../../../utils/inputs');
const Errors = {
CARD_NOT_FOUND: {
cardNotFound: 'Card not found',
},
};
module.exports = {
inputs: {
id: {
...idInput,
required: true,
},
},
exits: {
cardNotFound: {
responseType: 'notFound',
},
},
async fn(inputs) {
const { currentUser } = this.req;
const { card, project } = await sails.helpers.cards
.getPathToProjectById(inputs.id)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
if (currentUser.role !== User.Roles.ADMIN || project.ownerProjectManagerId) {
const isProjectManager = await sails.helpers.users.isProjectManager(
currentUser.id,
project.id,
);
if (!isProjectManager) {
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
card.boardId,
currentUser.id,
);
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden
}
}
}
const notifications = await sails.helpers.cards.readNotificationsForUser.with({
record: card,
user: currentUser,
request: this.req,
});
return {
item: card,
included: {
notifications,
},
};
},
};

View file

@ -1,3 +1,10 @@
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { idInput } = require('../../../utils/inputs');
const Errors = {
CARD_NOT_FOUND: {
cardNotFound: 'Card not found',
@ -7,8 +14,7 @@ const Errors = {
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
...idInput,
required: true,
},
},
@ -23,36 +29,57 @@ module.exports = {
const { currentUser } = this.req;
const { card, project } = await sails.helpers.cards
.getProjectPath(inputs.id)
.getPathToProjectById(inputs.id)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, card.boardId);
if (!isBoardMember) {
if (currentUser.role !== User.Roles.ADMIN || project.ownerProjectManagerId) {
const isProjectManager = await sails.helpers.users.isProjectManager(
currentUser.id,
project.id,
);
if (!isProjectManager) {
throw Errors.CARD_NOT_FOUND; // Forbidden
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
card.boardId,
currentUser.id,
);
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden
}
}
}
card.isSubscribed = await sails.helpers.users.isCardSubscriber(currentUser.id, card.id);
const cardMemberships = await sails.helpers.cards.getCardMemberships(card.id);
const cardLabels = await sails.helpers.cards.getCardLabels(card.id);
const tasks = await sails.helpers.cards.getTasks(card.id);
const attachments = await sails.helpers.cards.getAttachments(card.id);
const users = card.creatorUserId ? await User.qm.getByIds([card.creatorUserId]) : [];
const cardMemberships = await CardMembership.qm.getByCardId(card.id);
const cardLabels = await CardLabel.qm.getByCardId(card.id);
const taskLists = await TaskList.qm.getByCardId(card.id);
const taskListIds = sails.helpers.utils.mapRecords(taskLists);
const tasks = await Task.qm.getByTaskListIds(taskListIds);
const attachments = await Attachment.qm.getByCardId(card.id);
const customFieldGroups = await CustomFieldGroup.qm.getByCardId(card.id);
const customFieldGroupIds = sails.helpers.utils.mapRecords(customFieldGroups);
const customFields = await CustomField.qm.getByCustomFieldGroupIds(customFieldGroupIds);
const customFieldValues = await CustomFieldValue.qm.getByCardId(card.id);
return {
item: card,
included: {
cardMemberships,
cardLabels,
taskLists,
tasks,
attachments,
customFieldGroups,
customFields,
customFieldValues,
users: sails.helpers.users.presentMany(users, currentUser),
attachments: sails.helpers.attachments.presentMany(attachments),
},
};
},

View file

@ -1,4 +1,10 @@
const moment = require('moment');
/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { isDueDate, isStopwatch } = require('../../../utils/validators');
const { idInput } = require('../../../utils/inputs');
const Errors = {
NOT_ENOUGH_RIGHTS: {
@ -13,80 +19,60 @@ const Errors = {
LIST_NOT_FOUND: {
listNotFound: 'List not found',
},
COVER_ATTACHMENT_NOT_FOUND: {
coverAttachmentNotFound: 'Cover attachment not found',
},
LIST_MUST_BE_PRESENT: {
listMustBePresent: 'List must be present',
},
COVER_ATTACHMENT_MUST_CONTAIN_IMAGE: {
coverAttachmentMustContainImage: 'Cover attachment must contain image',
},
POSITION_MUST_BE_PRESENT: {
positionMustBePresent: 'Position must be present',
},
};
const dueDateValidator = (value) => moment(value, moment.ISO_8601, true).isValid();
const stopwatchValidator = (value) => {
if (!_.isPlainObject(value) || _.size(value) !== 2) {
return false;
}
if (
!_.isNull(value.startedAt) &&
_.isString(value.startedAt) &&
!moment(value.startedAt, moment.ISO_8601, true).isValid()
) {
return false;
}
if (!_.isFinite(value.total)) {
return false;
}
return true;
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
...idInput,
required: true,
},
boardId: {
type: 'string',
regex: /^[0-9]+$/,
},
listId: {
type: 'string',
regex: /^[0-9]+$/,
},
boardId: idInput,
listId: idInput,
coverAttachmentId: {
type: 'string',
regex: /^[0-9]+$/,
...idInput,
allowNull: true,
},
type: {
type: 'string',
isIn: Object.values(Card.Types),
},
position: {
type: 'number',
min: 0,
allowNull: true,
},
name: {
type: 'string',
isNotEmptyString: true,
maxLength: 1024,
},
description: {
type: 'string',
isNotEmptyString: true,
maxLength: 1048576,
allowNull: true,
},
dueDate: {
type: 'string',
custom: dueDateValidator,
allowNull: true,
},
isDueDateCompleted: {
type: 'boolean',
custom: isDueDate,
allowNull: true,
},
stopwatch: {
type: 'json',
custom: stopwatchValidator,
custom: isStopwatch,
},
isSubscribed: {
type: 'boolean',
@ -106,9 +92,15 @@ module.exports = {
listNotFound: {
responseType: 'notFound',
},
coverAttachmentNotFound: {
responseType: 'notFound',
},
listMustBePresent: {
responseType: 'unprocessableEntity',
},
coverAttachmentMustContainImage: {
responseType: 'unprocessableEntity',
},
positionMustBePresent: {
responseType: 'unprocessableEntity',
},
@ -117,23 +109,38 @@ module.exports = {
async fn(inputs) {
const { currentUser } = this.req;
const path = await sails.helpers.cards
.getProjectPath(inputs.id)
const pathToProject = await sails.helpers.cards
.getPathToProjectById(inputs.id)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
let { card } = path;
const { list, board, project } = path;
let { card } = pathToProject;
const { list, board, project } = pathToProject;
let boardMembership = await BoardMembership.findOne({
boardId: board.id,
userId: currentUser.id,
});
let boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
const availableInputKeys = ['id', 'isSubscribed'];
if (boardMembership.role === BoardMembership.Roles.EDITOR) {
availableInputKeys.push(
'boardId',
'listId',
'coverAttachmentId',
'type',
'position',
'name',
'description',
'dueDate',
'stopwatch',
);
}
if (_.difference(Object.keys(inputs), availableInputKeys).length > 0) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
@ -142,13 +149,13 @@ module.exports = {
if (!_.isUndefined(inputs.boardId)) {
({ board: nextBoard, project: nextProject } = await sails.helpers.boards
.getProjectPath(inputs.boardId)
.getPathToProjectById(inputs.boardId)
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND));
boardMembership = await BoardMembership.findOne({
boardId: nextBoard.id,
userId: currentUser.id,
});
boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
nextBoard.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
@ -161,23 +168,33 @@ module.exports = {
let nextList;
if (!_.isUndefined(inputs.listId)) {
nextList = await List.findOne({
id: inputs.listId,
nextList = await List.qm.getOneById(inputs.listId, {
boardId: (nextBoard || board).id,
});
if (!nextList) {
throw Errors.LIST_NOT_FOUND; // Forbidden
throw Errors.LIST_NOT_FOUND;
}
}
let nextCoverAttachment;
if (inputs.coverAttachmentId) {
nextCoverAttachment = await Attachment.qm.getOneById(inputs.coverAttachmentId, {
cardId: card.id,
});
if (!nextCoverAttachment || nextCoverAttachment.type !== Attachment.Types.FILE) {
throw Errors.COVER_ATTACHMENT_NOT_FOUND;
}
}
const values = _.pick(inputs, [
'coverAttachmentId',
'type',
'position',
'name',
'description',
'dueDate',
'isDueDateCompleted',
'stopwatch',
'isSubscribed',
]);
@ -193,12 +210,17 @@ module.exports = {
project: nextProject,
board: nextBoard,
list: nextList,
coverAttachment: nextCoverAttachment,
},
actorUser: currentUser,
request: this.req,
})
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT)
.intercept('listMustBeInValues', () => Errors.LIST_MUST_BE_PRESENT);
.intercept('listMustBeInValues', () => Errors.LIST_MUST_BE_PRESENT)
.intercept(
'coverAttachmentInValuesMustContainImage',
() => Errors.COVER_ATTACHMENT_MUST_CONTAIN_IMAGE,
);
if (!card) {
throw Errors.CARD_NOT_FOUND;