2018-08-16 00:32:31 +02:00
|
|
|
RulesHelper = {
|
|
|
|
|
|
|
|
|
|
|
|
executeRules(activity){
|
|
|
|
const matchingRules = this.findMatchingRules(activity);
|
2018-08-16 00:42:14 +02:00
|
|
|
console.log(matchingRules);
|
2018-08-16 00:32:31 +02:00
|
|
|
for(let i = 0;i< matchingRules.length;i++){
|
2018-08-16 00:42:14 +02:00
|
|
|
console.log(matchingRules[i]);
|
2018-08-16 00:32:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|