summaryrefslogtreecommitdiffstats
path: root/models/cards.js
diff options
context:
space:
mode:
Diffstat (limited to 'models/cards.js')
-rw-r--r--models/cards.js248
1 files changed, 205 insertions, 43 deletions
diff --git a/models/cards.js b/models/cards.js
index 9b93bd7c..6de95123 100644
--- a/models/cards.js
+++ b/models/cards.js
@@ -246,7 +246,7 @@ Cards.attachSchema(new SimpleSchema({
* type of the card
*/
type: String,
- defaultValue: '',
+ defaultValue: 'cardType-card',
},
linkedId: {
/**
@@ -272,6 +272,68 @@ Cards.allow({
});
Cards.helpers({
+ copy(boardId, swimlaneId, listId) {
+ const oldBoard = Boards.findOne(this.boardId);
+ const oldBoardLabels = oldBoard.labels;
+ // Get old label names
+ const oldCardLabels = _.pluck(_.filter(oldBoardLabels, (label) => {
+ return _.contains(this.labelIds, label._id);
+ }), 'name');
+
+ const newBoard = Boards.findOne(boardId);
+ const newBoardLabels = newBoard.labels;
+ const newCardLabels = _.pluck(_.filter(newBoardLabels, (label) => {
+ return _.contains(oldCardLabels, label.name);
+ }), '_id');
+
+ const oldId = this._id;
+ const oldCard = Cards.findOne(oldId);
+
+ // Copy Custom Fields
+ if (oldBoard._id !== boardId) {
+ CustomFields.find({
+ _id: {$in: oldCard.customFields.map((cf) => { return cf._id; })},
+ }).forEach((cf) => {
+ if (!_.contains(cf.boardIds, boardId))
+ cf.addBoard(boardId);
+ });
+ }
+
+ delete this._id;
+ delete this.labelIds;
+ this.labelIds = newCardLabels;
+ this.boardId = boardId;
+ this.swimlaneId = swimlaneId;
+ this.listId = listId;
+ const _id = Cards.insert(this);
+
+ // Copy attachments
+ oldCard.attachments().forEach((att) => {
+ att.cardId = _id;
+ delete att._id;
+ return Attachments.insert(att);
+ });
+
+ // copy checklists
+ Checklists.find({cardId: oldId}).forEach((ch) => {
+ ch.copy(_id);
+ });
+
+ // copy subtasks
+ Cards.find({parentId: oldId}).forEach((subtask) => {
+ subtask.parentId = _id;
+ subtask._id = null;
+ Cards.insert(subtask);
+ });
+
+ // copy card comments
+ CardComments.find({cardId: oldId}).forEach((cmt) => {
+ cmt.copy(_id);
+ });
+
+ return _id;
+ },
+
list() {
return Lists.findOne(this.listId);
},
@@ -421,7 +483,7 @@ Cards.helpers({
// get all definitions
const definitions = CustomFields.find({
- boardId: this.boardId,
+ boardIds: {$in: [this.boardId]},
}).fetch();
// match right definition to each field
@@ -430,6 +492,9 @@ Cards.helpers({
const definition = definitions.find((definition) => {
return definition._id === customField._id;
});
+ if (!definition) {
+ return {};
+ }
//search for "True Value" which is for DropDowns other then the Value (which is the id)
let trueValue = customField.value;
if (definition.settings.dropdownItems && definition.settings.dropdownItems.length > 0) {
@@ -930,6 +995,10 @@ Cards.helpers({
return this.assignedBy;
}
},
+
+ isTemplateCard() {
+ return this.type === 'template-card';
+ },
});
Cards.mutations({
@@ -963,50 +1032,41 @@ Cards.mutations({
};
},
- setTitle(title) {
- return {
- $set: {
- title,
- },
- };
- },
+ move(boardId, swimlaneId, listId, sort) {
+ // Copy Custom Fields
+ if (this.boardId !== boardId) {
+ CustomFields.find({
+ _id: {$in: this.customFields.map((cf) => { return cf._id; })},
+ }).forEach((cf) => {
+ if (!_.contains(cf.boardIds, boardId))
+ cf.addBoard(boardId);
+ });
+ }
- setDescription(description) {
- return {
- $set: {
- description,
- },
- };
- },
+ // Get label names
+ const oldBoard = Boards.findOne(this.boardId);
+ const oldBoardLabels = oldBoard.labels;
+ const oldCardLabels = _.pluck(_.filter(oldBoardLabels, (label) => {
+ return _.contains(this.labelIds, label._id);
+ }), 'name');
- setRequestedBy(requestedBy) {
- return {
- $set: {
- requestedBy,
- },
- };
- },
+ const newBoard = Boards.findOne(boardId);
+ const newBoardLabels = newBoard.labels;
+ const newCardLabelIds = _.pluck(_.filter(newBoardLabels, (label) => {
+ return label.name && _.contains(oldCardLabels, label.name);
+ }), '_id');
- setAssignedBy(assignedBy) {
- return {
- $set: {
- assignedBy,
- },
- };
- },
-
- move(swimlaneId, listId, sortIndex) {
- const list = Lists.findOne(listId);
const mutatedFields = {
+ boardId,
swimlaneId,
listId,
- boardId: list.boardId,
- sort: sortIndex,
+ sort,
+ labelIds: newCardLabelIds,
};
- return {
+ Cards.update(this._id, {
$set: mutatedFields,
- };
+ });
},
addLabel(labelId) {
@@ -1228,9 +1288,48 @@ Cards.mutations({
//FUNCTIONS FOR creation of Activities
-function cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId) {
- if ((_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
- (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)){
+function updateActivities(doc, fieldNames, modifier) {
+ if (_.contains(fieldNames, 'labelIds') && _.contains(fieldNames, 'boardId')) {
+ Activities.find({
+ activityType: 'addedLabel',
+ cardId: doc._id,
+ }).forEach((a) => {
+ const lidx = doc.labelIds.indexOf(a.labelId);
+ if (lidx !== -1 && modifier.$set.labelIds.length > lidx) {
+ Activities.update(a._id, {
+ $set: {
+ labelId: modifier.$set.labelIds[doc.labelIds.indexOf(a.labelId)],
+ boardId: modifier.$set.boardId,
+ },
+ });
+ } else {
+ Activities.remove(a._id);
+ }
+ });
+ } else if (_.contains(fieldNames, 'boardId')) {
+ Activities.remove({
+ activityType: 'addedLabel',
+ cardId: doc._id,
+ });
+ }
+}
+
+function cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId) {
+ if (_.contains(fieldNames, 'boardId') && (doc.boardId !== oldBoardId)) {
+ Activities.insert({
+ userId,
+ activityType: 'moveCardBoard',
+ boardName: Boards.findOne(doc.boardId).title,
+ boardId: doc.boardId,
+ oldBoardId,
+ oldBoardName: Boards.findOne(oldBoardId).title,
+ cardId: doc._id,
+ swimlaneName: Swimlanes.findOne(doc.swimlaneId).title,
+ swimlaneId: doc.swimlaneId,
+ oldSwimlaneId,
+ });
+ } else if ((_.contains(fieldNames, 'listId') && doc.listId !== oldListId) ||
+ (_.contains(fieldNames, 'swimlaneId') && doc.swimlaneId !== oldSwimlaneId)){
Activities.insert({
userId,
oldListId,
@@ -1286,6 +1385,9 @@ function cardMembers(userId, doc, fieldNames, modifier) {
activityType: 'joinMember',
boardId: doc.boardId,
cardId: doc._id,
+ memberId,
+ listId: doc.listId,
+ swimlaneId: doc.swimlaneId,
});
}
}
@@ -1302,6 +1404,9 @@ function cardMembers(userId, doc, fieldNames, modifier) {
activityType: 'unjoinMember',
boardId: doc.boardId,
cardId: doc._id,
+ memberId,
+ listId: doc.listId,
+ swimlaneId: doc.swimlaneId,
});
}
}
@@ -1342,6 +1447,56 @@ function cardLabels(userId, doc, fieldNames, modifier) {
}
}
+function cardCustomFields(userId, doc, fieldNames, modifier) {
+ if (!_.contains(fieldNames, 'customFields'))
+ return;
+
+ // Say hello to the new customField value
+ if (modifier.$set) {
+ _.each(modifier.$set, (value, key) => {
+ if (key.startsWith('customFields')) {
+ const dotNotation = key.split('.');
+
+ // only individual changes are registered
+ if (dotNotation.length > 1) {
+ const customFieldId = doc.customFields[dotNotation[1]]._id;
+ const act = {
+ userId,
+ customFieldId,
+ value,
+ activityType: 'setCustomField',
+ boardId: doc.boardId,
+ cardId: doc._id,
+ };
+ Activities.insert(act);
+ }
+ }
+ });
+ }
+
+ // Say goodbye to the former customField value
+ if (modifier.$unset) {
+ _.each(modifier.$unset, (value, key) => {
+ if (key.startsWith('customFields')) {
+ const dotNotation = key.split('.');
+
+ // only individual changes are registered
+ if (dotNotation.length > 1) {
+ const customFieldId = doc.customFields[dotNotation[1]]._id;
+ const act = {
+ userId,
+ customFieldId,
+ activityType: 'unsetCustomField',
+ boardId: doc.boardId,
+ cardId: doc._id,
+ };
+ Activities.insert(act);
+ }
+ }
+ });
+ }
+}
+
function cardCreation(userId, doc) {
Activities.insert({
userId,
@@ -1363,8 +1518,8 @@ function cardRemover(userId, doc) {
Checklists.remove({
cardId: doc._id,
});
- Subtasks.remove({
- cardId: doc._id,
+ Cards.remove({
+ parentId: doc._id,
});
CardComments.remove({
cardId: doc._id,
@@ -1400,12 +1555,14 @@ if (Meteor.isServer) {
Cards.after.update(function(userId, doc, fieldNames) {
const oldListId = this.previous.listId;
const oldSwimlaneId = this.previous.swimlaneId;
- cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId);
+ const oldBoardId = this.previous.boardId;
+ cardMove(userId, doc, fieldNames, oldListId, oldSwimlaneId, oldBoardId);
});
// Add a new activity if we add or remove a member to the card
Cards.before.update((userId, doc, fieldNames, modifier) => {
cardMembers(userId, doc, fieldNames, modifier);
+ updateActivities(doc, fieldNames, modifier);
});
// Add a new activity if we add or remove a label to the card
@@ -1413,6 +1570,11 @@ if (Meteor.isServer) {
cardLabels(userId, doc, fieldNames, modifier);
});
+ // Add a new activity if we edit a custom field
+ Cards.before.update((userId, doc, fieldNames, modifier) => {
+ cardCustomFields(userId, doc, fieldNames, modifier);
+ });
+
// Remove all activities associated with a card if we remove the card
// Remove also card_comments / checklists / attachments
Cards.after.remove((userId, doc) => {