Files
wekan/models/lib/brokenCardsRepair.js
T
Lauri Ojansivu d168a9d302 Add a Repair button for "Broken cards N" in Admin Panel / Problems / Summary.
The Summary page reported "Broken cards N" and told the admin to "open the board
to auto-repair, or run the repair migration", but there was no button anywhere to
run one - and worse, no repair in the app could actually clear that count.

countBrokenCards() counts every card missing a boardId OR a swimlaneId OR a
listId. repairAllBoards() walks only NON-ARCHIVED boards and only ever sets
swimlaneId, so three kinds of counted card could never be repaired by anything:
cards on an archived board, cards with a missing listId, and cards with no
boardId at all. The count sat there forever with nothing to press.

The new repairBrokenCards method (admin only) runs the standard per-board pass
and then closes those gaps: orphaned-swimlane cards on archived boards, and cards
missing a listId and/or swimlaneId on any board, are assigned to that board's
first usable list/swimlane (creating a default one if the board has none). Each
board resolves its default list/swimlane once rather than once per card. Cards
with no boardId cannot be placed on any board - guessing would drop a user's card
onto an unrelated board - so they are reported back as `unfixable` and left
alone, never deleted, and the result line says so instead of leaving a count that
never reaches zero unexplained.

What counts as broken now lives in one pure module, models/lib/brokenCardsRepair.js,
used by BOTH the count and the repair, so they can no longer drift apart and
leave the admin pressing a button that does not move the number. Progress is
persisted through setBoardRepairStatus, so a running repair shows up in the same
"in progress" list as the startup pass.

Adds unit tests (positive + negative) for the shared rule, including the
regression itself: every card the count reports as broken must be either
repairable or explicitly unfixable, never silently neither.

Thanks to xet7 !
2026-07-24 03:23:37 +03:00

100 lines
3.2 KiB
JavaScript

'use strict';
// Single source of truth for what "a broken card" means, plus the pure planner
// for repairing one.
//
// The Admin Panel → Problems → Summary page COUNTS broken cards and now also
// REPAIRS them from the same page. Those two had the rule written out twice —
// once in server/lib/systemStatus.js (countBrokenCards) and once in the repair —
// so they could drift and leave the admin pressing a Repair button that never
// makes the count go down. Both now use the selector and the planner here.
//
// A card is broken when it has no boardId, no swimlaneId or no listId: any of
// those makes it unreachable in the board views. Only two of the three can be
// repaired automatically:
//
// * missing listId / swimlaneId -> the card still knows its board, so it can
// be put on that board's first usable list / swimlane.
// * missing boardId -> the card belongs to no board at all. There
// is no way to know which board it came from, and guessing would drop a
// user's card onto an unrelated board, so these are reported as unfixable
// and left alone. They are never deleted.
const MISSING = { $in: [null, ''] };
// Mongo selector matching every broken card. Used by the count AND the repair.
function brokenCardsSelector() {
return {
$or: [
{ boardId: MISSING },
{ swimlaneId: MISSING },
{ listId: MISSING },
],
};
}
// Selector for the repairable subset: still has a board, missing a list and/or
// a swimlane.
function repairableCardsSelector() {
return {
boardId: { $nin: [null, ''] },
$or: [{ listId: MISSING }, { swimlaneId: MISSING }],
};
}
// Selector for the cards no automatic repair can place.
function unfixableCardsSelector() {
return { boardId: MISSING };
}
function isMissing(value) {
return typeof value !== 'string' || value.length === 0;
}
// True when this card is counted as broken.
function isBrokenCard(card) {
if (!card) return false;
return isMissing(card.boardId) || isMissing(card.swimlaneId) || isMissing(card.listId);
}
// Plan the repair of a set of cards (plain objects, no DB access).
// Returns:
// { needsList: [ids], needsSwimlane: [ids], unfixable: [ids], boardIds: [ids] }
// `boardIds` is every board a repair will need a default list/swimlane for, so
// the caller can resolve each board once instead of once per card.
function planBrokenCardRepair(cards) {
const needsList = [];
const needsSwimlane = [];
const unfixable = [];
const boardIds = new Set();
for (const card of Array.isArray(cards) ? cards : []) {
if (!card || isMissing(card._id)) continue;
if (isMissing(card.boardId)) {
// No board: nothing to attach it to.
unfixable.push(card._id);
continue;
}
let touched = false;
if (isMissing(card.listId)) {
needsList.push(card._id);
touched = true;
}
if (isMissing(card.swimlaneId)) {
needsSwimlane.push(card._id);
touched = true;
}
if (touched) boardIds.add(card.boardId);
}
return { needsList, needsSwimlane, unfixable, boardIds: [...boardIds] };
}
module.exports = {
brokenCardsSelector,
repairableCardsSelector,
unfixableCardsSelector,
isBrokenCard,
planBrokenCardRepair,
};