mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
models/lib/checklistTemplateCopy.js pulls the "copy a checklist/item document onto another card" logic out of Checklists.copy() / copyAllFromCardToCard() (#4017) into plain, Meteor-free functions - the same split already used for subtask copying (models/lib/subtaskCopy.js) and bulk checklist-item text editing (models/lib/checklistItemsAsText.js) - so the copy semantics are unit tested directly: the copy reproduces the source's checklist/item content, drops the source _id (a fresh one is assigned on insert), re-homes onto the destination card/board, places an appended checklist after the target's own (existing checklists are read but never mutated), and forces a copied item unchecked when resetChecked is set while leaving the default (ordinary "Copy Checklist" / card copy) behaviour unchanged. Checklists.copy() and copyAllFromCardToCard() now call these builders instead of repeating the field-by-field copy inline; behaviour is unchanged (the full node suite's existing checklist tests still pass). Thanks to Th0mas89 and xet7 !
69 lines
2.8 KiB
JavaScript
69 lines
2.8 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;
|
|
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;
|
|
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 };
|