瀏覽代碼

fixes retrieval of next board cardnumber

Kai Lehmann 3 年之前
父節點
當前提交
e8522c323f
共有 1 個文件被更改,包括 17 次插入2 次删除
  1. 17 2
      models/boards.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) {