mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
#6815 checked the number of every new card, but a card also enters a board's numbering by an update: a move from the browser, which computed the destination number from the cards it had loaded (often none, so 1), a restore from the archive, and a number set by the REST API or an import. A before-update hook now does what the insert hook does: a taken number is replaced by a fresh one from the board's counter, a free one is kept and the counter raised past it. Thanks to Raymond-Voogt and xet7 ! Fixes #6858,
56 lines
2.9 KiB
JavaScript
56 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
// #6815: card numbers are board-wide and meant to be unique (#5813 / #4743), but a card
|
|
// added in the browser (add-card form, template, link, checklist item to card)
|
|
// brings the number the browser computed itself: the highest number among the
|
|
// cards it HAS, plus one. With lazy card loading the browser only has the cards
|
|
// in view, so it reissues numbers that cards outside its view already carry -
|
|
// and two people adding a card at the same moment get the same number. The
|
|
// server's atomic counter (Boards.getNextCardNumber) never saw those numbers, so
|
|
// it reissued them too. One production board set had 842 numbers used twice on
|
|
// 452 boards; 231 of the pairs were created more than a day apart.
|
|
//
|
|
// The server therefore checks every new card's number (server/models/cards.js,
|
|
// Cards.before.insert): a number another card on the board already has is
|
|
// replaced by a fresh one from the counter, and the counter is raised to any
|
|
// number it did not issue, so it never hands that one out again.
|
|
|
|
const counterName = boardId => `cardNumber-${boardId}`;
|
|
|
|
// Only a real number is checked: 0 / missing means "no number" and stays so.
|
|
function isCardNumber(n) {
|
|
return Number.isSafeInteger(n) && n > 0;
|
|
}
|
|
|
|
function needsNewCardNumber(cardNumber, takenByAnotherCard) {
|
|
return isCardNumber(cardNumber) && !!takenByAnotherCard;
|
|
}
|
|
|
|
// #6815: what the counter is raised to for a free number: that number, or the
|
|
// board's highest one when the counter is new and that is higher.
|
|
function counterFloor(cardNumber, boardTop) {
|
|
return isCardNumber(boardTop) ? Math.max(cardNumber, boardTop) : cardNumber;
|
|
}
|
|
|
|
// #6815 follow-up: an UPDATE can bring a card into a board's numbering too.
|
|
// - A move to another board: the browser computes the destination number
|
|
// itself (Boards.getNextCardNumber on the client is the highest number among
|
|
// the destination cards it has loaded, plus one - often none of them).
|
|
// - A restore from the archive: an archived card may share its number with an
|
|
// open card from before the insert check existed.
|
|
// - A $set of cardNumber: from the REST API or an import.
|
|
// Returns { boardId, cardNumber } to check, or null when the update leaves the
|
|
// card's place in the numbering alone.
|
|
function numberToCheckOnUpdate(doc, set) {
|
|
if (!doc || !set || typeof set !== 'object') return null;
|
|
const movesBoard = set.boardId !== undefined && String(set.boardId) !== String(doc.boardId);
|
|
const restores = set.archived === false && doc.archived === true;
|
|
const setsNumber = set.cardNumber !== undefined;
|
|
if (!movesBoard && !restores && !setsNumber) return null;
|
|
const cardNumber = setsNumber ? set.cardNumber : doc.cardNumber;
|
|
if (!isCardNumber(cardNumber)) return null;
|
|
return { boardId: movesBoard ? set.boardId : doc.boardId, cardNumber };
|
|
}
|
|
|
|
module.exports = { counterName, isCardNumber, needsNewCardNumber, counterFloor, numberToCheckOnUpdate };
|