1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/helpers/lists/move-to-board.js
symonbaikov 869d9c1d11 feat: fully rework 'move list to board' feature to match review requirements
- All async logic for moving lists between boards is now handled via Redux sagas, not in React components.
- Removed direct API calls and sessionStorage usage from UI.
- Added a unified action creator for moving lists between boards.
- Saga watcher now uses the correct action type constant.
- On the backend, the move-to-board helper now:
  - Detaches card members who are not present on the target board.
  - Converts board-wide custom fields to per-card fields when moving lists.
- Added selector memoization in BoardSelectStep to prevent unnecessary rerenders.
- All business logic is now outside of UI components.
- The feature now fully handles:
  - Users (members/assignees) who do not exist on the target board.
  - Board-wide custom fields, which are now either copied or converted to per-card fields.
- All review comments are addressed: no business logic in components, no sessionStorage, all edge cases handled, only sagas and request used for async actions.
2025-07-04 00:30:07 +03:00

117 lines
3.7 KiB
JavaScript

module.exports = {
inputs: {
list: {
type: 'ref',
required: true,
},
targetBoard: {
type: 'ref',
required: true,
},
actorUser: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs) {
const updatedList = await List.updateOne(
{ id: inputs.list.id },
{ boardId: inputs.targetBoard.id },
);
const updatedCards = await Card.update(
{ listId: inputs.list.id },
{ boardId: inputs.targetBoard.id },
).fetch();
const migrateLabelsPromises = updatedCards.map(async (card) => {
const cardLabels = await CardLabel.find({ cardId: card.id });
return Promise.all(
cardLabels.map(async (cardLabel) => {
const oldLabel = await Label.findOne({ id: cardLabel.labelId });
if (!oldLabel) return;
let newLabel = await Label.findOne({
boardId: inputs.targetBoard.id,
name: oldLabel.name,
color: oldLabel.color,
});
if (!newLabel) {
const maxPosArr = await Label.find({ boardId: inputs.targetBoard.id })
.sort('position DESC')
.limit(1);
const maxPos = maxPosArr.length > 0 ? maxPosArr[0].position : 0;
newLabel = await Label.create({
boardId: inputs.targetBoard.id,
name: oldLabel.name,
color: oldLabel.color,
position: maxPos + 65536,
}).fetch();
}
await CardLabel.destroy({ cardId: card.id, labelId: cardLabel.labelId });
await CardLabel.create({ cardId: card.id, labelId: newLabel.id });
}),
);
});
await Promise.all(migrateLabelsPromises);
await Promise.all(
updatedCards.map(async (card) => {
const cardMemberships = await CardMembership.find({ cardId: card.id });
await Promise.all(
cardMemberships.map(async (membership) => {
const userMembership = await BoardMembership.findOne({
boardId: inputs.targetBoard.id,
userId: membership.userId,
});
if (!userMembership) {
await CardMembership.destroy({ id: membership.id });
}
}),
);
}),
);
await Promise.all(
updatedCards.map(async (card) => {
const customFieldValues = await CustomFieldValue.find({ cardId: card.id });
await Promise.all(
customFieldValues.map(async (value) => {
const group = await CustomFieldGroup.findOne({ id: value.customFieldGroupId });
if (group && group.boardId && group.boardId !== inputs.targetBoard.id) {
const newGroup = await CustomFieldGroup.create({
name: group.name,
position: group.position,
cardId: card.id,
baseCustomFieldGroupId: group.baseCustomFieldGroupId,
}).fetch();
const field = await CustomField.findOne({ id: value.customFieldId });
const newField = await CustomField.create({
name: field.name,
position: field.position,
showOnFrontOfCard: field.showOnFrontOfCard,
customFieldGroupId: newGroup.id,
baseCustomFieldGroupId: field.baseCustomFieldGroupId,
}).fetch();
await CustomFieldValue.updateOne(
{ id: value.id },
{
customFieldGroupId: newGroup.id,
customFieldId: newField.id,
},
);
}
}),
);
}),
);
return {
updatedList,
updatedCards,
};
},
};