|
@@ -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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|