Browse Source

Main flow implemented

Angelo Gallarello 6 years ago
parent
commit
6828ccd7f1

+ 2 - 0
client/components/rules/rulesMain.js

@@ -29,6 +29,8 @@ BlazeComponent.extendComponent({
     return [{'click .js-delete-rule'(event) {
       const rule = this.currentData();
       Rules.remove(rule._id);
+      Actions.remove(rule.actionId);
+      Triggers.remove(rule.triggerId);
 
     },
     'click .js-goto-trigger'(event) {

+ 1 - 1
client/components/rules/triggers/boardTriggers.js

@@ -11,7 +11,7 @@ BlazeComponent.extendComponent({
       const actionSelected = this.find('#action').value;
       const boardId = Session.get('currentBoard')
       if(actionSelected == "created"){
-        Triggers.insert({activityType: "createCard","boardId":boardId},function(error,id){
+        Triggers.insert({activityType: "createCard","boardId":boardId,"listId":"*"},function(error,id){
           datas.triggerIdVar.set(id);
         });
       }

+ 0 - 8
models/actions.js

@@ -43,14 +43,6 @@ Actions.helpers({
 
 
 
-if (Meteor.isServer) {
-  Meteor.startup(() => {
-    const rules = Triggers.findOne({});
-    if(!rules){
-       Actions.insert({actionType: "moveCardToTop"});
-    }
-  });
-}
 
 
 

+ 5 - 5
models/activities.js

@@ -60,13 +60,13 @@ Activities.before.insert((userId, doc) => {
 
 Activities.after.insert((userId, doc) => {
    const activity = Activities._transform(doc);
-   const matchedTriggers = Triggers.find(activity);
-   if(matchedTriggers.count() > 0){
-     const card = activity.card();
-     Cards.direct.update({_id: card._id},{$set: {title: "ciaooo"}});
-   }
+   RulesHelper.executeRules(activity);
+
 });
 
+
+
+
 if (Meteor.isServer) {
   // For efficiency create indexes on the date of creation, and on the date of
   // creation in conjunction with the card or board id, as corresponding views

+ 1 - 1
models/lists.js

@@ -82,7 +82,7 @@ Lists.helpers({
     };
     if (swimlaneId)
       selector.swimlaneId = swimlaneId;
-    return Cards.find(Filter.mongoSelector(selector),
+    return Cards.find(selector,
       { sort: ['sort'] });
   },
 

+ 5 - 0
models/rules.js

@@ -21,6 +21,11 @@ Rules.mutations({
   },
 });
 
+Rules.helpers({
+  getAction(){
+    return Actions.findOne({_id:this.actionId});
+  },
+});
 
 
 

+ 6 - 0
models/triggers.js

@@ -21,6 +21,12 @@ Triggers.allow({
 
 
 Triggers.helpers({
+
+
+  getRule(){
+    return Rules.findOne({triggerId:this._id});
+  },
+
   fromList() {
     return Lists.findOne(this.fromId);
   },

+ 41 - 0
server/rulesHelper.js

@@ -0,0 +1,41 @@
+RulesHelper = {
+
+
+	executeRules(activity){
+		const matchingRules = this.findMatchingRules(activity);
+		for(let i = 0;i< matchingRules.length;i++){
+			const actionType = matchingRules[i].getAction().actionType;
+			this.performAction(activity,actionType);
+		}
+	},
+
+	performAction(activity,actionType){
+		if(actionType == "moveCardToTop"){
+			const card = Cards.findOne({_id:activity.cardId});
+		    const minOrder = _.min(card.list().cards(card.swimlaneId).map((c) => c.sort));
+		    card.move(card.swimlaneId, card.listId, minOrder - 1);
+		}
+	},
+	findMatchingRules(activity){
+		const activityType = activity.activityType;
+		const matchingFields = TriggersDef[activityType].matchingFields;
+		const matchingMap = this.buildMatchingFieldsMap(activity,matchingFields);
+		let matchingTriggers = Triggers.find(matchingMap);
+		let matchingRules = [];
+		matchingTriggers.forEach(function(trigger){
+			matchingRules.push(trigger.getRule());
+		});
+		return matchingRules;
+	},
+	buildMatchingFieldsMap(activity, matchingFields){
+		let matchingMap = {};
+		for(let i = 0;i< matchingFields.length;i++){
+			// Creating a matching map with the actual field of the activity
+			// and with the wildcard (for example: trigger when a card is added
+			// in any [*] board
+			matchingMap[matchingFields[i]] = { $in: [activity[matchingFields[i]],"*"]};
+		}
+		return matchingMap;
+	}
+
+}

+ 39 - 0
server/triggersDef.js

@@ -0,0 +1,39 @@
+TriggersDef = {
+	createCard:{
+		matchingFields: ["boardId","listId"]
+	},
+	moveCard:{
+		matchingFields: ["boardId","listId","oldListId"]
+	},
+	archivedCard:{
+		matchingFields: ["boardId"]
+	}
+}
+
+
+		// if(activityType == "createCard"){
+
+		// }
+		// if(activityType == "moveCard"){
+
+		// }
+		// if(activityType == "archivedCard"){
+
+		// }
+		// if(activityType == "restoredCard"){
+			
+		// }
+		// if(activityType == "joinMember"){
+
+		// }
+		// if(activityType == "unJoinMember"){
+			
+		// }
+		// if(activityType == "addChecklist"){
+			
+		// }
+		// if(activityType == "addChecklistItem"){
+			
+		// }
+
+