Преглед изворни кода

Merge pull request #3936 from syndimann/feature/boardwise-card-numbers

Fix: Consecutive Card numbering when a card is moved to another board or copied
Lauri Ojansivu пре 4 година
родитељ
комит
f01b440fb7
2 измењених фајлова са 22 додато и 2 уклоњено
  1. 17 2
      models/boards.js
  2. 5 0
      models/cards.js

+ 17 - 2
models/boards.js

@@ -1065,8 +1065,23 @@ Boards.helpers({
   },
 
   getNextCardNumber() {
-    const boardCards = Cards.find({ boardId: this._id }).fetch();
-    return boardCards.length + 1;
+    const boardCards = Cards.find(
+      {
+        boardId: this._id
+      },
+      {
+        sort: { cardNumber: -1 },
+        limit: 1
+      }
+    ).fetch();
+
+    // If no card is assigned to the board, return 1
+    if (!boardCards || boardCards.length === 0) {
+      return 1;
+    }
+
+    const maxCardNr = !!boardCards[0].cardNumber ? boardCards[0].cardNumber : 0;
+    return maxCardNr + 1;
   },
 
   cardsDueInBetween(start, end) {

+ 5 - 0
models/cards.js

@@ -577,6 +577,7 @@ Cards.helpers({
 
     delete this._id;
     this.boardId = boardId;
+    this.cardNumber = Boards.findOne(boardId).getNextCardNumber();
     this.swimlaneId = swimlaneId;
     this.listId = listId;
     const _id = Cards.insert(this);
@@ -1989,8 +1990,12 @@ Cards.mutations({
         '_id',
       );
 
+      // assign the new card number from the target board
+      const newCardNumber = newBoard.getNextCardNumber();
+
       Object.assign(mutatedFields, {
         labelIds: newCardLabelIds,
+        cardNumber: newCardNumber
       });
 
       mutatedFields.customFields = this.mapCustomFieldsToBoard(newBoard._id);