Files
wekan/models/lib/checklistTemplateCopy.js
T
Lauri Ojansivu 131260b5bc Copy Checklist copies again.
The insert hooks store the author as userId, which neither the checklist nor
the item schema has. A copy carried it from the source and the server refused
it (keyNotInSchema), so the copy dialog's Done did nothing. The copy builders
leave it out.

tests/checklistTemplateCopy reads both schemas and requires every field of a
copy to be one of theirs.

Thanks to xet7 !
2026-11-22 07:21:01 +02:00

74 lines
3.1 KiB
JavaScript

'use strict';
// #4017 "Apply checklist template to an existing card". WeKan already let a
// checklist template be applied only at CARD-CREATION time (copying a
// template card, via Cards.copy() -> Checklists.copy()); there was no way to
// append a template's checklists onto a card that already exists. This
// module holds the pure, Meteor-free document-building logic behind the new
// "Copy Checklist From Template" action
// (client/components/cards/checklists.js /
// Checklists.copyAllFromCardToCard() in models/checklists.js), split out so
// the copy semantics can be unit tested without a database - the same
// pattern already used for subtask copying (models/lib/subtaskCopy.js) and
// bulk checklist-item text editing (models/lib/checklistItemsAsText.js).
//
// Three things the append must get right, each pinned by a test:
// - the copied checklist/item documents carry the SOURCE content, with no
// `_id` (the actual Mongo insert - not done here - assigns the fresh
// one) and re-homed onto the destination card/board;
// - a copied item is always inserted UNCHECKED, regardless of the source
// item's own `isFinished` - applying a template should never pre-check
// the card it lands on;
// - the appended checklists are placed AFTER whatever the destination card
// already has (existing checklists are left untouched, never
// overwritten), in the source card's own order.
/**
* Build the document for a copied checklist, minus `_id` (the caller's
* insert assigns a fresh one).
*/
function buildCopiedChecklistDoc(sourceChecklist, { newCardId, boardId, sort }) {
const copy = Object.assign({}, sourceChecklist);
delete copy._id;
// The insert hook stores the author as userId, which is not in the schema;
// the server refuses a copy that carries it (keyNotInSchema), and the copy
// dialog's Done did nothing (copy-checklist-lazy-destination.e2e.js).
delete copy.userId;
copy.cardId = newCardId;
copy.boardId = boardId;
copy.sort = sort;
return copy;
}
/**
* Build the document for a copied checklist item, minus `_id`. When
* `resetChecked` is true (always the case for a template application), the
* copy is forced unchecked no matter what the source item's `isFinished`
* was - the defining rule of #4017.
*/
function buildCopiedItemDoc(sourceItem, { newChecklistId, newCardId, boardId, resetChecked }) {
const copy = Object.assign({}, sourceItem);
delete copy._id;
delete copy.userId; // not in the schema either; see above
copy.checklistId = newChecklistId;
copy.cardId = newCardId;
copy.boardId = boardId;
if (resetChecked) {
copy.isFinished = false;
}
return copy;
}
/**
* The sort value the first appended checklist should get, so the whole
* batch lands strictly after the destination card's own checklists (which
* are left exactly as they are) rather than colliding with, or sorting
* ahead of, them.
*/
function firstAppendSort(targetChecklists) {
const last = targetChecklists[targetChecklists.length - 1];
return last && typeof last.sort === 'number' ? last.sort + 1 : 0;
}
export { buildCopiedChecklistDoc, buildCopiedItemDoc, firstAppendSort };