mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Labels belong to a board, so a card moved or copied to another board has its labels mapped by name, and a label the destination board did not have was dropped. Now a missing named label is created on the destination board, with its colour, when the person moving or copying the card is an admin of that board. Creating labels is admin-only there, and a move must not do what that user could not do directly, so for anybody else such a label is still dropped. Unnamed labels are never created or matched, as they cannot be told apart. Copy and move share the rule in models/lib/crossBoardLabels.js. tests/crossBoardLabels.test.cjs covers admins, members, unnamed and duplicate labels; the cross-board move Playwright case checks a label that only the source board had. Thanks to d3dbit and xet7 ! Fixes #1759.
37 lines
1.6 KiB
JavaScript
37 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
// #1759: a card's labels when it is moved or copied to another board.
|
|
//
|
|
// Labels belong to a board, so a card's label ids are remapped by name to the
|
|
// destination board's labels. A label the destination board does not have was
|
|
// dropped. It is now created there - same name, same colour - when the person
|
|
// moving or copying the card may create labels on that board, which is a board
|
|
// admin (server/permissions/boards.js); for anybody else the label is still
|
|
// dropped, because a move must not do what that user could not do directly.
|
|
// Unnamed labels are never created or matched: they cannot be told apart.
|
|
//
|
|
// Pure: tested by tests/crossBoardLabels.test.cjs.
|
|
function planCrossBoardLabels({ sourceLabels, labelIds, destLabels, canCreate, newId }) {
|
|
const src = Array.isArray(sourceLabels) ? sourceLabels : [];
|
|
const ids = Array.isArray(labelIds) ? labelIds : [];
|
|
const dest = Array.isArray(destLabels) ? destLabels : [];
|
|
const byName = new Map();
|
|
for (const label of dest) if (label && label.name && !byName.has(label.name)) byName.set(label.name, label._id);
|
|
const create = [];
|
|
const mapped = [];
|
|
for (const label of src) {
|
|
if (!label || !label.name || !ids.includes(label._id)) continue;
|
|
if (byName.has(label.name)) {
|
|
if (!mapped.includes(byName.get(label.name))) mapped.push(byName.get(label.name));
|
|
} else if (canCreate) {
|
|
const created = { _id: newId(), name: label.name, color: label.color };
|
|
byName.set(label.name, created._id);
|
|
create.push(created);
|
|
mapped.push(created._id);
|
|
}
|
|
}
|
|
return { labelIds: mapped, create };
|
|
}
|
|
|
|
module.exports = { planCrossBoardLabels };
|