mirror of
https://github.com/plankanban/planka.git
synced 2025-12-24 09:15:01 +03:00
feat: Add ability to move lists between boards (#1208)
This commit is contained in:
@@ -12,6 +12,9 @@ const Errors = {
|
||||
LIST_NOT_FOUND: {
|
||||
listNotFound: 'List not found',
|
||||
},
|
||||
BOARD_NOT_FOUND: {
|
||||
boardNotFound: 'Board not found',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
@@ -20,6 +23,7 @@ module.exports = {
|
||||
...idInput,
|
||||
required: true,
|
||||
},
|
||||
boardId: idInput,
|
||||
type: {
|
||||
type: 'string',
|
||||
isIn: List.FINITE_TYPES,
|
||||
@@ -47,6 +51,9 @@ module.exports = {
|
||||
listNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
boardNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
@@ -59,7 +66,7 @@ module.exports = {
|
||||
let { list } = pathToProject;
|
||||
const { board, project } = pathToProject;
|
||||
|
||||
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
|
||||
let boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
|
||||
board.id,
|
||||
currentUser.id,
|
||||
);
|
||||
@@ -76,13 +83,39 @@ module.exports = {
|
||||
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']);
|
||||
|
||||
list = await sails.helpers.lists.updateOne.with({
|
||||
values,
|
||||
project,
|
||||
board,
|
||||
record: list,
|
||||
values: {
|
||||
...values,
|
||||
project: nextProject,
|
||||
board: nextBoard,
|
||||
},
|
||||
actorUser: currentUser,
|
||||
request: this.req,
|
||||
});
|
||||
|
||||
279
server/api/helpers/cards/detach-custom-fields.js
Normal file
279
server/api/helpers/cards/detach-custom-fields.js
Normal file
@@ -0,0 +1,279 @@
|
||||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
const { POSITION_GAP } = require('../../../constants');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
idOrIds: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
boardId: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
withBaseCustomFieldGroups: {
|
||||
type: 'boolean',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const cardIds = _.isString(inputs.idOrIds) ? [inputs.idOrIds] : inputs.idOrIds;
|
||||
|
||||
const boardCustomFieldGroups = await CustomFieldGroup.qm.getByBoardId(inputs.boardId);
|
||||
const boardCustomFieldGroupIds = sails.helpers.utils.mapRecords(boardCustomFieldGroups);
|
||||
|
||||
const boardCustomFields =
|
||||
await CustomField.qm.getByCustomFieldGroupIds(boardCustomFieldGroupIds);
|
||||
|
||||
const cardsCustomFieldGroups = await CustomFieldGroup.qm.getByCardIds(cardIds);
|
||||
const customFieldGroupsByCardId = _.groupBy(cardsCustomFieldGroups, 'cardId');
|
||||
|
||||
let basedBoardCustomFieldGroups;
|
||||
let basedCardCustomFieldGroups;
|
||||
let baseCustomFieldGroupById;
|
||||
let customFieldsByBaseCustomFieldGroupId;
|
||||
|
||||
if (inputs.withBaseCustomFieldGroups) {
|
||||
basedBoardCustomFieldGroups = boardCustomFieldGroups.filter(
|
||||
({ baseCustomFieldGroupId }) => baseCustomFieldGroupId,
|
||||
);
|
||||
|
||||
basedCardCustomFieldGroups = cardsCustomFieldGroups.filter(
|
||||
({ baseCustomFieldGroupId }) => baseCustomFieldGroupId,
|
||||
);
|
||||
|
||||
const basedCustomFieldGroups = [
|
||||
...basedBoardCustomFieldGroups,
|
||||
...basedCardCustomFieldGroups,
|
||||
];
|
||||
|
||||
const baseCustomFieldGroupIds = sails.helpers.utils.mapRecords(
|
||||
basedCustomFieldGroups,
|
||||
'baseCustomFieldGroupId',
|
||||
true,
|
||||
);
|
||||
|
||||
const baseCustomFieldGroups = await BaseCustomFieldGroup.qm.getByIds(baseCustomFieldGroupIds);
|
||||
baseCustomFieldGroupById = _.keyBy(baseCustomFieldGroups, 'id');
|
||||
|
||||
const baseCustomFields = await CustomField.qm.getByBaseCustomFieldGroupIds(
|
||||
Object.keys(baseCustomFieldGroupById),
|
||||
);
|
||||
|
||||
customFieldsByBaseCustomFieldGroupId = _.groupBy(baseCustomFields, 'baseCustomFieldGroupId');
|
||||
}
|
||||
|
||||
let idsTotal = (boardCustomFieldGroups.length + boardCustomFields.length) * cardIds.length;
|
||||
|
||||
if (inputs.withBaseCustomFieldGroups) {
|
||||
idsTotal += basedBoardCustomFieldGroups.reduce((result, customFieldGroup) => {
|
||||
const customFieldsItem =
|
||||
customFieldsByBaseCustomFieldGroupId[customFieldGroup.baseCustomFieldGroupId];
|
||||
|
||||
return result + (customFieldsItem ? customFieldsItem.length : 0) * cardIds.length;
|
||||
}, 0);
|
||||
|
||||
idsTotal += basedCardCustomFieldGroups.reduce((result, customFieldGroup) => {
|
||||
const customFieldsItem =
|
||||
customFieldsByBaseCustomFieldGroupId[customFieldGroup.baseCustomFieldGroupId];
|
||||
|
||||
return result + (customFieldsItem ? customFieldsItem.length : 0);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
const ids = await sails.helpers.utils.generateIds(idsTotal);
|
||||
|
||||
const nextCustomFieldGroupIdByCustomFieldGroupIdByCardId = {};
|
||||
const nextCustomFieldGroupsValues = (
|
||||
await Promise.all(
|
||||
cardIds.map(async (cardId) => {
|
||||
const customFieldGroupIdByCustomFieldGroupId = {};
|
||||
const customFieldGroupsValues = boardCustomFieldGroups.map((customFieldGroup, index) => {
|
||||
const id = ids.shift();
|
||||
customFieldGroupIdByCustomFieldGroupId[customFieldGroup.id] = id;
|
||||
|
||||
const values = {
|
||||
..._.pick(customFieldGroup, ['baseCustomFieldGroupId', 'name']),
|
||||
id,
|
||||
cardId,
|
||||
position: POSITION_GAP * (index + 1),
|
||||
};
|
||||
|
||||
if (inputs.withBaseCustomFieldGroups && customFieldGroup.baseCustomFieldGroupId) {
|
||||
values.baseCustomFieldGroupId = null;
|
||||
|
||||
if (!customFieldGroup.name) {
|
||||
values.name =
|
||||
baseCustomFieldGroupById[customFieldGroup.baseCustomFieldGroupId].name;
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
});
|
||||
|
||||
nextCustomFieldGroupIdByCustomFieldGroupIdByCardId[cardId] =
|
||||
customFieldGroupIdByCustomFieldGroupId;
|
||||
|
||||
if (customFieldGroupsValues.length > 0) {
|
||||
const cardCustomFieldGroups = customFieldGroupsByCardId[cardId];
|
||||
|
||||
if (cardCustomFieldGroups && cardCustomFieldGroups.length > 0) {
|
||||
const { position } = customFieldGroupsValues[customFieldGroupsValues.length - 1];
|
||||
|
||||
await Promise.all(
|
||||
cardCustomFieldGroups.map((customFieldGroup) =>
|
||||
CustomFieldGroup.qm.updateOne(customFieldGroup.id, {
|
||||
position: customFieldGroup.position + position,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return customFieldGroupsValues;
|
||||
}),
|
||||
)
|
||||
).flat();
|
||||
|
||||
await CustomFieldGroup.qm.create(nextCustomFieldGroupsValues);
|
||||
|
||||
if (inputs.withBaseCustomFieldGroups) {
|
||||
await CustomFieldGroup.qm.update(
|
||||
{
|
||||
cardId: cardIds,
|
||||
baseCustomFieldGroupId: {
|
||||
'!=': null,
|
||||
},
|
||||
},
|
||||
{
|
||||
baseCustomFieldGroupId: null,
|
||||
},
|
||||
);
|
||||
|
||||
const unnamedCustomFieldGroups = basedCardCustomFieldGroups.filter(({ name }) => !name);
|
||||
|
||||
await Promise.all(
|
||||
unnamedCustomFieldGroups.map((customFieldGroup) =>
|
||||
CustomFieldGroup.qm.updateOne(customFieldGroup.id, {
|
||||
name: baseCustomFieldGroupById[customFieldGroup.baseCustomFieldGroupId].name,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const nextCustomFieldIdByCustomFieldIdByCardId = {};
|
||||
const nextCustomFieldsValues = cardIds.flatMap((cardId) => {
|
||||
const customFieldIdByCustomFieldId = {};
|
||||
const customFieldsValues = boardCustomFields.map((customField) => {
|
||||
const id = ids.shift();
|
||||
customFieldIdByCustomFieldId[customField.id] = id;
|
||||
|
||||
return {
|
||||
..._.pick(customField, ['name', 'showOnFrontOfCard', 'position']),
|
||||
id,
|
||||
customFieldGroupId:
|
||||
nextCustomFieldGroupIdByCustomFieldGroupIdByCardId[cardId][
|
||||
customField.customFieldGroupId
|
||||
],
|
||||
};
|
||||
});
|
||||
|
||||
nextCustomFieldIdByCustomFieldIdByCardId[cardId] = customFieldIdByCustomFieldId;
|
||||
return customFieldsValues;
|
||||
});
|
||||
|
||||
if (inputs.withBaseCustomFieldGroups) {
|
||||
cardIds.forEach((cardId) => {
|
||||
basedBoardCustomFieldGroups.forEach((customFieldGroup) => {
|
||||
const customFieldsItem =
|
||||
customFieldsByBaseCustomFieldGroupId[customFieldGroup.baseCustomFieldGroupId];
|
||||
|
||||
if (!customFieldsItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
customFieldsItem.forEach((customField) => {
|
||||
const id = ids.shift();
|
||||
|
||||
nextCustomFieldIdByCustomFieldIdByCardId[cardId][
|
||||
`${customFieldGroup.id}:${customField.id}`
|
||||
] = id;
|
||||
|
||||
nextCustomFieldsValues.push({
|
||||
..._.pick(customField, ['name', 'showOnFrontOfCard', 'position']),
|
||||
id,
|
||||
customFieldGroupId:
|
||||
nextCustomFieldGroupIdByCustomFieldGroupIdByCardId[cardId][customFieldGroup.id],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
basedCardCustomFieldGroups.forEach((customFieldGroup) => {
|
||||
const customFieldsItem =
|
||||
customFieldsByBaseCustomFieldGroupId[customFieldGroup.baseCustomFieldGroupId];
|
||||
|
||||
if (!customFieldsItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
customFieldsItem.forEach((customField) => {
|
||||
const id = ids.shift();
|
||||
|
||||
nextCustomFieldIdByCustomFieldIdByCardId[customFieldGroup.cardId][
|
||||
`${customFieldGroup.id}:${customField.id}`
|
||||
] = id;
|
||||
|
||||
nextCustomFieldsValues.push({
|
||||
..._.pick(customField, ['name', 'showOnFrontOfCard', 'position']),
|
||||
id,
|
||||
customFieldGroupId: customFieldGroup.id,
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
await CustomField.qm.create(nextCustomFieldsValues);
|
||||
|
||||
const customFieldGroupIds = boardCustomFieldGroupIds;
|
||||
if (inputs.withBaseCustomFieldGroups) {
|
||||
customFieldGroupIds.push(...sails.helpers.utils.mapRecords(basedCardCustomFieldGroups));
|
||||
}
|
||||
|
||||
const customFieldValues = await CustomFieldValue.qm.getByCardIds(cardIds, {
|
||||
customFieldGroupIdOrIds: customFieldGroupIds,
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
customFieldValues.map((customFieldValue) => {
|
||||
const updateValues = {
|
||||
customFieldGroupId:
|
||||
nextCustomFieldGroupIdByCustomFieldGroupIdByCardId[customFieldValue.cardId][
|
||||
customFieldValue.customFieldGroupId
|
||||
],
|
||||
};
|
||||
|
||||
const nextCustomFieldIdByCustomFieldId =
|
||||
nextCustomFieldIdByCustomFieldIdByCardId[customFieldValue.cardId];
|
||||
|
||||
if (nextCustomFieldIdByCustomFieldId) {
|
||||
const nextCustomFieldId =
|
||||
nextCustomFieldIdByCustomFieldId[
|
||||
`${customFieldValue.customFieldGroupId}:${customFieldValue.customFieldId}`
|
||||
] || nextCustomFieldIdByCustomFieldId[customFieldValue.customFieldId];
|
||||
|
||||
if (nextCustomFieldId) {
|
||||
updateValues.customFieldId = nextCustomFieldId;
|
||||
}
|
||||
}
|
||||
|
||||
return CustomFieldValue.qm.updateOne(customFieldValue.id, updateValues);
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -3,8 +3,6 @@
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
const { POSITION_GAP } = require('../../../constants');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
record: {
|
||||
@@ -186,194 +184,10 @@ module.exports = {
|
||||
},
|
||||
);
|
||||
|
||||
const boardCustomFieldGroups = await CustomFieldGroup.qm.getByBoardId(inputs.board.id);
|
||||
const boardCustomFieldGroupIds = sails.helpers.utils.mapRecords(boardCustomFieldGroups);
|
||||
|
||||
const boardCustomFields =
|
||||
await CustomField.qm.getByCustomFieldGroupIds(boardCustomFieldGroupIds);
|
||||
|
||||
const cardCustomFieldGroups = await CustomFieldGroup.qm.getByCardId(inputs.record.id);
|
||||
|
||||
let basedCardCustomFieldGroups;
|
||||
let basedCustomFieldGroups;
|
||||
let baseCustomFieldGroupById;
|
||||
let customFieldsByBaseCustomFieldGroupId;
|
||||
|
||||
if (values.project) {
|
||||
const basedBoardCustomFieldGroups = boardCustomFieldGroups.filter(
|
||||
({ baseCustomFieldGroupId }) => baseCustomFieldGroupId,
|
||||
);
|
||||
|
||||
basedCardCustomFieldGroups = cardCustomFieldGroups.filter(
|
||||
({ baseCustomFieldGroupId }) => baseCustomFieldGroupId,
|
||||
);
|
||||
|
||||
basedCustomFieldGroups = [...basedBoardCustomFieldGroups, ...basedCardCustomFieldGroups];
|
||||
|
||||
const baseCustomFieldGroupIds = sails.helpers.utils.mapRecords(
|
||||
basedCustomFieldGroups,
|
||||
'baseCustomFieldGroupId',
|
||||
true,
|
||||
);
|
||||
|
||||
const baseCustomFieldGroups =
|
||||
await BaseCustomFieldGroup.qm.getByIds(baseCustomFieldGroupIds);
|
||||
|
||||
baseCustomFieldGroupById = _.keyBy(baseCustomFieldGroups, 'id');
|
||||
|
||||
const baseCustomFields = await CustomField.qm.getByBaseCustomFieldGroupIds(
|
||||
Object.keys(baseCustomFieldGroupById),
|
||||
);
|
||||
|
||||
customFieldsByBaseCustomFieldGroupId = _.groupBy(
|
||||
baseCustomFields,
|
||||
'baseCustomFieldGroupId',
|
||||
);
|
||||
}
|
||||
|
||||
let idsTotal = boardCustomFieldGroups.length + boardCustomFields.length;
|
||||
|
||||
if (values.project) {
|
||||
idsTotal += basedCustomFieldGroups.reduce((result, customFieldGroup) => {
|
||||
const customFieldsItem =
|
||||
customFieldsByBaseCustomFieldGroupId[customFieldGroup.baseCustomFieldGroupId];
|
||||
|
||||
return result + (customFieldsItem ? customFieldsItem.length : 0);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
const ids = await sails.helpers.utils.generateIds(idsTotal);
|
||||
|
||||
const nextCustomFieldGroupIdByCustomFieldGroupId = {};
|
||||
const nextCustomFieldGroupsValues = boardCustomFieldGroups.map(
|
||||
(customFieldGroup, index) => {
|
||||
const id = ids.shift();
|
||||
nextCustomFieldGroupIdByCustomFieldGroupId[customFieldGroup.id] = id;
|
||||
|
||||
const nextValues = {
|
||||
..._.pick(customFieldGroup, ['baseCustomFieldGroupId', 'name']),
|
||||
id,
|
||||
cardId: inputs.record.id,
|
||||
position: POSITION_GAP * (index + 1),
|
||||
};
|
||||
|
||||
if (values.project && customFieldGroup.baseCustomFieldGroupId) {
|
||||
nextValues.baseCustomFieldGroupId = null;
|
||||
|
||||
if (!customFieldGroup.name) {
|
||||
nextValues.name =
|
||||
baseCustomFieldGroupById[customFieldGroup.baseCustomFieldGroupId].name;
|
||||
}
|
||||
}
|
||||
|
||||
return nextValues;
|
||||
},
|
||||
);
|
||||
|
||||
if (nextCustomFieldGroupsValues.length > 0) {
|
||||
const { position } = nextCustomFieldGroupsValues[nextCustomFieldGroupsValues.length - 1];
|
||||
|
||||
await Promise.all(
|
||||
cardCustomFieldGroups.map((customFieldGroup) =>
|
||||
CustomFieldGroup.qm.updateOne(customFieldGroup.id, {
|
||||
position: customFieldGroup.position + position,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
await CustomFieldGroup.qm.create(nextCustomFieldGroupsValues);
|
||||
|
||||
if (values.project) {
|
||||
await CustomFieldGroup.qm.update(
|
||||
{
|
||||
cardId: inputs.record.id,
|
||||
baseCustomFieldGroupId: {
|
||||
'!=': null,
|
||||
},
|
||||
},
|
||||
{
|
||||
baseCustomFieldGroupId: null,
|
||||
},
|
||||
);
|
||||
|
||||
const unnamedCustomFieldGroups = basedCardCustomFieldGroups.filter(({ name }) => !name);
|
||||
|
||||
await Promise.all(
|
||||
unnamedCustomFieldGroups.map((customFieldGroup) =>
|
||||
CustomFieldGroup.qm.updateOne(customFieldGroup.id, {
|
||||
name: baseCustomFieldGroupById[customFieldGroup.baseCustomFieldGroupId].name,
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
const nextCustomFieldIdByCustomFieldId = {};
|
||||
const nextCustomFieldsValues = boardCustomFields.map((customField) => {
|
||||
const id = ids.shift();
|
||||
nextCustomFieldIdByCustomFieldId[customField.id] = id;
|
||||
|
||||
return {
|
||||
..._.pick(customField, ['name', 'showOnFrontOfCard', 'position']),
|
||||
id,
|
||||
customFieldGroupId:
|
||||
nextCustomFieldGroupIdByCustomFieldGroupId[customField.customFieldGroupId],
|
||||
};
|
||||
});
|
||||
|
||||
if (values.project) {
|
||||
basedCustomFieldGroups.forEach((customFieldGroup) => {
|
||||
const customFieldsItem =
|
||||
customFieldsByBaseCustomFieldGroupId[customFieldGroup.baseCustomFieldGroupId];
|
||||
|
||||
if (!customFieldsItem) {
|
||||
return;
|
||||
}
|
||||
|
||||
customFieldsItem.forEach((customField) => {
|
||||
const id = ids.shift();
|
||||
nextCustomFieldIdByCustomFieldId[`${customFieldGroup.id}:${customField.id}`] = id;
|
||||
|
||||
nextCustomFieldsValues.push({
|
||||
..._.pick(customField, ['name', 'showOnFrontOfCard', 'position']),
|
||||
id,
|
||||
customFieldGroupId:
|
||||
nextCustomFieldGroupIdByCustomFieldGroupId[customFieldGroup.id] ||
|
||||
customFieldGroup.id,
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
await CustomField.qm.create(nextCustomFieldsValues);
|
||||
|
||||
const customFieldGroupIds = boardCustomFieldGroupIds;
|
||||
if (values.project) {
|
||||
customFieldGroupIds.push(...sails.helpers.utils.mapRecords(basedCardCustomFieldGroups));
|
||||
}
|
||||
|
||||
const customFieldValues = await CustomFieldValue.qm.getByCardId(inputs.record.id, {
|
||||
customFieldGroupIdOrIds: customFieldGroupIds,
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
customFieldValues.map((customFieldValue) => {
|
||||
const updateValues = {
|
||||
customFieldGroupId:
|
||||
nextCustomFieldGroupIdByCustomFieldGroupId[customFieldValue.customFieldGroupId],
|
||||
};
|
||||
|
||||
const nextCustomFieldId =
|
||||
nextCustomFieldIdByCustomFieldId[
|
||||
`${customFieldValue.customFieldGroupId}:${customFieldValue.customFieldId}`
|
||||
] || nextCustomFieldIdByCustomFieldId[customFieldValue.customFieldId];
|
||||
|
||||
if (nextCustomFieldId) {
|
||||
updateValues.customFieldId = nextCustomFieldId;
|
||||
}
|
||||
|
||||
return CustomFieldValue.qm.updateOne(customFieldValue.id, updateValues);
|
||||
}),
|
||||
await sails.helpers.cards.detachCustomFields(
|
||||
inputs.record.id,
|
||||
inputs.board.id,
|
||||
!!values.project,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,14 +30,35 @@ module.exports = {
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
boardInValuesMustBelongToProject: {},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const { values } = inputs;
|
||||
|
||||
if (values.project && values.project.id === inputs.project.id) {
|
||||
delete values.project;
|
||||
}
|
||||
|
||||
const project = values.project || inputs.project;
|
||||
|
||||
if (values.board) {
|
||||
if (values.board.projectId !== project.id) {
|
||||
throw 'boardInValuesMustBelongToProject';
|
||||
}
|
||||
|
||||
if (values.board.id === inputs.board.id) {
|
||||
delete values.board;
|
||||
} else {
|
||||
values.boardId = values.board.id;
|
||||
}
|
||||
}
|
||||
|
||||
const board = values.board || inputs.board;
|
||||
|
||||
if (!_.isUndefined(values.position)) {
|
||||
const lists = await sails.helpers.boards.getFiniteListsById(
|
||||
inputs.board.id,
|
||||
inputs.record.id,
|
||||
);
|
||||
const lists = await sails.helpers.boards.getFiniteListsById(board.id, inputs.record.id);
|
||||
|
||||
const { position, repositions } = sails.helpers.utils.insertToPositionables(
|
||||
values.position,
|
||||
@@ -60,7 +81,7 @@ module.exports = {
|
||||
},
|
||||
);
|
||||
|
||||
sails.sockets.broadcast(`board:${inputs.board.id}`, 'listUpdate', {
|
||||
sails.sockets.broadcast(`board:${board.id}`, 'listUpdate', {
|
||||
item: {
|
||||
id: reposition.record.id,
|
||||
position: reposition.position,
|
||||
@@ -72,17 +93,126 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
let cardIdsByLabelId;
|
||||
let prevLabels;
|
||||
|
||||
if (values.board) {
|
||||
const cards = await Card.qm.getByListId(inputs.record.id);
|
||||
const cardIds = sails.helpers.utils.mapRecords(cards);
|
||||
|
||||
const cardLabels = await CardLabel.qm.getByCardIds(cardIds);
|
||||
|
||||
cardIdsByLabelId = cardLabels.reduce(
|
||||
(result, { cardId, labelId }) => ({
|
||||
...result,
|
||||
[labelId]: [...(result[labelId] || []), cardId],
|
||||
}),
|
||||
{},
|
||||
);
|
||||
|
||||
prevLabels = await Label.qm.getByIds(Object.keys(cardIdsByLabelId));
|
||||
|
||||
const boardMemberUserIds = await sails.helpers.boards.getMemberUserIds(values.board.id);
|
||||
|
||||
await CardSubscription.qm.delete({
|
||||
cardId: cardIds,
|
||||
userId: {
|
||||
'!=': boardMemberUserIds,
|
||||
},
|
||||
});
|
||||
|
||||
await CardMembership.qm.delete({
|
||||
cardId: cardIds,
|
||||
userId: {
|
||||
'!=': boardMemberUserIds,
|
||||
},
|
||||
});
|
||||
|
||||
await CardLabel.qm.delete({
|
||||
cardId: cardIds,
|
||||
});
|
||||
|
||||
const taskLists = await TaskList.qm.getByCardIds(cardIds);
|
||||
const taskListIds = sails.helpers.utils.mapRecords(taskLists);
|
||||
|
||||
await Task.qm.update(
|
||||
{
|
||||
taskListId: taskListIds,
|
||||
assigneeUserId: {
|
||||
'!=': boardMemberUserIds,
|
||||
},
|
||||
},
|
||||
{
|
||||
assigneeUserId: null,
|
||||
},
|
||||
);
|
||||
|
||||
await sails.helpers.cards.detachCustomFields(cardIds, inputs.board.id, !!values.project);
|
||||
}
|
||||
|
||||
const { list, tasks } = await List.qm.updateOne(inputs.record.id, values);
|
||||
|
||||
if (list) {
|
||||
sails.sockets.broadcast(
|
||||
`board:${list.boardId}`,
|
||||
'listUpdate',
|
||||
{
|
||||
if (values.board) {
|
||||
if (prevLabels.length > 0) {
|
||||
const labels = await Label.qm.getByBoardId(list.boardId);
|
||||
const labelByName = _.keyBy(labels, 'name');
|
||||
|
||||
const cardLabelsValues = (
|
||||
await Promise.all(
|
||||
prevLabels.map(async (label) => {
|
||||
let labelId;
|
||||
if (labelByName[label.name]) {
|
||||
({ id: labelId } = labelByName[label.name]);
|
||||
} else {
|
||||
({ id: labelId } = await sails.helpers.labels.createOne.with({
|
||||
project,
|
||||
values: {
|
||||
..._.omit(label, ['id', 'boardId', 'createdAt', 'updatedAt']),
|
||||
board,
|
||||
},
|
||||
actorUser: inputs.actorUser,
|
||||
}));
|
||||
}
|
||||
|
||||
return cardIdsByLabelId[label.id].map((cardId) => ({
|
||||
cardId,
|
||||
labelId,
|
||||
}));
|
||||
}),
|
||||
)
|
||||
).flat();
|
||||
|
||||
await CardLabel.qm.create(cardLabelsValues);
|
||||
}
|
||||
|
||||
sails.sockets.broadcast(
|
||||
`board:${inputs.board.id}`,
|
||||
'listUpdate',
|
||||
{
|
||||
item: {
|
||||
id: list.id,
|
||||
boardId: null,
|
||||
},
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
|
||||
sails.sockets.broadcast(`board:${list.boardId}`, 'listUpdate', {
|
||||
item: list,
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
});
|
||||
|
||||
// TODO: add transfer action
|
||||
} else {
|
||||
sails.sockets.broadcast(
|
||||
`board:${list.boardId}`,
|
||||
'listUpdate',
|
||||
{
|
||||
item: list,
|
||||
},
|
||||
inputs.request,
|
||||
);
|
||||
}
|
||||
|
||||
if (tasks) {
|
||||
const taskListIds = sails.helpers.utils.mapRecords(tasks, 'taskListId', true);
|
||||
|
||||
@@ -3,7 +3,13 @@
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
const defaultFind = (criteria) => CustomFieldValue.find(criteria).sort('id');
|
||||
const defaultFind = (criteria, { customFieldGroupIdOrIds }) => {
|
||||
if (customFieldGroupIdOrIds) {
|
||||
criteria.customFieldGroupId = customFieldGroupIdOrIds; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
|
||||
return CustomFieldValue.find(criteria).sort('id');
|
||||
};
|
||||
|
||||
/* Query methods */
|
||||
|
||||
@@ -41,22 +47,21 @@ const createOrUpdateOne = async (values) => {
|
||||
|
||||
const getByIds = (ids) => defaultFind(ids);
|
||||
|
||||
const getByCardId = (cardId, { customFieldGroupIdOrIds } = {}) => {
|
||||
const criteria = {
|
||||
cardId,
|
||||
};
|
||||
const getByCardId = (cardId, { customFieldGroupIdOrIds } = {}) =>
|
||||
defaultFind(
|
||||
{
|
||||
cardId,
|
||||
},
|
||||
{ customFieldGroupIdOrIds },
|
||||
);
|
||||
|
||||
if (customFieldGroupIdOrIds) {
|
||||
criteria.customFieldGroupId = customFieldGroupIdOrIds;
|
||||
}
|
||||
|
||||
return defaultFind(criteria);
|
||||
};
|
||||
|
||||
const getByCardIds = (cardIds) =>
|
||||
defaultFind({
|
||||
cardId: cardIds,
|
||||
});
|
||||
const getByCardIds = (cardIds, { customFieldGroupIdOrIds } = {}) =>
|
||||
defaultFind(
|
||||
{
|
||||
cardId: cardIds,
|
||||
},
|
||||
{ customFieldGroupIdOrIds },
|
||||
);
|
||||
|
||||
const getByCustomFieldGroupId = (customFieldGroupId) =>
|
||||
defaultFind({
|
||||
|
||||
@@ -52,13 +52,13 @@ const getOneTrashByBoardId = (boardId) =>
|
||||
});
|
||||
|
||||
const updateOne = async (criteria, values) => {
|
||||
if (values.type) {
|
||||
if (values.boardId || values.type) {
|
||||
return sails.getDatastore().transaction(async (db) => {
|
||||
const [whereQuery, whereQueryValues] = buildWhereQuery(criteria);
|
||||
|
||||
const queryResult = await sails
|
||||
.sendNativeQuery(
|
||||
`SELECT type FROM list WHERE ${whereQuery} LIMIT 1 FOR UPDATE`,
|
||||
`SELECT board_id, type FROM list WHERE ${whereQuery} LIMIT 1 FOR UPDATE`,
|
||||
whereQueryValues,
|
||||
)
|
||||
.usingConnection(db);
|
||||
@@ -68,6 +68,7 @@ const updateOne = async (criteria, values) => {
|
||||
}
|
||||
|
||||
const prev = {
|
||||
boardId: queryResult.rows[0].board_id,
|
||||
type: queryResult.rows[0].type,
|
||||
};
|
||||
|
||||
@@ -79,6 +80,17 @@ const updateOne = async (criteria, values) => {
|
||||
let tasks = [];
|
||||
|
||||
if (list) {
|
||||
if (list.boardId !== prev.boardId) {
|
||||
await Card.update(
|
||||
{
|
||||
listId: list.id,
|
||||
},
|
||||
{
|
||||
boardId: list.boardId,
|
||||
},
|
||||
).usingConnection(db);
|
||||
}
|
||||
|
||||
const prevTypeState = List.TYPE_STATE_BY_TYPE[prev.type];
|
||||
const typeState = List.TYPE_STATE_BY_TYPE[list.type];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user