Files
planka/server/api/controllers/lists/update.js

202 lines
5.0 KiB
JavaScript
Raw Normal View History

/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
2025-09-08 16:20:27 +02:00
/**
* @swagger
* /api/lists/{id}:
* patch:
* summary: Update list
* description: Updates a list. Can move lists between boards. Requires board editor permissions.
* tags:
* - Lists
* parameters:
* - name: id
* in: path
* required: true
* description: ID of the list to update
* schema:
* type: string
* example: 1357158568008091264
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* boardId:
* type: string
* description: ID of the board to move list to
* example: 1357158568008091265
* type:
* type: string
* enum: [active, closed]
* description: Type/status of the list
* example: active
* position:
* type: number
* minimum: 0
* description: Position of the list within the board
* example: 65536
* name:
* type: string
* maxLength: 128
* description: Name/title of the list
* example: To Do
* color:
* type: string
* enum: [berry-red, pumpkin-orange, lagoon-blue, pink-tulip, light-mud, orange-peel, bright-moss, antique-blue, dark-granite, turquoise-sea]
* nullable: true
* description: Color for the list
* example: lagoon-blue
* responses:
* 200:
* description: List updated successfully
* content:
* application/json:
* schema:
* type: object
* required:
* - item
* properties:
* item:
* $ref: '#/components/schemas/List'
* 400:
* $ref: '#/components/responses/ValidationError'
* 401:
* $ref: '#/components/responses/Unauthorized'
* 403:
* $ref: '#/components/responses/Forbidden'
* 404:
* $ref: '#/components/responses/NotFound'
*/
const { idInput } = require('../../../utils/inputs');
2019-08-31 04:07:25 +05:00
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
2019-08-31 04:07:25 +05:00
LIST_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
listNotFound: 'List not found',
},
BOARD_NOT_FOUND: {
boardNotFound: 'Board not found',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
id: {
...idInput,
required: true,
2019-08-31 04:07:25 +05:00
},
boardId: idInput,
type: {
type: 'string',
isIn: List.FINITE_TYPES,
},
2019-08-31 04:07:25 +05:00
position: {
type: 'number',
min: 0,
2019-08-31 04:07:25 +05:00
},
name: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
},
color: {
type: 'string',
isIn: List.COLORS,
allowNull: true,
},
2019-08-31 04:07:25 +05:00
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-03 00:35:25 +05:00
listNotFound: {
responseType: 'notFound',
},
boardNotFound: {
responseType: 'notFound',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
2019-08-31 04:07:25 +05:00
const { currentUser } = this.req;
const pathToProject = await sails.helpers.lists
.getPathToProjectById(inputs.id)
2020-04-03 00:35:25 +05:00
.intercept('pathNotFound', () => Errors.LIST_NOT_FOUND);
2019-08-31 04:07:25 +05:00
let { list } = pathToProject;
const { board, project } = pathToProject;
let boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
if (!boardMembership) {
2019-08-31 04:07:25 +05:00
throw Errors.LIST_NOT_FOUND; // Forbidden
}
if (!sails.helpers.lists.isFinite(list)) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
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.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
}
const values = _.pick(inputs, ['type', 'position', 'name', 'color']);
2022-12-26 21:10:50 +01:00
list = await sails.helpers.lists.updateOne.with({
project,
board,
2022-12-26 21:10:50 +01:00
record: list,
values: {
...values,
project: nextProject,
board: nextBoard,
},
actorUser: currentUser,
2022-12-26 21:10:50 +01:00
request: this.req,
});
2019-08-31 04:07:25 +05:00
if (!list) {
throw Errors.LIST_NOT_FOUND;
}
return {
item: list,
};
},
2019-08-31 04:07:25 +05:00
};