mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Add optional checklist dueAt with the shared selected-calendar picker and badge. Return checklist and item deadlines from checklist and single-card GETs. Accept validated timezone-qualified ISO deadlines in checklist/item POST and PUT; null clears, omission preserves. Keep existing write permissions and ownership checks. Node regression and Chromium save/clear test pass. Thanks to rmb82 and xet7 ! Fixes #6693,
397 lines
14 KiB
JavaScript
397 lines
14 KiB
JavaScript
import { Meteor } from 'meteor/meteor';
|
|
import { Mongo } from 'meteor/mongo';
|
|
import { ReactiveCache, ReactiveMiniMongoIndex } from '/imports/reactiveCache';
|
|
import Activities from '/models/activities';
|
|
import ChecklistItems from '/models/checklistItems';
|
|
import {
|
|
isChecklistShownAtMinicard,
|
|
toggledChecklistAtMinicard,
|
|
} from '/models/lib/minicardChecklistVisibility';
|
|
import {
|
|
buildCopiedChecklistDoc,
|
|
buildCopiedItemDoc,
|
|
firstAppendSort,
|
|
} from '/models/lib/checklistTemplateCopy';
|
|
import { CHECKLIST_RESET_INTERVALS } from '/models/lib/checklistResetSchedule';
|
|
import { selectBulkCheckItemIds } from '/models/lib/checklistBulkCheck';
|
|
const { SimpleSchema } = require('/imports/simpleSchema');
|
|
|
|
const Checklists = new Mongo.Collection('checklists');
|
|
|
|
/**
|
|
* A Checklist
|
|
*/
|
|
Checklists.attachSchema(
|
|
new SimpleSchema({
|
|
cardId: {
|
|
/**
|
|
* The ID of the card the checklist is in
|
|
*/
|
|
type: String,
|
|
},
|
|
boardId: {
|
|
/**
|
|
* The ID of the board the checklist's card is on. Denormalized from the
|
|
* card so the board publication can publish checklists with a single
|
|
* reactive cursor filtered by boardId (a new checklist on a new card then
|
|
* publishes reactively). Kept in sync on insert, on checklist move to
|
|
* another card, and when the card moves to another board.
|
|
*/
|
|
type: String,
|
|
optional: true,
|
|
},
|
|
title: {
|
|
/**
|
|
* the title of the checklist
|
|
*/
|
|
type: String,
|
|
defaultValue: 'Checklist',
|
|
},
|
|
dueAt: {
|
|
/** Date the whole checklist is due; independent of item deadlines. */
|
|
type: Date,
|
|
optional: true,
|
|
},
|
|
finishedAt: {
|
|
/**
|
|
* When was the checklist finished
|
|
*/
|
|
type: Date,
|
|
optional: true,
|
|
},
|
|
createdAt: {
|
|
/**
|
|
* Creation date of the checklist
|
|
*/
|
|
type: Date,
|
|
// eslint-disable-next-line consistent-return
|
|
autoValue() {
|
|
if (this.isInsert) {
|
|
return new Date();
|
|
} else if (this.isUpsert) {
|
|
return { $setOnInsert: new Date() };
|
|
} else {
|
|
this.unset();
|
|
}
|
|
},
|
|
},
|
|
modifiedAt: {
|
|
type: Date,
|
|
// eslint-disable-next-line consistent-return
|
|
autoValue() {
|
|
if (this.isInsert || this.isUpsert || this.isUpdate) {
|
|
return new Date();
|
|
} else {
|
|
this.unset();
|
|
}
|
|
},
|
|
},
|
|
sort: {
|
|
/**
|
|
* sorting value of the checklist
|
|
*/
|
|
type: Number,
|
|
},
|
|
hideCheckedChecklistItems: {
|
|
/**
|
|
* hide the checked checklist-items?
|
|
*/
|
|
type: Boolean,
|
|
optional: true,
|
|
},
|
|
hideAllChecklistItems: {
|
|
/**
|
|
* hide all checklist items ?
|
|
*/
|
|
type: Boolean,
|
|
optional: true,
|
|
},
|
|
showChecklistAtMinicard: {
|
|
/**
|
|
* show this checklist on minicard? Unset = follow the board's
|
|
* allowsChecklistsOnMinicard default; true/false override it.
|
|
*/
|
|
// Reported by email: unchecking "Show on minicard" did nothing. It could not
|
|
// do anything - the minicard ORed this with the board default, which is TRUE
|
|
// out of the box, so the only value that ever mattered was the board's. This
|
|
// is an OVERRIDE now, and an override needs a third state: with
|
|
// `defaultValue: false` every checklist was born "hidden", making "hidden"
|
|
// and "not chosen" the same value. See models/lib/minicardChecklistVisibility.js.
|
|
type: Boolean,
|
|
optional: true,
|
|
},
|
|
resetInterval: {
|
|
/**
|
|
* #3818 / #4729: automatically uncheck all items of this checklist on a
|
|
* recurring schedule. 'none' (default) never resets automatically.
|
|
* See models/lib/checklistResetSchedule.js and
|
|
* server/checklistResetSchedule.js for the due-date calculation and the
|
|
* periodic job that applies it.
|
|
*/
|
|
type: String,
|
|
allowedValues: CHECKLIST_RESET_INTERVALS,
|
|
defaultValue: 'none',
|
|
optional: true,
|
|
},
|
|
lastResetAt: {
|
|
/**
|
|
* When the automatic-reset job last unchecked this checklist's items.
|
|
* Falls back to createdAt (in isChecklistResetDue()) until the first
|
|
* automatic reset happens.
|
|
*/
|
|
type: Date,
|
|
optional: true,
|
|
},
|
|
}),
|
|
);
|
|
|
|
Checklists.helpers({
|
|
getDue() {
|
|
return this.dueAt;
|
|
},
|
|
async setDue(dueAt) {
|
|
return await Checklists.updateAsync(this._id, { $set: { dueAt } });
|
|
},
|
|
async unsetDue() {
|
|
return await Checklists.updateAsync(this._id, { $unset: { dueAt: '' } });
|
|
},
|
|
/**
|
|
* Copy this checklist (and its items) onto another card.
|
|
* @param newCardId the destination card
|
|
* @param options.resetChecked when true, every copied item is inserted
|
|
* unchecked regardless of its source `isFinished` state. Used when
|
|
* applying a template checklist onto an existing card (#4017), where a
|
|
* template's items should never arrive pre-checked. Ordinary
|
|
* copy/duplicate keeps the source checked state, so this defaults to
|
|
* false so existing callers (e.g. the "Copy Checklist" popup, card
|
|
* copy) are unaffected.
|
|
*/
|
|
async copy(newCardId, options = {}) {
|
|
const { resetChecked = false } = options;
|
|
// #5688: copy the checklist and its items with `.direct` so the per-document
|
|
// before/after.insert hooks do NOT fire. The after.insert hooks look up the
|
|
// card and insert an activity per checklist item — for a card with many
|
|
// items that is an N+1 activity-insert storm that made a copy take minutes
|
|
// and spike the CPU. We set boardId ourselves (the before.insert hook would
|
|
// otherwise derive it) from the destination card, which may be on a
|
|
// different board. Same approach as the cardRemover delete path.
|
|
const newCard = await ReactiveCache.getCard(newCardId);
|
|
const boardId = newCard && newCard.boardId;
|
|
|
|
const copyObj = buildCopiedChecklistDoc(this, {
|
|
newCardId,
|
|
boardId,
|
|
sort: this.sort,
|
|
});
|
|
copyObj.createdAt = new Date();
|
|
const newChecklistId = await Checklists.direct.insertAsync(copyObj);
|
|
|
|
const items = await ReactiveCache.getChecklistItems({ checklistId: this._id });
|
|
for (const item of items) {
|
|
const copyItem = buildCopiedItemDoc(item, {
|
|
newChecklistId,
|
|
newCardId,
|
|
boardId,
|
|
resetChecked,
|
|
});
|
|
await ChecklistItems.direct.insertAsync(copyItem);
|
|
}
|
|
|
|
return newChecklistId;
|
|
},
|
|
|
|
itemCount() {
|
|
const ret = this.items().length;
|
|
return ret;
|
|
},
|
|
items() {
|
|
const ret = ReactiveMiniMongoIndex.getChecklistItemsWithChecklistId(this._id, {}, { sort: ['sort'] });
|
|
return ret;
|
|
|
|
},
|
|
firstItem() {
|
|
const ret = this.items()[0];
|
|
return ret;
|
|
},
|
|
lastItem() {
|
|
const ret = this.items().at(-1);
|
|
return ret;
|
|
},
|
|
finishedCount() {
|
|
const ret = this.items().filter(_item => _item.isFinished).length;
|
|
return ret;
|
|
},
|
|
/** returns the finished percent of the checklist */
|
|
finishedPercent() {
|
|
const count = this.itemCount();
|
|
const checklistItemsFinished = this.finishedCount();
|
|
|
|
let ret = 0;
|
|
|
|
if (count > 0) {
|
|
ret = Math.round(checklistItemsFinished / count * 100);
|
|
}
|
|
return ret;
|
|
},
|
|
isFinished() {
|
|
let ret = this.hideAllChecklistItems;
|
|
if (!ret) {
|
|
ret = 0 !== this.itemCount() && this.itemCount() === this.finishedCount();
|
|
}
|
|
return ret;
|
|
},
|
|
/** Per-checklist "hide checked items" state, read from THIS checklist only
|
|
* (issue #5408). Never falls back to a card/global flag, so toggling one
|
|
* checklist never affects the others. Kept in sync with the pure helper
|
|
* checklistHideState() in server/lib/checklistHide.js (unit-tested there). */
|
|
hideCheckedState() {
|
|
return this.hideCheckedChecklistItems === true;
|
|
},
|
|
/** Should the given (checked/unchecked) item be hidden for THIS checklist?
|
|
* An item is hidden only when it is checked and this checklist's toggle is
|
|
* on. Mirrors the pure helper isItemHidden() in server/lib/checklistHide.js. */
|
|
isItemHidden(isChecked) {
|
|
return isChecked === true && this.hideCheckedState() === true;
|
|
},
|
|
showChecklist(hideFinishedChecklistIfItemsAreHidden) {
|
|
let ret = true;
|
|
if (this.isFinished() && hideFinishedChecklistIfItemsAreHidden === true && (this.hideCheckedState() === true || this.hideAllChecklistItems)) {
|
|
ret = false;
|
|
}
|
|
return ret;
|
|
},
|
|
// #2473: "Check all items" / "Uncheck all items" on the checklist's action
|
|
// menu. selectBulkCheckItemIds() (models/lib/checklistBulkCheck.js, unit
|
|
// tested there) is what decides WHICH items belong to this checklist - kept
|
|
// pure/testable so "every item of this checklist, regardless of starting
|
|
// state" and "items of any other checklist are untouched" can be asserted
|
|
// without a database.
|
|
async checkAllItems() {
|
|
const checkItems = await ReactiveCache.getChecklistItems({ checklistId: this._id });
|
|
const ids = selectBulkCheckItemIds(checkItems, this._id);
|
|
for (const item of checkItems) {
|
|
if (ids.includes(item._id)) {
|
|
await item.check();
|
|
}
|
|
}
|
|
},
|
|
async uncheckAllItems() {
|
|
const checkItems = await ReactiveCache.getChecklistItems({ checklistId: this._id });
|
|
const ids = selectBulkCheckItemIds(checkItems, this._id);
|
|
for (const item of checkItems) {
|
|
if (ids.includes(item._id)) {
|
|
await item.uncheck();
|
|
}
|
|
}
|
|
},
|
|
itemIndex(itemId) {
|
|
const items = ReactiveCache.getChecklist({ _id: this._id }).items;
|
|
return items.map(item => item._id).indexOf(itemId);
|
|
},
|
|
|
|
async setTitle(title) {
|
|
return await Checklists.updateAsync(this._id, { $set: { title } });
|
|
},
|
|
/** move the checklist to another card
|
|
* @param newCardId move the checklist to this cardId
|
|
*/
|
|
async move(newCardId) {
|
|
// Note: Activities and ChecklistItems updates are now handled server-side
|
|
// in the moveChecklist Meteor method to avoid client-side permission issues
|
|
return await Checklists.updateAsync(this._id, { $set: { cardId: newCardId } });
|
|
},
|
|
async toggleHideCheckedChecklistItems() {
|
|
return await Checklists.updateAsync(this._id, {
|
|
$set: { hideCheckedChecklistItems: !this.hideCheckedChecklistItems },
|
|
});
|
|
},
|
|
async toggleHideAllChecklistItems() {
|
|
return await Checklists.updateAsync(this._id, {
|
|
$set: { hideAllChecklistItems: !this.hideAllChecklistItems },
|
|
});
|
|
},
|
|
// Is this checklist on the minicard right now? The board's
|
|
// allowsChecklistsOnMinicard is the default and this checklist's own field, when
|
|
// it has one, overrides it (models/lib/minicardChecklistVisibility.js). The
|
|
// caller passes the board's value: this runs in client templates, where a
|
|
// synchronous answer is needed.
|
|
isShownAtMinicard(boardAllowsChecklistsOnMinicard) {
|
|
return isChecklistShownAtMinicard(this, boardAllowsChecklistsOnMinicard);
|
|
},
|
|
async toggleShowChecklistAtMinicard(boardAllowsChecklistsOnMinicard) {
|
|
return await Checklists.updateAsync(this._id, {
|
|
$set: {
|
|
showChecklistAtMinicard:
|
|
toggledChecklistAtMinicard(this, boardAllowsChecklistsOnMinicard),
|
|
},
|
|
});
|
|
},
|
|
/** #3818 / #4729: set (or clear, with 'none') this checklist's automatic-reset
|
|
* interval. Does not itself uncheck anything - the periodic job in
|
|
* server/checklistResetSchedule.js does that once the interval comes due. */
|
|
async setResetInterval(resetInterval) {
|
|
if (!CHECKLIST_RESET_INTERVALS.includes(resetInterval)) return undefined;
|
|
return await Checklists.updateAsync(this._id, {
|
|
$set: { resetInterval },
|
|
});
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Append every checklist (and its items) of a source card onto a destination
|
|
* card, in source order, leaving the destination card's existing checklists
|
|
* untouched (#4017: apply a template card's checklists onto an already
|
|
* existing card). Copied items always come in unchecked, regardless of the
|
|
* source card's own checked state — a template being applied should never
|
|
* pre-check its target. Reuses the same per-checklist `copy()` helper the
|
|
* "Copy Checklist" popup and card-copy (`Cards.copy`) already use, so the
|
|
* copy semantics (fresh ids, `.direct` inserts to skip the per-item activity
|
|
* storm, board re-homing) stay in one place.
|
|
* @param sourceCardId the template/source card whose checklists are copied
|
|
* @param targetCardId the existing card the checklists are appended onto
|
|
* @returns the array of newly created checklist ids, in source order
|
|
*/
|
|
Checklists.copyAllFromCardToCard = async function (sourceCardId, targetCardId) {
|
|
const sourceChecklists = await ReactiveCache.getChecklists(
|
|
{ cardId: sourceCardId },
|
|
{ sort: { sort: 1 } },
|
|
);
|
|
|
|
// Place the appended checklists after whatever the target card already has,
|
|
// in source order, rather than reusing the source card's own sort values
|
|
// (which could collide with, or sort ahead of, the target's existing
|
|
// checklists).
|
|
const targetChecklists = await ReactiveCache.getChecklists(
|
|
{ cardId: targetCardId },
|
|
{ sort: { sort: 1 } },
|
|
);
|
|
let nextSort = firstAppendSort(targetChecklists);
|
|
|
|
const newChecklistIds = [];
|
|
for (const checklist of sourceChecklists) {
|
|
const newChecklistId = await checklist.copy(targetCardId, { resetChecked: true });
|
|
await Checklists.direct.updateAsync(newChecklistId, { $set: { sort: nextSort } });
|
|
nextSort += 1;
|
|
newChecklistIds.push(newChecklistId);
|
|
}
|
|
return newChecklistIds;
|
|
};
|
|
|
|
Checklists.before.insert((userId, doc) => {
|
|
doc.createdAt = new Date();
|
|
if (!doc.userId) {
|
|
doc.userId = userId;
|
|
}
|
|
});
|
|
if (Meteor.isServer) {
|
|
// A card's checklists in their own order, and the board-wide queries the
|
|
// export and the minicard badges make.
|
|
const { ensureIndex } = require('/server/lib/mongoStartup');
|
|
Meteor.startup(async () => {
|
|
await ensureIndex(Checklists, { cardId: 1, sort: 1 });
|
|
await ensureIndex(Checklists, { boardId: 1 });
|
|
});
|
|
}
|
|
|
|
export default Checklists;
|