feat: Add ability to copy/cut cards with shortcut support

This commit is contained in:
Maksim Eltyshev
2025-12-09 14:58:01 +01:00
parent 52acc9de90
commit 9e6e38fcf7
113 changed files with 1616 additions and 259 deletions

View File

@@ -26,18 +26,25 @@
* application/json:
* schema:
* type: object
* required:
* - position
* - name
* properties:
* boardId:
* type: string
* description: ID of the board to duplicate the card to
* example: "1357158568008091265"
* listId:
* type: string
* description: ID of the list to duplicate the card to
* example: "1357158568008091266"
* position:
* type: number
* minimum: 0
* nullable: true
* description: Position for the duplicated card within the list
* example: 65536
* name:
* type: string
* maxLength: 1024
* nullable: true
* description: Name/title for the duplicated card
* example: Implement user authentication (copy)
* responses:
@@ -113,6 +120,8 @@
* $ref: '#/components/responses/Forbidden'
* 404:
* $ref: '#/components/responses/NotFound'
* 422:
* $ref: '#/components/responses/UnprocessableEntity'
*/
const { idInput } = require('../../../utils/inputs');
@@ -124,6 +133,18 @@ const Errors = {
CARD_NOT_FOUND: {
cardNotFound: 'Card not found',
},
BOARD_NOT_FOUND: {
boardNotFound: 'Board not found',
},
LIST_NOT_FOUND: {
listNotFound: 'List not found',
},
LIST_MUST_BE_PRESENT: {
listMustBePresent: 'List must be present',
},
POSITION_MUST_BE_PRESENT: {
positionMustBePresent: 'Position must be present',
},
};
module.exports = {
@@ -132,15 +153,17 @@ module.exports = {
...idInput,
required: true,
},
boardId: idInput,
listId: idInput,
position: {
type: 'number',
min: 0,
required: true,
allowNull: true,
},
name: {
type: 'string',
maxLength: 1024,
required: true,
allowNull: true,
},
},
@@ -151,6 +174,18 @@ module.exports = {
cardNotFound: {
responseType: 'notFound',
},
boardNotFound: {
responseType: 'notFound',
},
listNotFound: {
responseType: 'notFound',
},
listMustBePresent: {
responseType: 'unprocessableEntity',
},
positionMustBePresent: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs) {
@@ -160,24 +195,60 @@ module.exports = {
.getPathToProjectById(inputs.id)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
const isProjectManager = await sails.helpers.users.isProjectManager(currentUser.id, project.id);
let boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden
if (!isProjectManager) {
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
}
// TODO: allow for endless lists?
if (!sails.helpers.lists.isFinite(list)) {
throw Errors.NOT_ENOUGH_RIGHTS;
let nextProject;
let nextBoard;
if (!_.isUndefined(inputs.boardId)) {
({ board: nextBoard, project: nextProject } = await sails.helpers.boards
.getPathToProjectById(inputs.boardId)
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND));
boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
nextBoard.id,
currentUser.id,
);
if (!boardMembership) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
}
}
if (!boardMembership) {
throw Errors.LIST_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
let nextList;
if (!_.isUndefined(inputs.listId)) {
nextList = await List.qm.getOneById(inputs.listId, {
boardId: (nextBoard || board).id,
});
if (!nextList) {
throw Errors.LIST_NOT_FOUND;
}
}
const values = _.pick(inputs, ['position', 'name']);
const {
@@ -190,17 +261,23 @@ module.exports = {
customFieldGroups,
customFields,
customFieldValues,
} = await sails.helpers.cards.duplicateOne.with({
project,
board,
list,
record: card,
values: {
...values,
creatorUser: currentUser,
},
request: this.req,
});
} = await sails.helpers.cards.duplicateOne
.with({
project,
board,
list,
record: card,
values: {
...values,
project: nextProject,
board: nextBoard,
list: nextList,
creatorUser: currentUser,
},
request: this.req,
})
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT)
.intercept('listMustBeInValues', () => Errors.LIST_MUST_BE_PRESENT);
return {
item: nextCard,