소스 검색

Merge pull request #3458 from jrsupplee/issue-2924

Issue 2924: Rules not copied during board copy
Lauri Ojansivu 4 년 전
부모
커밋
1971037049
4개의 변경된 파일80개의 추가작업 그리고 8개의 파일을 삭제
  1. 6 2
      client/components/boards/boardsList.js
  2. 12 6
      client/components/lists/listBody.js
  3. 46 0
      models/boards.js
  4. 16 0
      server/publications/boards.js

+ 6 - 2
client/components/boards/boardsList.js

@@ -124,9 +124,13 @@ BlazeComponent.extendComponent({
         },
         'click .js-clone-board'(evt) {
           Meteor.call(
-            'cloneBoard',
+            'copyBoard',
             this.currentData()._id,
-            Session.get('fromBoard'),
+            {
+              sort: Boards.find({ archived: false }).count(),
+              type: 'board',
+              title: Boards.findOne(this.currentData()._id).copyTitle(),
+            },
             (err, res) => {
               if (err) {
                 this.setError(err.error);

+ 12 - 6
client/components/lists/listBody.js

@@ -675,12 +675,18 @@ BlazeComponent.extendComponent({
             element.type = 'swimlane';
             _id = element.copy(this.boardId);
           } else if (this.isBoardTemplateSearch) {
-            board = Boards.findOne(element.linkedId);
-            board.sort = Boards.find({ archived: false }).count();
-            board.type = 'board';
-            board.title = element.title;
-            delete board.slug;
-            _id = board.copy();
+            Meteor.call(
+              'copyBoard',
+              element.linkedId,
+              {
+                sort: Boards.find({ archived: false }).count(),
+                type: 'board',
+                title: element.title,
+              },
+              (err, data) => {
+                _id = data;
+              },
+            );
           }
           Popup.close();
         },

+ 46 - 0
models/boards.js

@@ -508,6 +508,7 @@ Boards.helpers({
   copy() {
     const oldId = this._id;
     delete this._id;
+    delete this.slug;
     const _id = Boards.insert(this);
 
     // Copy all swimlanes in board
@@ -537,7 +538,52 @@ Boards.helpers({
         },
       });
     });
+
+    // copy rules, actions, and triggers
+    const actionsMap = {};
+    Actions.find({ boardId: oldId }).forEach(action => {
+      const id = action._id;
+      delete action._id;
+      action.boardId = _id;
+      actionsMap[id] = Actions.insert(action);
+    });
+    const triggersMap = {};
+    Triggers.find({ boardId: oldId }).forEach(trigger => {
+      const id = trigger._id;
+      delete trigger._id;
+      trigger.boardId = _id;
+      triggersMap[id] = Triggers.insert(trigger);
+    });
+    Rules.find({ boardId: oldId }).forEach(rule => {
+      delete rule._id;
+      rule.boardId = _id;
+      rule.actionId = actionsMap[rule.actionId];
+      rule.triggerId = triggersMap[rule.triggerId];
+      Rules.insert(rule);
+    });
   },
+  /**
+   * Return a unique title based on the current title
+   *
+   * @returns {string|null}
+   */
+  copyTitle() {
+    const m = this.title.match(/^(?<title>.*?)\s*(\[(?<num>\d+)]\s*$|\s*$)/);
+    const title = m.groups.title;
+    let num = 0;
+    Boards.find({ title: new RegExp(`^${title}\\s*\\[\\d+]\\s*$`) }).forEach(
+      board => {
+        const m = board.title.match(/^(?<title>.*?)\s*\[(?<num>\d+)]\s*$/);
+        if (m) {
+          const n = parseInt(m.groups.num, 10);
+          num = num < n ? n : num;
+        }
+      },
+    );
+
+    return `${title} [${num + 1}]`;
+  },
+
   /**
    * Is supplied user authorized to view this board?
    */

+ 16 - 0
server/publications/boards.js

@@ -209,3 +209,19 @@ Meteor.publishRelations('board', function(boardId, isArchived) {
 
   return this.ready();
 });
+
+Meteor.methods({
+  copyBoard(boardId, properties) {
+    check(boardId, String);
+    check(properties, Object);
+
+    const board = Boards.findOne(boardId);
+    if (board) {
+      for (const key in properties) {
+        board[key] = properties[key];
+      }
+      return board.copy();
+    }
+    return null;
+  },
+});