1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/controllers/cards/update.js
Mikhail Khromov bcc92dfa76 Add GitHub link field to tasks
Add a custom field named "Github Link" to tasks and display it in the frontend.

* Add `githubLink` attribute to the `Task` model in `client/src/models/Task.js`.
* Update `createTask` and `updateTask` actions in `client/src/actions/tasks.js` to handle the `githubLink` attribute.
* Add input field for `githubLink` in `CardModal` component in `client/src/components/CardModal/CardModal.jsx`.
  * Display `githubLink` as a clickable link when filled in.
  * Display a button that expands into an input field when `githubLink` is not filled in.
* Include `githubLink` attribute in card data retrieval logic in `server/api/controllers/cards/show.js`.
* Include `githubLink` attribute in card data updating logic in `server/api/controllers/cards/update.js`.
* Add validation function for `githubLink` in `client/src/utils/validator.js`.
* Add unit tests for `githubLink` field in `client/src/components/CardModal/CardModal.test.js`.
  * Test display of `githubLink` as a clickable link when filled in.
  * Test display of button to add `githubLink` when not filled in.
  * Test input field display when add `githubLink` button is clicked.
  * Test `onUpdate` call with `githubLink` when save button is clicked.
  * Test validation of `githubLink` format.
2025-02-26 21:56:32 +03:00

227 lines
5 KiB
JavaScript

const moment = require('moment');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
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',
},
};
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;
};
const githubLinkValidator = (value) => {
const urlPattern = new RegExp('^(https?:\\/\\/)?'+ // validate the protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|'+ // validate the domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR validate the IP (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // validate the port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // validate the query string
'(\\#[-a-z\\d_]*)?$','i'); // validate the fragment locator
return !!urlPattern.test(value) && value.startsWith('https://github.com/');
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
boardId: {
type: 'string',
regex: /^[0-9]+$/,
},
listId: {
type: 'string',
regex: /^[0-9]+$/,
},
coverAttachmentId: {
type: 'string',
regex: /^[0-9]+$/,
allowNull: true,
},
position: {
type: 'number',
},
name: {
type: 'string',
isNotEmptyString: true,
},
description: {
type: 'string',
isNotEmptyString: true,
allowNull: true,
},
dueDate: {
type: 'string',
custom: dueDateValidator,
allowNull: true,
},
isDueDateCompleted: {
type: 'boolean',
allowNull: true,
},
stopwatch: {
type: 'json',
custom: stopwatchValidator,
},
isSubscribed: {
type: 'boolean',
},
githubLink: {
type: 'string',
custom: githubLinkValidator,
allowNull: true,
},
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
cardNotFound: {
responseType: 'notFound',
},
boardNotFound: {
responseType: 'notFound',
},
listNotFound: {
responseType: 'notFound',
},
listMustBePresent: {
responseType: 'unprocessableEntity',
},
positionMustBePresent: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs) {
const { currentUser } = this.req;
const path = await sails.helpers.cards
.getProjectPath(inputs.id)
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
let { card } = path;
const { list, board, project } = path;
let boardMembership = await BoardMembership.findOne({
boardId: board.id,
userId: currentUser.id,
});
if (!boardMembership) {
throw Errors.CARD_NOT_FOUND; // Forbidden
}
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
.getProjectPath(inputs.boardId)
.intercept('pathNotFound', () => Errors.BOARD_NOT_FOUND));
boardMembership = await BoardMembership.findOne({
boardId: nextBoard.id,
userId: currentUser.id,
});
if (!boardMembership) {
throw Errors.BOARD_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
}
let nextList;
if (!_.isUndefined(inputs.listId)) {
nextList = await List.findOne({
id: inputs.listId,
boardId: (nextBoard || board).id,
});
if (!nextList) {
throw Errors.LIST_NOT_FOUND; // Forbidden
}
}
const values = _.pick(inputs, [
'coverAttachmentId',
'position',
'name',
'description',
'dueDate',
'isDueDateCompleted',
'stopwatch',
'isSubscribed',
'githubLink',
]);
card = await sails.helpers.cards.updateOne
.with({
project,
board,
list,
record: card,
values: {
...values,
project: nextProject,
board: nextBoard,
list: nextList,
},
actorUser: currentUser,
request: this.req,
})
.intercept('positionMustBeInValues', () => Errors.POSITION_MUST_BE_PRESENT)
.intercept('listMustBeInValues', () => Errors.LIST_MUST_BE_PRESENT);
if (!card) {
throw Errors.CARD_NOT_FOUND;
}
return {
item: card,
};
},
};