Files
wekan/server/lib/labelRemap.js
T
Lauri OjansivuandClaude Opus 4.8 9643dcded9 Fix #5158: copying a swimlane to another board preserves labels
Swimlane copy() remapped card labels by name but never created labels missing on
the destination board, so those assignments were silently dropped. Pre-create
missing destination labels (preserving color) before the per-card copy, so the
by-name remap matches. Pure remapLabelIds helper + tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-20 17:45:43 +03:00

60 lines
2.5 KiB
JavaScript

/**
* Pure, dependency-free helper for remapping board-level labels across boards.
*
* This module intentionally has NO Meteor / collection imports so it can be
* imported directly by unit tests (mocha + chai) without a running app, while
* still being the single source of truth used by models/swimlanes.js when a
* swimlane (and its cards) is copied to ANOTHER board.
*
* #5158 "Copy Swimlane does not copy labels".
*
* Board labels are referenced by id on each card (card.labelIds), but label ids
* are board-scoped. When content moves to another board the assignments must be
* remapped by NAME to the destination board's labels. Source labels whose name
* is missing on the destination board cannot be mapped, so they are reported so
* the caller can create them first.
*/
/**
* Map source label ids to destination label ids by (non-empty) name.
*
* @param {Array<{_id:string,name?:string}>} sourceLabels - labels of the source board
* @param {Array<string>} sourceLabelIds - label ids referenced (e.g. a card's labelIds)
* @param {Array<{_id:string,name?:string}>} destLabels - labels of the destination board
* @returns {{mappedIds: Array<string>, missingNames: Array<string>}}
* mappedIds: destination label ids whose name matches a referenced source label
* missingNames: names of referenced source labels that have no match on dest
*/
export function remapLabelIds(sourceLabels, sourceLabelIds, destLabels) {
const src = Array.isArray(sourceLabels) ? sourceLabels : [];
const ids = Array.isArray(sourceLabelIds) ? sourceLabelIds : [];
const dest = Array.isArray(destLabels) ? destLabels : [];
// Names of the referenced source labels, ignoring unnamed labels: an unnamed
// label can never be matched by name and would otherwise wrongly select every
// unnamed label on the destination board.
const referencedNames = src
.filter(label => label && ids.includes(label._id) && label.name)
.map(label => label.name);
// Build a name -> destination id lookup (first match wins for duplicates).
const destByName = new Map();
for (const label of dest) {
if (label && label.name && !destByName.has(label.name)) {
destByName.set(label.name, label._id);
}
}
const mappedIds = [];
const missingNames = [];
for (const name of referencedNames) {
if (destByName.has(name)) {
mappedIds.push(destByName.get(name));
} else if (!missingNames.includes(name)) {
missingNames.push(name);
}
}
return { mappedIds, missingNames };
}