1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 05:09:43 +02:00

feat: Add ability to link tasks to cards

This commit is contained in:
Maksim Eltyshev 2025-07-11 01:04:02 +02:00
parent 49203e9d56
commit 230f50e3d9
35 changed files with 761 additions and 243 deletions

View file

@ -12,6 +12,9 @@ const Errors = {
TASK_LIST_NOT_FOUND: {
taskListNotFound: 'Task list not found',
},
LINKED_CARD_NOT_FOUND: {
linkedCardNotFound: 'Linked card not found',
},
};
module.exports = {
@ -20,6 +23,7 @@ module.exports = {
...idInput,
required: true,
},
linkedCardId: idInput,
position: {
type: 'number',
min: 0,
@ -28,7 +32,7 @@ module.exports = {
name: {
type: 'string',
maxLength: 1024,
required: true,
// required: true,
},
isCompleted: {
type: 'boolean',
@ -42,6 +46,9 @@ module.exports = {
taskListNotFound: {
responseType: 'notFound',
},
linkedCardNotFound: {
responseType: 'notFound',
},
},
async fn(inputs) {
@ -51,7 +58,7 @@ module.exports = {
.getPathToProjectById(inputs.taskListId)
.intercept('pathNotFound', () => Errors.TASK_LIST_NOT_FOUND);
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
let boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
@ -64,6 +71,33 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
let linkedCard;
if (!_.isUndefined(inputs.linkedCardId)) {
const path = await sails.helpers.cards
.getPathToProjectById(inputs.linkedCardId)
.intercept('pathNotFound', () => Errors.LINKED_CARD_NOT_FOUND);
({ card: linkedCard } = path);
if (currentUser.role !== User.Roles.ADMIN || path.project.ownerProjectManagerId) {
const isProjectManager = await sails.helpers.users.isProjectManager(
currentUser.id,
path.project.id,
);
if (!isProjectManager) {
boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
linkedCard.boardId,
currentUser.id,
);
if (!boardMembership) {
throw Errors.LINKED_CARD_NOT_FOUND; // Forbidden
}
}
}
}
const values = _.pick(inputs, ['position', 'name', 'isCompleted']);
const task = await sails.helpers.tasks.createOne.with({
@ -74,6 +108,7 @@ module.exports = {
values: {
...values,
taskList,
linkedCard,
},
actorUser: currentUser,
request: this.req,

View file

@ -83,6 +83,14 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
if (task.linkedCardId) {
const availableInputKeys = ['id', 'taskListId', 'position'];
if (_.difference(Object.keys(inputs), availableInputKeys).length > 0) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
}
let nextTaskList;
if (!_.isUndefined(inputs.taskListId)) {
nextTaskList = await TaskList.qm.getOneById(inputs.taskListId, {

View file

@ -39,6 +39,15 @@ module.exports = {
await sails.helpers.taskLists.deleteRelated(taskLists);
await Task.qm.update(
{
linkedCardId: cardIdOrIds,
},
{
linkedCardId: null,
},
);
const { fileReferences } = await Attachment.qm.delete({
cardId: cardIdOrIds,
});

View file

@ -147,7 +147,7 @@ module.exports = {
const nextTaskLists = await TaskList.qm.create(nextTaskListsValues);
const nextTasksValues = tasks.map((task) => ({
..._.pick(task, ['assigneeUserId', 'position', 'name', 'isCompleted']),
..._.pick(task, ['linkedCardId', 'assigneeUserId', 'position', 'name', 'isCompleted']),
taskListId: nextTaskListIdByTaskListId[task.taskListId],
}));
@ -172,9 +172,9 @@ module.exports = {
const nextCoverAttachmentId = nextAttachmentIdByAttachmentId[inputs.record.coverAttachmentId];
if (nextCoverAttachmentId) {
card = await Card.qm.updateOne(card.id, {
({ card } = await Card.qm.updateOne(card.id, {
coverAttachmentId: nextCoverAttachmentId,
});
}));
}
}

View file

@ -397,7 +397,10 @@ module.exports = {
values.listChangedAt = new Date().toISOString();
}
card = await Card.qm.updateOne(inputs.record.id, values);
const updateResult = await Card.qm.updateOne(inputs.record.id, values);
({ card } = updateResult);
const { tasks } = updateResult;
if (!card) {
return card;
@ -491,6 +494,32 @@ module.exports = {
}
}
if (tasks) {
const taskListIds = sails.helpers.utils.mapRecords(tasks, 'taskListId', true);
const taskLists = await TaskList.qm.getByIds(taskListIds);
const taskListById = _.keyBy(taskLists, 'id');
const cardIds = sails.helpers.utils.mapRecords(taskLists, 'cardId', true);
const cards = await Card.qm.getByIds(cardIds);
const cardById = _.keyBy(cards, 'id');
const boardIdByTaskId = tasks.reduce(
(result, task) => ({
...result,
[task.id]: cardById[taskListById[task.taskListId].cardId].boardId,
}),
{},
);
tasks.forEach((task) => {
sails.sockets.broadcast(`board:${boardIdByTaskId[task.id]}`, 'taskUpdate', {
item: task,
});
});
// TODO: send webhooks
}
sails.helpers.utils.sendWebhooks.with({
webhooks,
event: Webhook.Events.CARD_UPDATE,

View file

@ -29,7 +29,7 @@ module.exports = {
async fn(inputs) {
const trashList = await List.qm.getOneTrashByBoardId(inputs.board.id);
const cards = await Card.qm.update(
const { cards } = await Card.qm.update(
{
listId: inputs.record.id,
},

View file

@ -55,7 +55,7 @@ module.exports = {
values.prevListId = null;
}
const cards = await Card.qm.update(
const { cards } = await Card.qm.update(
{
listId: inputs.record.id,
},

View file

@ -78,8 +78,8 @@ module.exports = {
}
cards = await Promise.all(
cards.map((card, index) =>
Card.qm.updateOne(
cards.map(async (card, index) => {
const { card: nextCard } = await Card.qm.updateOne(
{
id: card.id,
listId: card.listId,
@ -87,8 +87,10 @@ module.exports = {
{
position: POSITION_GAP * (index + 1),
},
),
),
);
return nextCard;
}),
);
sails.sockets.broadcast(

View file

@ -33,28 +33,6 @@ module.exports = {
async fn(inputs) {
const { values } = inputs;
if (values.type) {
let isClosed;
if (values.type === List.Types.CLOSED) {
if (inputs.record.type === List.Types.ACTIVE) {
isClosed = true;
}
} else if (inputs.record.type === List.Types.CLOSED) {
isClosed = false;
}
if (!_.isUndefined(isClosed)) {
await Card.qm.update(
{
listId: inputs.record.id,
},
{
isClosed,
},
);
}
}
if (!_.isUndefined(values.position)) {
const lists = await sails.helpers.boards.getFiniteListsById(
inputs.board.id,
@ -94,7 +72,7 @@ module.exports = {
}
}
const list = await List.qm.updateOne(inputs.record.id, values);
const { list, tasks } = await List.qm.updateOne(inputs.record.id, values);
if (list) {
sails.sockets.broadcast(
@ -106,6 +84,32 @@ module.exports = {
inputs.request,
);
if (tasks) {
const taskListIds = sails.helpers.utils.mapRecords(tasks, 'taskListId', true);
const taskLists = await TaskList.qm.getByIds(taskListIds);
const taskListById = _.keyBy(taskLists, 'id');
const cardIds = sails.helpers.utils.mapRecords(taskLists, 'cardId', true);
const cards = await Card.qm.getByIds(cardIds);
const cardById = _.keyBy(cards, 'id');
const boardIdByTaskId = tasks.reduce(
(result, task) => ({
...result,
[task.id]: cardById[taskListById[task.taskListId].cardId].boardId,
}),
{},
);
tasks.forEach((task) => {
sails.sockets.broadcast(`board:${boardIdByTaskId[task.id]}`, 'taskUpdate', {
item: task,
});
});
// TODO: send webhooks
}
const webhooks = await Webhook.qm.getAll();
sails.helpers.utils.sendWebhooks.with({

View file

@ -67,6 +67,17 @@ module.exports = {
// TODO: send webhooks
}
if (values.linkedCard) {
Object.assign(values, {
linkedCardId: values.linkedCard.id,
name: values.linkedCard.name,
});
if (values.linkedCard.isClosed) {
values.isCompleted = true;
}
}
const task = await Task.qm.createOne({
...values,
position,

View file

@ -200,9 +200,69 @@ const getOneById = (id, { listId } = {}) => {
return Card.findOne(criteria);
};
const update = (criteria, values) => Card.update(criteria).set(values).fetch();
const update = async (criteria, values) => {
if (!_.isUndefined(values.isClosed)) {
return sails.getDatastore().transaction(async (db) => {
const cards = await Card.update(criteria).set(values).fetch().usingConnection(db);
const updateOne = (criteria, values) => Card.updateOne(criteria).set({ ...values });
let tasks = [];
if (card) {
tasks = await Task.update({
linkedCardId: sails.helpers.utils.mapRecords(cards),
})
.set({
isCompleted: card.isClosed,
})
.fetch()
.usingConnection(db);
}
return {
cards,
tasks,
};
});
}
const cards = await Card.update(criteria).set(values).fetch();
return {
cards,
};
};
const updateOne = async (criteria, values) => {
if (!_.isUndefined(values.isClosed)) {
return sails.getDatastore().transaction(async (db) => {
const card = await Card.updateOne(criteria)
.set({ ...values })
.usingConnection(db);
let tasks = [];
if (card) {
tasks = await Task.update({
linkedCardId: card.id,
})
.set({
isCompleted: card.isClosed,
})
.fetch()
.usingConnection(db);
}
return {
card,
tasks,
};
});
}
const card = await Card.updateOne(criteria).set({ ...values });
return {
card,
};
};
// eslint-disable-next-line no-underscore-dangle
const delete_ = (criteria) => Card.destroy(criteria).fetch();

View file

@ -47,7 +47,61 @@ const getOneTrashByBoardId = (boardId) =>
type: List.Types.TRASH,
});
const updateOne = (criteria, values) => List.updateOne(criteria).set({ ...values });
const updateOne = async (criteria, values) => {
if (values.type) {
return sails.getDatastore().transaction(async (db) => {
const list = await List.updateOne(criteria)
.set({ ...values })
.usingConnection(db);
let cards = [];
let tasks = [];
if (list) {
let isClosed;
if (list.type === List.Types.ACTIVE) {
isClosed = false;
} else if (list.type === List.Types.CLOSED) {
isClosed = true;
}
if (!_.isUndefined(isClosed)) {
cards = await Card.update({
listId: list.id,
})
.set({
isClosed,
})
.fetch()
.usingConnection(db);
if (cards.length > 0) {
tasks = await Task.update({
linkedCardId: sails.helpers.utils.mapRecords(cards),
})
.set({
isCompleted: isClosed,
})
.fetch()
.usingConnection(db);
}
}
}
return {
list,
cards,
tasks,
};
});
}
const list = await List.updateOne(criteria).set({ ...values });
return {
list,
};
};
// eslint-disable-next-line no-underscore-dangle
const delete_ = (criteria) => List.destroy(criteria).fetch();

View file

@ -36,6 +36,11 @@ const getByTaskListIds = async (taskListIds, { sort = ['position', 'id'] } = {})
{ sort },
);
const getByLinkedCardId = (linkedCardId) =>
defaultFind({
linkedCardId,
});
const getOneById = (id, { taskListId } = {}) => {
const criteria = {
id,
@ -63,6 +68,7 @@ module.exports = {
getByIds,
getByTaskListId,
getByTaskListIds,
getByLinkedCardId,
getOneById,
update,
updateOne,

View file

@ -43,6 +43,10 @@ module.exports = {
required: true,
columnName: 'task_list_id',
},
linkedCardId: {
model: 'Card',
columnName: 'linked_card_id',
},
assigneeUserId: {
model: 'User',
columnName: 'assignee_user_id',